diff options
Diffstat (limited to 'entity.lua')
| -rw-r--r-- | entity.lua | 45 |
1 files changed, 43 insertions, 2 deletions
@@ -29,6 +29,14 @@ function Entity:b_state(name) self.state_stopwatch = 0 return self end +function Entity:b_health(health) + self.health = health + return self +end +function Entity:b_damage(damage_spec) + self.damage = damage_spec + return self +end function Entity:b_render_order(ord) self.render_order = ord return self @@ -69,6 +77,10 @@ function Entity:build() assert(self.state_stopwatch) end +function Entity:is_in_iframe() + return self.knockback ~= nil and self.knockback.time >= 0 +end + function Entity:transition_to(state_name) assert(self.states[state_name]) if self.state ~= state_name then @@ -99,6 +111,9 @@ function Entity:update(dt) if self.life_time ~= nil then self.life_time -= dt end + if self.life ~= nil and self.life <= 0 then + self:kill() + end end function Entity:update_line_of_sight() @@ -109,10 +124,11 @@ end -- prevent cobblestoning during non-manhattan movement by "lagging" the sprite -- behind the actual physical position +-- this part was painful. function Entity:update_sprite_position() if self.sprite_position == nil then return end - -- step in only x or y. trivial + -- step in only x or y. trivial. if self.velocity == nil or self.velocity.y == 0 or self.velocity.x == 0 then self.sprite_position.x = self.position.x self.sprite_position.y = self.position.y @@ -152,9 +168,34 @@ function Entity:update_sprite_position() return self end +function Entity:take_damage(direction, damage_spec) + if self.health == nil then + return + end + self.health -= damage_spec.amount + if damage_spec.knockback ~= nil then + self.knockback = { + velocity = direction * damage_spec.knockback.magnitude, + remaining_time = damage_spec.knockback.time, + time = damage_spec.knockback.time + } + end +end + function Entity:integrate(dt) self.state_stopwatch += dt - if (self.velocity != nil) then + + if self.knockback ~= nil then + if self.knockback.remaining_time <= 0 then + self.knockback = nil + self.velocity = vec2(0, 0) + else + self.velocity = self.knockback.velocity * (self.knockback.remaining_time / self.knockback.time) + self.knockback.remaining_time -= dt + end + end + + if self.velocity ~= nil then self.position = self.position + (self.velocity * dt) end end |
