summaryrefslogtreecommitdiff
path: root/entity.lua
diff options
context:
space:
mode:
authorElizabeth Alexander Hunt <me@liz.coffee>2026-04-29 19:26:18 -0700
committerElizabeth Alexander Hunt <me@liz.coffee>2026-04-29 19:26:18 -0700
commit48491750e4ece19d2252592850b75d100afc2455 (patch)
tree54a1edb6f390053c0ecee47da7e6121a9a387839 /entity.lua
parent42aeb43e2c8a959f5ea1f9a33cfe63d667321c56 (diff)
downloaddyl8-48491750e4ece19d2252592850b75d100afc2455.tar.gz
dyl8-48491750e4ece19d2252592850b75d100afc2455.zip
The refactor from HELL
Diffstat (limited to 'entity.lua')
-rw-r--r--entity.lua170
1 files changed, 88 insertions, 82 deletions
diff --git a/entity.lua b/entity.lua
index 5cb52d8..449200e 100644
--- a/entity.lua
+++ b/entity.lua
@@ -3,7 +3,8 @@ Entities = {
Sword = 1,
Enemy = 2,
Bow = 3,
- Particle = 4
+ Particle = 4,
+ Wife = 5
}
States = {
Walk = "walk",
@@ -17,65 +18,6 @@ States = {
Entity = {}
Entity.__index = Entity
-function Entity:b_add_state(name, state)
- if self.states == nil then self.states = {} end
- self.states[name] = state
- if self.state == nil then self.state = name end
- return self
-end
-function Entity:b_state(name)
- assert(self.states[name] != nil)
- self.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
-end
-function Entity:b_position(vec)
- self.position = vec2(vec)
- return self
-end
-function Entity:b_sprite_position(vec)
- self.sprite_position = vec2(vec)
- return self
-end
-function Entity:b_type(entity_type)
- self.entity_type = entity_type
- return self
-end
-function Entity:b_equipped(equipped)
- self.equipped = equipped
- return self
-end
-function Entity:b_collidable()
- self.collision = true
- return self
-end
-function Entity:b_live_for(t)
- self.life_time = t
- return self
-end
-function Entity:b_line_of_sight(vec)
- self.line_of_sight = vec2(vec)
- return self
-end
-function Entity:build()
- assert(self.transition_state)
-
- assert(self.position)
- assert(self.state)
- assert(self.state_stopwatch)
-end
function Entity:is_in_iframe()
return self.knockback ~= nil and self.knockback.time >= 0
@@ -102,7 +44,9 @@ function Entity:update(dt)
self:update_sprite_position()
if (self.equipped != nil) then
parent = self
- foreach(self.equipped, function (e) e:equipped_from(parent) end)
+ for id, entity in pairs(self.equipped) do
+ entity:equipped_from(parent)
+ end
end
assert(self.transition_state)
@@ -118,8 +62,11 @@ end
function Entity:update_line_of_sight()
nv = self.velocity:apply(normalize_scalar)
- if nv.y != 0 then self.line_of_sight = vec2(0, nv.y)
- elseif nv.x != 0 then self.line_of_sight = vec2(nv.x, 0) end
+ if nv.y != 0 then
+ self.line_of_sight = vec2(0, nv.y)
+ elseif nv.x != 0 then
+ self.line_of_sight = vec2(nv.x, 0)
+ end
end
-- prevent cobblestoning during non-manhattan movement by "lagging" the sprite
@@ -195,11 +142,15 @@ function Entity:integrate(dt)
end
end
- if self.velocity ~= nil then
+ if self.velocity ~= nil and self.position ~= nil then
self.position = self.position + (self.velocity * dt)
end
end
+function Entity:equip(that)
+ self.equipped[that.id] = that
+end
+
_equipped_item_distance = 6
function Entity:equipped_from(parent, dist)
dist = dist or _equipped_item_distance
@@ -212,7 +163,7 @@ function Entity:equipped_from(parent, dist)
end
-- -1 0 1
-_animation_keys = {"neg", "pos", "pos"}
+_animation_keys = { "neg", "pos", "pos" }
function _get_animation_key(line_of_sight)
n_line_of_sight = line_of_sight:apply(normalize_scalar)
if n_line_of_sight.y ~= 0 then
@@ -226,12 +177,14 @@ function Entity:render()
animation = self.states[self.state].animation
if (animation == nil) then return end
- key = _get_animation_key(self.line_of_sight)
- animation = animation[key] or animation
+ if (self.line_of_sight ~= nil) then
+ key = _get_animation_key(self.line_of_sight)
+ animation = animation[key] or animation
+ end
assert(animation)
frames_passed = flr(self.state_stopwatch / animation.dt)
- frame = animation.sequence[1 + (frames_passed % (#animation.sequence))]
+ frame = animation.sequence[1 + (frames_passed % #animation.sequence)]
assert(frame)
reflection = animation.reflect or vec2(false, false)
@@ -243,20 +196,73 @@ function Entity:render()
)
end
-_id = 1
-function _next_id()
- i = _id
- _id += 1
- return i
-end
-function entity()
- id = _next_id()
- World.add(setmetatable(
+EntityBuilder = {}
+EntityBuilder.__index = EntityBuilder
+function EntityBuilder:new(world)
+ return setmetatable(
{
- id = id,
- velocity = vec2(0,0),
+ build_context = {
+ world = world
+ },
+ velocity = vec2(0, 0),
collision = false
- }, Entity
- ))
- return World.get(id)
+ }, EntityBuilder
+ )
+end
+function EntityBuilder:b_add_state(name, state)
+ if self.states == nil then self.states = {} end
+ self.states[name] = state
+ if self.state == nil then self.state = name end
+ return self
+end
+function EntityBuilder:b_state(name)
+ assert(self.states[name] != nil)
+ self.state = name
+ self.state_stopwatch = 0
+ return self
+end
+function EntityBuilder:b_health(health)
+ self.health = health
+ return self
+end
+function EntityBuilder:b_damage(damage_spec)
+ self.damage = damage_spec
+ return self
+end
+function EntityBuilder:b_render_order(ord)
+ self.render_order = ord
+ return self
end
+function EntityBuilder:b_position(vec)
+ self.position = vec2(vec)
+ return self
+end
+function EntityBuilder:b_sprite_position(vec)
+ self.sprite_position = vec2(vec)
+ return self
+end
+function EntityBuilder:b_type(entity_type)
+ self.entity_type = entity_type
+ return self
+end
+function EntityBuilder:b_equipped(equipped)
+ self.equipped = equipped
+ return self
+end
+function EntityBuilder:b_collidable()
+ self.collision = true
+ return self
+end
+function EntityBuilder:b_live_for(t)
+ self.life_time = t
+ return self
+end
+function EntityBuilder:b_line_of_sight(vec)
+ self.line_of_sight = vec2(vec)
+ return self
+end
+function EntityBuilder:build()
+ self.equipped = {}
+
+ return self.build_context.world.add(setmetatable(self, Entity))
+end \ No newline at end of file