39 lines
1.0 KiB
Lua
39 lines
1.0 KiB
Lua
-- Called once on load
|
|
function OnInit()
|
|
player = Engine.GetObjectByTag("PlayerRoot")
|
|
if not player then
|
|
Engine.LogError("PlayerRoot not found!")
|
|
return
|
|
end
|
|
|
|
if Engine.GetGlobal("player_health") == nil then
|
|
Engine.SetGlobal("player_health", 100)
|
|
end
|
|
end
|
|
|
|
-- Called every frame
|
|
function OnUpdate(dt)
|
|
if not player then return end
|
|
|
|
local speed = 200
|
|
local move = { x = 0, y = 0 }
|
|
|
|
if Engine.KeyDown(Keycode.W) then move.y = move.y - 1 end
|
|
if Engine.KeyDown(Keycode.S) then move.y = move.y + 1 end
|
|
if Engine.KeyDown(Keycode.A) then move.x = move.x - 1 end
|
|
if Engine.KeyDown(Keycode.D) then move.x = move.x + 1 end
|
|
|
|
local pos = player:GetPosition()
|
|
pos.x = pos.x + move.x * speed * dt
|
|
pos.y = pos.y + move.y * speed * dt
|
|
player:SetPosition(pos)
|
|
|
|
-- Simulate damage each second
|
|
local hp = Engine.GetGlobal("player_health") or 100
|
|
hp = hp - dt * 5
|
|
Engine.SetGlobal("player_health", hp)
|
|
|
|
-- Debug print
|
|
Engine.LogDebug(string.format("HP: %.1f", hp))
|
|
end
|