47 lines
1.4 KiB
Lua
47 lines
1.4 KiB
Lua
-- scripts/enemy_rect.lua
|
|
-- RectNode: bounces around, changes color on bounce.
|
|
-- Try editing vx/vy or the color while the engine is running!
|
|
|
|
local vx, vy = 180, 130
|
|
|
|
---@param self RectNode
|
|
function on_init(self)
|
|
self.x = 600
|
|
self.y = 300
|
|
-- self.color already set by engine (passed in addRectNode),
|
|
-- but we can override it here.
|
|
self.color = { r = 220, g = 60, b = 60, a = 255 }
|
|
end
|
|
|
|
---@param self RectNode
|
|
---@param dt number
|
|
function on_update(self, dt)
|
|
self.x = self.x + vx * dt
|
|
self.y = self.y + vy * dt
|
|
|
|
if self.x < 0 or self.x + self.width > 1030 then
|
|
vx = -vx
|
|
self.x = math.max(0, math.min(1030 - self.width, self.x))
|
|
-- Flash yellow on bounce
|
|
self.color = { r = 240, g = 220, b = 50, a = 255 }
|
|
end
|
|
if self.y < 0 or self.y + self.height > 720 then
|
|
vy = -vy
|
|
self.y = math.max(0, math.min(720 - self.height, self.y))
|
|
self.color = { r = 240, g = 220, b = 50, a = 255 }
|
|
end
|
|
|
|
-- Fade back to red
|
|
self.color.r = math.min(220, self.color.r + 180 * dt)
|
|
self.color.g = math.max(60, self.color.g - 300 * dt)
|
|
self.color.b = math.max(60, self.color.b - 200 * dt)
|
|
end
|
|
|
|
---@param self RectNode
|
|
function on_render(self)
|
|
-- Label over auto-drawn rect
|
|
draw_text("Enemy", self.x + 4, self.y + 18, 13, 255, 210, 210)
|
|
local spd = math.floor(math.sqrt(vx * vx + vy * vy))
|
|
draw_text("v=" .. spd, self.x + 4, self.y + 34, 10, 200, 200, 200)
|
|
end
|