diff --git a/init.lua b/init.lua index 70072c5..72614b6 100644 --- a/init.lua +++ b/init.lua @@ -1,9 +1,9 @@ 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") --- inline stubs (extract in P.0.lib.{input,player_control,actor,interaction}) -local input = { hello = function() return "input stub" end } +-- 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 } @@ -11,19 +11,26 @@ local interaction = { hello = function() return "interaction stub" end } local demo_map = maps.load("maps/demo.map.json") maps.set_current(demo_map) --- Center camera on map center -local m_size = maps.size() -local t_size = maps.tile_size() +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) 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 }) + +local PAN_SPEED = 200 -- pixels per second + local frames = 0 local TARGET_FRAMES = 60 function init(ctx) engine.print("spine-prototype init: maps loaded") engine.print("inline stubs loaded: " - .. input.hello() .. ", " .. player_control.hello() .. ", " .. actor.hello() .. ", " .. interaction.hello()) @@ -37,6 +44,7 @@ function init(ctx) engine.print(string.format("render: %d tiles per frame", m_size.w * m_size.h)) 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())) end function render(ctx) @@ -47,6 +55,17 @@ function render(ctx) end function update(ctx, dt) + if input.was_action_pressed("quit") then + 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 + frames = frames + 1 if frames >= TARGET_FRAMES then engine.exit(0) diff --git a/manifest.module b/manifest.module index 82a6160..8082e51 100644 --- a/manifest.module +++ b/manifest.module @@ -7,6 +7,7 @@ "deps": [ {"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.camera","version":"0.1.0"}, + {"id":"lib-core.input","version":"0.1.0"} ] }