Get play state working again.
This commit is contained in:
parent
073c65c81c
commit
4e99903be5
@ -449,9 +449,6 @@ I'm thinking there should be at least 4 game states:
|
||||
Play.__super.init(self)
|
||||
self.map = Map:new("assets/map.lua")
|
||||
self.current_frame = 0
|
||||
-- TODO: replace x, y (180, 100) with player position from Tiled map
|
||||
self.player_entity = Entity:new(180, 100, 4, 8, self.physics.gravity, 2*8, self.physics.horizontal_acc, self.physics.friction)
|
||||
|
||||
self.physics = {
|
||||
gravity=self.map.tileheight*9.8, -- somewhat exagerated gravity (3x)
|
||||
maxdx=self.map.tilewidth*8, -- max horizontal speed
|
||||
@ -460,6 +457,8 @@ I'm thinking there should be at least 4 game states:
|
||||
}
|
||||
self.physics.horizontal_acc = self.physics.maxdx*2 -- horizontal acceleration
|
||||
self.physics.friction = self.physics.maxdx*1.5 -- horizontal friction
|
||||
-- TODO: replace x, y (180, 100) with player position from Tiled map
|
||||
self.player_entity = Entity:new(180, 100, 4, 8, self.physics.gravity, 2*8, self.physics.horizontal_acc, self.physics.friction, self.map)
|
||||
end
|
||||
|
||||
function Play:updatePlayer(dt)
|
||||
@ -477,7 +476,7 @@ I'm thinking there should be at least 4 game states:
|
||||
|
||||
function Play:update(dt)
|
||||
Play.__super.update(self, dt)
|
||||
self.current_frame = game.current_frame + 1
|
||||
self.current_frame = self.current_frame + 1
|
||||
self:updatePlayer(dt)
|
||||
self:updateCamera(dt)
|
||||
self:updateItems(dt)
|
||||
@ -667,6 +666,13 @@ Go through each tile in the map and query its value to draw solid tiles (value >
|
||||
end
|
||||
end
|
||||
|
||||
function Map:get_tile_from_pixel(pixel)
|
||||
local tile_x = math.floor(pixel.x/self.tilewidth) + 1
|
||||
local tile_y = math.floor(pixel.y/self.tileheight) + 1
|
||||
local value = self.data[tile_x + (tile_y - 1)*self.width]
|
||||
return Tile:new(tile_x, tile_y, self.tilewidth, self.tileheight, value)
|
||||
end
|
||||
|
||||
return Map
|
||||
#+end_src
|
||||
|
||||
@ -736,7 +742,7 @@ All of that while having gravity always affect an entity. I'm thinking of starti
|
||||
|
||||
local Entity = Object:extend()
|
||||
|
||||
function Entity:init(x, y, w, h, gravity, max_jump_height, horizontal_acc, friction)
|
||||
function Entity:init(x, y, w, h, gravity, max_jump_height, horizontal_acc, friction, map)
|
||||
Entity.__super.init(self, x, y, w, h)
|
||||
self.gravity = gravity
|
||||
self.pos = Vector:new(x, y)
|
||||
@ -748,6 +754,7 @@ All of that while having gravity always affect an entity. I'm thinking of starti
|
||||
self.horizontal_acc = horizontal_acc
|
||||
self.max_jump_height = max_jump_height
|
||||
self.friction = friction
|
||||
self.map = map
|
||||
end
|
||||
|
||||
function Entity:get_rect()
|
||||
@ -770,7 +777,7 @@ prevent actually moving there.
|
||||
The first step is to create a copy of the current state so we work on that rather than the actual entity, thus, let's define a ~copy~ method:
|
||||
#+begin_src lua :tangle entity.lua
|
||||
function Entity:copy()
|
||||
local new = Entity:new(self.pos.x, self.pos.y, self.width, self.height, self.gravity, self.max_jump_height, self.horizontal_acc, self.friction)
|
||||
local new = Entity:new(self.pos.x, self.pos.y, self.width, self.height, self.gravity, self.max_jump_height, self.horizontal_acc, self.friction, self.map)
|
||||
new.vel.x = self.vel.x
|
||||
new.vel.y = self.vel.y
|
||||
new.grounded = self.grounded
|
||||
@ -802,7 +809,7 @@ First, let's do the horizontal movement (walking or flying?)
|
||||
end
|
||||
end
|
||||
next_state.vel.x = helpers.bound(self.vel.x + x_accel*dt, -self.maxvel.x, self.maxvel.x)
|
||||
next_state.pos.x = helpers.normalize(self.pos.x + next_state.vel.x*dt, self.width/2, Tile.max_pixel.x + self.width*1.5)
|
||||
next_state.pos.x = helpers.normalize(self.pos.x + next_state.vel.x*dt, self.width/2, self.map.max_pixel.x + self.width*1.5)
|
||||
|
||||
if self.vel.x < 0 and next_state.vel.x > 0 or self.vel.x > 0 and next_state.vel.x < 0 then
|
||||
next_state.vel.x = 0
|
||||
@ -818,7 +825,7 @@ Next, let's handle jumps with a method that assumes the player is allowed to jum
|
||||
local next_state = self:copy()
|
||||
next_state.grounded = false
|
||||
next_state.vel.y = -math.sqrt(2*self.max_jump_height*self.gravity)
|
||||
next_state.pos.y = helpers.bound(next_state.pos.y + next_state.vel.y*dt, 0, Tile.max_pixel.y - self.height/2)
|
||||
next_state.pos.y = helpers.bound(next_state.pos.y + next_state.vel.y*dt, 0, self.map.max_pixel.y - self.height/2)
|
||||
return next_state
|
||||
end
|
||||
#+end_src
|
||||
@ -828,7 +835,7 @@ When the entity isn't grounded and isn't jumping, it's falling:
|
||||
local next_state = self:copy()
|
||||
next_state.grounded = false
|
||||
next_state.vel.y = self.vel.y + self.gravity*dt
|
||||
next_state.pos.y = helpers.bound(next_state.pos.y + next_state.vel.y*dt, 0, Tile.max_pixel.y - self.height/2)
|
||||
next_state.pos.y = helpers.bound(next_state.pos.y + next_state.vel.y*dt, 0, self.map.max_pixel.y - self.height/2)
|
||||
return next_state
|
||||
end
|
||||
#+end_src
|
||||
@ -846,10 +853,10 @@ We'll need to check for collisions against solid tiles and adjust the entities p
|
||||
#+begin_src lua :tangle entity.lua
|
||||
function Entity:adjust_for_collisions(old_state, dt, frame)
|
||||
local next_state = self:copy()
|
||||
local top_left_tile = Tile:from_pixel(next_state.pos)
|
||||
local bottom_left_tile = top_left_tile + Tile:new(0, 1)
|
||||
local bottom_right_tile = top_left_tile + Tile:new(1, 1)
|
||||
local top_right_tile = top_left_tile + Tile:new(1, 0)
|
||||
local top_left_tile = self.map:get_tile_from_pixel(next_state.pos)
|
||||
local bottom_left_tile = self.map:get_tile_from_pixel(next_state.pos + Vector:new(0, self.map.tileheight))
|
||||
local bottom_right_tile = self.map:get_tile_from_pixel(next_state.pos + Vector:new(self.map.tilewidth, self.map.tileheight))
|
||||
local top_right_tile = self.map:get_tile_from_pixel(next_state.pos + Vector:new(self.map.tilewidth, 0))
|
||||
|
||||
local next_rect = next_state:get_rect()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user