feat: P.0.lib.player_control integration — player rect + camera-follow + move_* actions
This commit is contained in:
58
init.lua
58
init.lua
@@ -2,38 +2,43 @@ local maps = require("lib-core.maps")
|
||||
local r_lib = require("lib-core.render")
|
||||
local camera = require("lib-core.camera")
|
||||
local input = require("lib-core.input")
|
||||
local player = require("lib-core.player_control")
|
||||
|
||||
-- inline stubs (extract in P.0.lib.{player_control,actor,interaction})
|
||||
local player_control = { hello = function() return "player_control stub" end }
|
||||
local actor = { hello = function() return "actor stub" end }
|
||||
local interaction = { hello = function() return "interaction stub" end }
|
||||
-- inline stubs (extract in P.0.lib.{actor,interaction})
|
||||
local actor = { hello = function() return "actor stub" end }
|
||||
local interaction = { hello = function() return "interaction stub" end }
|
||||
|
||||
local demo_map = maps.load("maps/demo.map.json")
|
||||
maps.set_current(demo_map)
|
||||
|
||||
local m_size = maps.size()
|
||||
local t_size = maps.tile_size()
|
||||
camera.set_target((m_size.w * t_size) / 2, (m_size.h * t_size) / 2)
|
||||
local map_center_x = (m_size.w * t_size) / 2
|
||||
local map_center_y = (m_size.h * t_size) / 2
|
||||
|
||||
camera.set_target(map_center_x, map_center_y)
|
||||
camera.set_zoom(1.0)
|
||||
|
||||
-- Bind input actions: ESC quits; WASD/arrows pan camera
|
||||
input.bind("quit", { engine.input.KEY_ESCAPE })
|
||||
input.bind("pan_left", { engine.input.KEY_A, engine.input.KEY_LEFT })
|
||||
input.bind("pan_right", { engine.input.KEY_D, engine.input.KEY_RIGHT })
|
||||
input.bind("pan_up", { engine.input.KEY_W, engine.input.KEY_UP })
|
||||
input.bind("pan_down", { engine.input.KEY_S, engine.input.KEY_DOWN })
|
||||
-- Bind input actions: ESC quits; WASD/arrows drive player movement
|
||||
input.bind("quit", { engine.input.KEY_ESCAPE })
|
||||
input.bind("move_left", { engine.input.KEY_A, engine.input.KEY_LEFT })
|
||||
input.bind("move_right", { engine.input.KEY_D, engine.input.KEY_RIGHT })
|
||||
input.bind("move_up", { engine.input.KEY_W, engine.input.KEY_UP })
|
||||
input.bind("move_down", { engine.input.KEY_S, engine.input.KEY_DOWN })
|
||||
|
||||
local PAN_SPEED = 200 -- pixels per second
|
||||
-- Configure player: defaults are 24x24 + 200 px/sec; place at map-center
|
||||
local p_size = player.size()
|
||||
player.set_position(map_center_x - p_size.w / 2, map_center_y - p_size.h / 2)
|
||||
player.bind_movement("move_left", "move_right", "move_up", "move_down")
|
||||
|
||||
local PLAYER_R, PLAYER_G, PLAYER_B = 200, 60, 80
|
||||
|
||||
local frames = 0
|
||||
local TARGET_FRAMES = 60
|
||||
|
||||
function init(ctx)
|
||||
engine.print("spine-prototype init: maps loaded")
|
||||
engine.print("inline stubs loaded: "
|
||||
.. player_control.hello() .. ", "
|
||||
.. actor.hello() .. ", "
|
||||
.. interaction.hello())
|
||||
engine.print("inline stubs loaded: " .. actor.hello() .. ", " .. interaction.hello())
|
||||
engine.print(string.format("map size: %dx%d", m_size.w, m_size.h))
|
||||
local corner = maps.tile_at(0, 0)
|
||||
local interior = maps.tile_at(5, 5)
|
||||
@@ -45,12 +50,21 @@ function init(ctx)
|
||||
local t = camera.target()
|
||||
engine.print(string.format("camera: target=(%d,%d) zoom=%.2f", t.x, t.y, camera.zoom()))
|
||||
engine.print(string.format("input: %d actions bound", input.action_count()))
|
||||
local p = player.position()
|
||||
local s = player.size()
|
||||
engine.print(string.format("player: pos=(%d,%d) size=%dx%d speed=%d",
|
||||
p.x, p.y, s.w, s.h, player.speed()))
|
||||
end
|
||||
|
||||
function render(ctx)
|
||||
engine.render.clear_color(engine.render.rgb(20, 20, 30))
|
||||
camera.begin()
|
||||
r_lib.draw_map()
|
||||
-- draw player on top of map
|
||||
local p = player.position()
|
||||
local s = player.size()
|
||||
engine.render.draw_rect(p.x, p.y, s.w, s.h,
|
||||
engine.render.rgb(PLAYER_R, PLAYER_G, PLAYER_B))
|
||||
camera.finish()
|
||||
end
|
||||
|
||||
@@ -59,12 +73,12 @@ function update(ctx, dt)
|
||||
engine.exit(0)
|
||||
end
|
||||
|
||||
local d = input.direction("pan_left", "pan_right", "pan_up", "pan_down")
|
||||
if d.x ~= 0 or d.y ~= 0 then
|
||||
local t = camera.target()
|
||||
camera.set_target(t.x + d.x * PAN_SPEED * dt,
|
||||
t.y + d.y * PAN_SPEED * dt)
|
||||
end
|
||||
player.update(dt)
|
||||
|
||||
-- Camera follows player center
|
||||
local p = player.position()
|
||||
local s = player.size()
|
||||
camera.set_target(p.x + s.w / 2, p.y + s.h / 2)
|
||||
|
||||
frames = frames + 1
|
||||
if frames >= TARGET_FRAMES then
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
{"id":"lib-core.maps","version":"0.1.0"},
|
||||
{"id":"lib-core.render","version":"0.1.0"},
|
||||
{"id":"lib-core.camera","version":"0.1.0"},
|
||||
{"id":"lib-core.input","version":"0.1.0"}
|
||||
{"id":"lib-core.input","version":"0.1.0"},
|
||||
{"id":"lib-core.player_control","version":"0.1.0"}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user