first commit
This commit is contained in:
18
src/states/entity/EntityBaseState.lua
Normal file
18
src/states/entity/EntityBaseState.lua
Normal file
@@ -0,0 +1,18 @@
|
||||
-- require('mobdebug').start()
|
||||
|
||||
EntityBaseState = Class{}
|
||||
|
||||
function EntityBaseState:init(entity)
|
||||
self.entity = entity
|
||||
end
|
||||
|
||||
function EntityBaseState:update(dt) end
|
||||
function EntityBaseState:enter() end
|
||||
function EntityBaseState:exit() end
|
||||
function EntityBaseState:processAI(params, dt) end
|
||||
|
||||
function EntityBaseState:render()
|
||||
local anim = self.entity.currentAnimation
|
||||
love.graphics.draw(gTextures[anim.texture], gFrames[anim.texture][anim:getCurrentFrame()],
|
||||
math.floor(self.entity.x), math.floor(self.entity.y))
|
||||
end
|
||||
32
src/states/entity/EntityIdleState.lua
Normal file
32
src/states/entity/EntityIdleState.lua
Normal file
@@ -0,0 +1,32 @@
|
||||
-- require('mobdebug').start()
|
||||
|
||||
EntityIdleState = Class{__includes = BaseState}
|
||||
|
||||
function EntityIdleState:init(entity)
|
||||
self.entity = entity
|
||||
|
||||
self.entity:changeAnimation('idle-' .. self.entity.direction)
|
||||
|
||||
end
|
||||
|
||||
function EntityIdleState:update(dt)
|
||||
if love.keyboard.isDown('left') or love.keyboard.isDown('right') or
|
||||
love.keyboard.isDown('up') or love.keyboard.isDown('down') then
|
||||
if not self.entity:onTheEdge() then
|
||||
Event.dispatch('walking')
|
||||
self.entity:changeState('walk')
|
||||
else
|
||||
Event.dispatch('on-edge')
|
||||
self.entity:changeState('edge')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function EntityIdleState:render()
|
||||
local anim = self.entity.currentAnimation
|
||||
love.graphics.draw(gTextures[anim.texture], gFrames[anim.texture][anim:getCurrentFrame()],self.entity.x, self.entity.y)
|
||||
|
||||
-- love.graphics.setColor(255, 0, 255, 255)
|
||||
-- love.graphics.rectangle('line', self.entity.x, self.entity.y, self.entity.width, self.entity.height)
|
||||
-- love.graphics.setColor(255, 255, 255, 255)
|
||||
end
|
||||
100
src/states/entity/EntityWalkEdgeState.lua
Normal file
100
src/states/entity/EntityWalkEdgeState.lua
Normal file
@@ -0,0 +1,100 @@
|
||||
-- require('mobdebug').start()
|
||||
|
||||
|
||||
EntityWalkEdgeState = Class{__includes = BaseState}
|
||||
|
||||
function EntityWalkEdgeState:init(entity)
|
||||
self.entity = entity
|
||||
self.entity:changeAnimation('walk-'..self.entity.direction)
|
||||
end
|
||||
|
||||
function EntityWalkEdgeState:update(dt)
|
||||
|
||||
local dy = self.entity.walkingSpeed * dt
|
||||
local dx = self.entity.walkingSpeed * dt
|
||||
|
||||
if self.entity.direction == 'left' then
|
||||
if self.entity.level:pointOnEdge(self.entity.x - dx + TILE_SIZE / 2, self.entity.y + TILE_SIZE / 2) then
|
||||
self.entity.x = self.entity.x - dx
|
||||
Event.dispatch('on-edge')
|
||||
else
|
||||
if self.entity.level:insideBounds(self.entity.x + TILE_SIZE / 2 - dx, self.entity.y + TILE_SIZE / 2) then
|
||||
self.entity:changeState('walk')
|
||||
Event.dispatch('walking')
|
||||
self.entity.x = self.entity.x - dx
|
||||
end
|
||||
end
|
||||
elseif self.entity.direction == 'right' then
|
||||
if self.entity.level:pointOnEdge(self.entity.x + dx + TILE_SIZE / 2, self.entity.y + TILE_SIZE / 2) then
|
||||
self.entity.x = self.entity.x + dx
|
||||
Event.dispatch('on-edge')
|
||||
else
|
||||
if self.entity.level:insideBounds(self.entity.x + TILE_SIZE / 2 + dx, self.entity.y + TILE_SIZE / 2) then
|
||||
self.entity:changeState('walk')
|
||||
Event.dispatch('walking')
|
||||
self.entity.x = self.entity.x + dx
|
||||
end
|
||||
end
|
||||
elseif self.entity.direction == 'up' then
|
||||
if self.entity.level:pointOnEdge(self.entity.x + TILE_SIZE / 2, self.entity.y - dy + TILE_SIZE / 2) then
|
||||
self.entity.y = self.entity.y - dy
|
||||
Event.dispatch('on-edge')
|
||||
else
|
||||
if self.entity.level:insideBounds(self.entity.x + TILE_SIZE / 2, self.entity.y + TILE_SIZE / 2 - dy) then
|
||||
self.entity:changeState('walk')
|
||||
Event.dispatch('walking')
|
||||
self.entity.y = self.entity.y - dy
|
||||
end
|
||||
end
|
||||
elseif self.entity.direction == 'down' then
|
||||
if self.entity.level:pointOnEdge(self.entity.x + TILE_SIZE / 2, self.entity.y + dy + TILE_SIZE / 2) then
|
||||
self.entity.y = self.entity.y + dy
|
||||
Event.dispatch('on-edge')
|
||||
else
|
||||
if self.entity.level:insideBounds(self.entity.x + TILE_SIZE / 2, self.entity.y + TILE_SIZE / 2 + dy) then
|
||||
self.entity:changeState('walk')
|
||||
Event.dispatch('walking')
|
||||
self.entity.y = self.entity.y + dy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if love.keyboard.isDown('left') then
|
||||
if self.entity.direction ~= 'left' then
|
||||
self.entity.previousDirection = self.entity.direction
|
||||
end
|
||||
self.entity.direction = 'left'
|
||||
self.entity:changeAnimation('walk-left')
|
||||
elseif love.keyboard.isDown('right') then
|
||||
if self.entity.direction ~= 'right' then
|
||||
self.entity.previousDirection = self.entity.direction
|
||||
end
|
||||
self.entity.direction = 'right'
|
||||
self.entity:changeAnimation('walk-right')
|
||||
elseif love.keyboard.isDown('up') then
|
||||
if self.entity.direction ~= 'up' then
|
||||
self.entity.previousDirection = self.entity.direction
|
||||
end
|
||||
self.entity.direction = 'up'
|
||||
self.entity:changeAnimation('walk-up')
|
||||
elseif love.keyboard.isDown('down') then
|
||||
if self.entity.direction ~= 'down' then
|
||||
self.entity.previousDirection = self.entity.direction
|
||||
end
|
||||
self.entity.direction = 'down'
|
||||
self.entity:changeAnimation('walk-down')
|
||||
else
|
||||
self.entity:changeState('idle')
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
function EntityWalkEdgeState:render()
|
||||
local anim = self.entity.currentAnimation
|
||||
love.graphics.draw(gTextures[anim.texture], gFrames[anim.texture][anim:getCurrentFrame()],self.entity.x, self.entity.y)
|
||||
|
||||
-- love.graphics.setColor(255, 0, 255, 255)
|
||||
-- love.graphics.rectangle('line', self.entity.x, self.entity.y, self.entity.width, self.entity.height)
|
||||
-- love.graphics.setColor(255, 255, 255, 255)
|
||||
end
|
||||
93
src/states/entity/EntityWalkState.lua
Normal file
93
src/states/entity/EntityWalkState.lua
Normal file
@@ -0,0 +1,93 @@
|
||||
-- require('mobdebug').start()
|
||||
|
||||
|
||||
EntityWalkState = Class{__includes = BaseState}
|
||||
|
||||
function EntityWalkState:init(entity)
|
||||
self.entity = entity
|
||||
self.entity:changeAnimation('walk-'..self.entity.direction)
|
||||
end
|
||||
|
||||
function EntityWalkState:update(dt)
|
||||
if self.entity.direction == 'up' then
|
||||
self.entity.y = self.entity.y - self.entity.walkingSpeed * dt
|
||||
if self.entity.y + TILE_SIZE / 2 < LEVEL_RENDER_OFFSET_TOP - 2 then
|
||||
self.entity.y = self.entity.y + self.entity.walkingSpeed * dt
|
||||
Event.dispatch('back-to-wall')
|
||||
self.entity:changeState('edge')
|
||||
end
|
||||
elseif self.entity.direction == 'down' then
|
||||
self.entity.y = self.entity.y + self.entity.walkingSpeed * dt
|
||||
if self.entity.y + TILE_SIZE / 2 > VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET + 2 then
|
||||
self.entity.y = self.entity.y - self.entity.walkingSpeed * dt
|
||||
Event.dispatch('back-to-wall')
|
||||
self.entity:changeState('edge')
|
||||
end
|
||||
elseif self.entity.direction == 'right' then
|
||||
self.entity.x = self.entity.x + self.entity.walkingSpeed * dt
|
||||
if self.entity.x + TILE_SIZE / 2 > VIRTUAL_WIDTH - TILE_SIZE / 2 + 2 or self.entity.previousdirection == 'left' then
|
||||
self.entity.x = self.entity.x - self.entity.walkingSpeed * dt
|
||||
Event.dispatch('back-to-wall')
|
||||
self.entity:changeState('edge')
|
||||
end
|
||||
elseif self.entity.direction == 'left' then
|
||||
self.entity.x = self.entity.x - self.entity.walkingSpeed * dt
|
||||
if self.entity.x < 0 or self.entity.previousdirection == 'right' then
|
||||
self.entity.x = self.entity.x + self.entity.walkingSpeed * dt
|
||||
Event.dispatch('back-to-wall')
|
||||
self.entity:changeState('edge')
|
||||
end
|
||||
end
|
||||
if love.keyboard.isDown('left') then
|
||||
if self.entity.direction ~= 'left' then
|
||||
self.entity.previousDirection = self.entity.direction
|
||||
Event.dispatch('change-direction')
|
||||
else
|
||||
Event.dispatch('walking')
|
||||
end
|
||||
self.entity.direction = 'left'
|
||||
self.entity:changeAnimation('walk-left')
|
||||
elseif love.keyboard.isDown('right') then
|
||||
if self.entity.direction ~= 'right' then
|
||||
self.entity.previousDirection = self.entity.direction
|
||||
Event.dispatch('change-direction')
|
||||
else
|
||||
Event.dispatch('walking')
|
||||
end
|
||||
self.entity.direction = 'right'
|
||||
self.entity:changeAnimation('walk-right')
|
||||
elseif love.keyboard.isDown('up') then
|
||||
if self.entity.direction ~= 'up' then
|
||||
self.entity.previousDirection = self.entity.direction
|
||||
Event.dispatch('change-direction')
|
||||
else
|
||||
Event.dispatch('walking')
|
||||
end
|
||||
self.entity.direction = 'up'
|
||||
self.entity:changeAnimation('walk-up')
|
||||
elseif love.keyboard.isDown('down') then
|
||||
if self.entity.direction ~= 'down' then
|
||||
self.entity.previousDirection = self.entity.direction
|
||||
Event.dispatch('change-direction')
|
||||
else
|
||||
Event.dispatch('walking')
|
||||
end
|
||||
self.entity.direction = 'down'
|
||||
self.entity:changeAnimation('walk-down')
|
||||
else
|
||||
self.entity:changeState('idle')
|
||||
end
|
||||
if not self.entity:insideBounds() then
|
||||
Event.dispatch('back-to-wall')
|
||||
self.entity:changeState('edge')
|
||||
end
|
||||
end
|
||||
|
||||
function EntityWalkState:render()
|
||||
local anim = self.entity.currentAnimation
|
||||
love.graphics.draw(gTextures[anim.texture], gFrames[anim.texture][anim:getCurrentFrame()],self.entity.x, self.entity.y)
|
||||
|
||||
-- love.graphics.setColor(255, 0, 255, 255)
|
||||
-- love.graphics.rectangle('line', self.entity.x, self.entity.y, self.entity.width, self.entity.height)
|
||||
-- love.graphics.setColor(255, 255, 255, 255)
|
||||
end
|
||||
Reference in New Issue
Block a user