diff --git a/game/engine/Actor.lua b/game/engine/Actor.lua index 6f4087d9bf80d74f075e1be64b3d6dca8c99078b..f15367941bcad43dedafde06d76ee22e61c0d195 100644 --- a/game/engine/Actor.lua +++ b/game/engine/Actor.lua @@ -11,6 +11,8 @@ function _M:init(t) end function _M:move(map, x, y) + if map:checkAllEntity(x, y, "block_move") then return end + if self.x and self.y then map:remove(self.x, self.y, Map.ACTOR) end diff --git a/game/engine/Entity.lua b/game/engine/Entity.lua index e5bae5244d7146a09b7c33dcc02fcea4f1ef516d..4d6e390b82f3679469fdac762ce9591d4d132cfc 100644 --- a/game/engine/Entity.lua +++ b/game/engine/Entity.lua @@ -6,7 +6,6 @@ local next_uid = 1 setmetatable(__uids, {__mode="v"}) function _M:init(t) - print("entity init") t = t or {} self.uid = next_uid __uids[self.uid] = self diff --git a/game/engine/Game.lua b/game/engine/Game.lua index de8cc58c59d1e42219327a12d8c6d1b97287b977..479b44cbd37ed9228c82d1e922f0f2f839d09d5c 100644 --- a/game/engine/Game.lua +++ b/game/engine/Game.lua @@ -9,3 +9,12 @@ end function _M:setLevel(level) self.level = level end + +function _M:setCurrent() + core.game.set_current_gametick(self) +end + +-- This is the "main game loop", do something here +function _M:tick() + +end diff --git a/game/engine/KeyCommand.lua b/game/engine/KeyCommand.lua index 0972f4cde10c0dc8a9852a679f05efe4c9700870..af8af25dd31839e805fd6b06e7dffdb768399c5c 100644 --- a/game/engine/KeyCommand.lua +++ b/game/engine/KeyCommand.lua @@ -25,9 +25,12 @@ function _M:receiveKey(sym, ctrl, shift, alt, meta, unicode) end function _M:addCommand(sym, mods, fct) + if type(sym) == "string" then sym = self[sym] end + if not sym then return end + self.commands[sym] = self.commands[sym] or {} - if not mods or #mods == 0 then - self.commands[sym].plain = fct + if not fct then + self.commands[sym].plain = mods else table.sort(mods) self.commands[sym][table.concat(mods,',')] = fct diff --git a/game/engine/Map.lua b/game/engine/Map.lua index b031a09e18f47ef3d1cab4e1fa5843d0f25fcba9..3784045d9742b2e40ac6b10a6ebd0b1fef38c0d7 100644 --- a/game/engine/Map.lua +++ b/game/engine/Map.lua @@ -19,7 +19,7 @@ function _M:init(w, h) setmetatable(self.seens, {__call = function(t, x, y, v) if v ~= nil then t[x + y * w] = v end return t[x + y * w] end}) setmetatable(self.remembers, {__call = function(t, x, y, v) if v ~= nil then t[x + y * w] = v end return t[x + y * w] end}) - self.surface = core.display.newSurface(400, 400) + self.surface = core.display.newSurface(w * 16, h * 16) self.fov = core.fov.new(_M.opaque, _M.apply, self) self.changed = true end @@ -34,7 +34,11 @@ function _M:call(x, y, pos, entity) self.changed = true else if self.map[x + y * self.w] then - return self.map[x + y * self.w][pos] + if not pos then + return self.map[x + y * self.w] + else + return self.map[x + y * self.w][pos] + end end end end @@ -47,10 +51,6 @@ function _M:remove(x, y, pos) end function _M:display() - self.fov(player.x, player.y, 20) - self.seens(player.x, player.y, true) - player:move(self, player.x+1, player.y) - -- If nothing changed, return the same surface as before if not self.changed then return self.surface end self.changed = false @@ -112,3 +112,12 @@ function _M:apply(x, y) self.seens[x + y * self.w] = true self.remembers[x + y * self.w] = true end + +function _M:checkAllEntity(x, y, what, ...) + if x < 0 or x >= self.w or y < 0 or y >= self.h then return end + if self.map[x + y * self.w] then + for _, e in pairs(self.map[x + y * self.w]) do + if e:check(what, x, y, ...) then return true end + end + end +end diff --git a/game/modules/tome/class/Actor.lua b/game/modules/tome/class/Actor.lua index 71a59cb60d983d83802e58c090d82d17a2f8155e..e7773253ae7aecb7ffea7ea0ae703e80336fc167 100644 --- a/game/modules/tome/class/Actor.lua +++ b/game/modules/tome/class/Actor.lua @@ -3,10 +3,13 @@ require "engine.Actor" module(..., package.seeall, class.inherit(engine.Actor)) -function _M:init(t) +function _M:init(game, t) + self.game = game engine.Actor.init(self, t) end -function _M:move(map, x, y) - engine.Actor.move(self, map, x, y) +function _M:move(x, y) + engine.Actor.move(self, self.game.level.map, x, y) + self.game.level.map.fov(self.x, self.y, 20) + self.game.level.map.seens(self.x, self.y, true) end diff --git a/game/modules/tome/class/Game.lua b/game/modules/tome/class/Game.lua new file mode 100644 index 0000000000000000000000000000000000000000..012d1b095cb623095f3ed16d9959e91bb0359bfd --- /dev/null +++ b/game/modules/tome/class/Game.lua @@ -0,0 +1,61 @@ +require "engine.class" +require "engine.Game" +require "engine.KeyCommand" +local Map = require "engine.Map" +local Level = require "engine.Level" +local Entity = require "engine.Entity" +local Actor = require "tome.class.Actor" + +module(..., package.seeall, class.inherit(engine.Game)) + +function _M:init() + engine.Game.init(self, engine.KeyCommand.new()) + self:setupCommands() + + local map = Map.new(40, 40) + local floor = Entity.new{display='#', color_r=100, color_g=100, color_b=100} + local e1 = Entity.new{display='#', color_r=255, block_sight=true} + local e2 = Entity.new{display='#', color_g=255, block_sight=true} + local e3 = Entity.new{display='#', color_b=255, block_sight=true, block_move=true} + local e4 = e3:clone{color_r=255} + + for i = 0, 39 do for j = 0, 39 do + map(i, j, 1, floor) + end end + + map(8, 6, Map.TERRAIN, e4) + map(8, 7, Map.TERRAIN, e2) + map(8, 8, Map.TERRAIN, e3) + map(9, 6, Map.TERRAIN, e1) + map(9, 7, Map.TERRAIN, e2) + map(9, 8, Map.TERRAIN, e3) + map(10, 6, Map.TERRAIN, e1) + map(10, 7, Map.TERRAIN, e2) + map(10, 8, Map.TERRAIN, e3) + + local level = Level.new(map) + level:activate() + self:setLevel(level) + + self.player = Actor.new(self, {name="player!", display='.', color_r=125, color_g=125, color_b=0}) + self.player:move(2, 3) +end + +function _M:tick() +end + +function _M:setupCommands() + self.key:addCommand("_LEFT", function() + self.player:move(self.player.x - 1, self.player.y) + end) + self.key:addCommand("_RIGHT", function() + self.player:move(self.player.x + 1, self.player.y) + end) + self.key:addCommand("_UP", function() + self.player:move(self.player.x, self.player.y - 1) + end) + self.key:addCommand("_DOWN", function() + self.player:move(self.player.x, self.player.y + 1) + end) + self.key:setCurrent() +end diff --git a/game/modules/tome/load.lua b/game/modules/tome/load.lua index 2f528908d73b28c8a15d326502ff7791812a4835..7c09e013437c37bb9cd529b83e8069464957c38a 100644 --- a/game/modules/tome/load.lua +++ b/game/modules/tome/load.lua @@ -1,30 +1,4 @@ -local Map = require "engine.Map" -local Entity = require "engine.Entity" -local Actor = require "tome.class.Actor" +local Game = require "tome.class.Game" -local map = Map.new(20, 20) - -local floor = Entity.new{display='#', color_r=100, color_g=100, color_b=100} -local e1 = Entity.new{display='#', color_r=255, block_sight=true} -local e2 = Entity.new{display='#', color_g=255, block_sight=true} -local e3 = Entity.new{display='#', color_b=255, block_sight=true} -local e4 = e3:clone{color_r=255} - -for i = 0, 19 do for j = 0, 19 do - map(i, j, 1, floor) -end end - -map(8, 6, Map.TERRAIN, e4) -map(8, 7, Map.TERRAIN, e2) -map(8, 8, Map.TERRAIN, e3) -map(9, 6, Map.TERRAIN, e1) -map(9, 7, Map.TERRAIN, e2) -map(9, 8, Map.TERRAIN, e3) -map(10, 6, Map.TERRAIN, e1) -map(10, 7, Map.TERRAIN, e2) -map(10, 8, Map.TERRAIN, e3) - -player = Actor.new{name="player!", display='.', color_r=125, color_g=125, color_b=0} -player:move(map, 2, 3) - -map:setCurrent() +local game = Game.new() +game:setCurrent() diff --git a/src/core_lua.c b/src/core_lua.c index 0bd4de32827e244c06e50b9c867c6c8c0bd403cb..04b4d7e3acb652a5bfc28ec4c9345f872316f6f5 100644 --- a/src/core_lua.c +++ b/src/core_lua.c @@ -131,6 +131,30 @@ static const struct luaL_reg keylib[] = {NULL, NULL}, }; +/****************************************************************** + ****************************************************************** + * Game * + ****************************************************************** + ******************************************************************/ +extern int current_gametick; +static int lua_set_current_gametick(lua_State *L) +{ + if (current_gametick != LUA_NOREF) + luaL_unref(L, LUA_REGISTRYINDEX, current_gametick); + + if (lua_isnil(L, 1)) + current_gametick = LUA_NOREF; + else + current_gametick = luaL_ref(L, LUA_REGISTRYINDEX); + + return 0; +} +static const struct luaL_reg gamelib[] = +{ + {"set_current_gametick", lua_set_current_gametick}, + {NULL, NULL}, +}; + /****************************************************************** ****************************************************************** * Display * @@ -226,5 +250,6 @@ int luaopen_core(lua_State *L) luaL_openlib(L, "core.fov", fovlib, 0); luaL_openlib(L, "core.display", displaylib, 0); luaL_openlib(L, "core.key", keylib, 0); + luaL_openlib(L, "core.game", gamelib, 0); return 1; } diff --git a/src/main.c b/src/main.c index 7857ea55d97881075fa7086b24f846bb24b4ed03..fd4a8347192f651b8a0ef044e72bad86472a42df 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,7 @@ lua_State *L = NULL; int current_map = LUA_NOREF; int current_keyhandler = LUA_NOREF; +int current_gametick = LUA_NOREF; int px = 1, py = 1; void display_utime() @@ -37,8 +38,8 @@ typedef struct { void on_event(SGEGAMESTATE *state, SDL_Event *event) { switch (event->type) { - case SDL_KEYUP: - if (current_keyhandler) + case SDL_KEYDOWN: + if (current_keyhandler != LUA_NOREF) { lua_rawgeti(L, LUA_REGISTRYINDEX, current_keyhandler); lua_pushstring(L, "receiveKey"); @@ -71,15 +72,16 @@ void on_redraw(SGEGAMESTATE *state) return; } - // draw a rectangle - // - // IMPORTANT: you should always lock and unlock surfaces if directly - // altering pixeldata, on some platforms, e.g. the gp2x, it will lead - // to a crash if you dont do so. - // - // you'll have to do so on most sgegfx.h functions. you do *NOT* need - // to lock a surface, if you blit on it (e.g. drawing sprites or using - // SDL_BlitSurface + if (current_gametick != LUA_NOREF) + { + lua_rawgeti(L, LUA_REGISTRYINDEX, current_gametick); + lua_pushstring(L, "tick"); + lua_gettable(L, -2); + lua_remove(L, -2); + lua_rawgeti(L, LUA_REGISTRYINDEX, current_gametick); + lua_call(L, 1, 0); + } + sgeLock(screen); SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00)); @@ -152,7 +154,8 @@ int run(int argc, char *argv[]) sgeInit(NOAUDIO, NOJOYSTICK); sgeOpenScreen("T-Engine", 800, 600, 32, NOFULLSCREEN); // sgeHideMouse(); -// SDL_EnableUNICODE(TRUE); + SDL_EnableUNICODE(TRUE); + SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); // add a new gamestate. you will usually have to add different gamestates // like 'main menu', 'game loop', 'load screen', etc. diff --git a/src/sge2d/src/sgegamestate.c b/src/sge2d/src/sgegamestate.c index 0ba58cdcf38a6cdeb5379221a2f8f2b0648ce13c..4a14dca3bdf48c49910d6615157ffb343531ddd3 100644 --- a/src/sge2d/src/sgegamestate.c +++ b/src/sge2d/src/sgegamestate.c @@ -51,9 +51,6 @@ void sgeGameStateManagerRun(SGEGAMESTATEMANAGER *manager, int fps) sgeEventResetInputs(&manager->event_state); break; case SDL_KEYDOWN: - case SDL_KEYUP: - case SDL_JOYBUTTONUP: - case SDL_JOYBUTTONDOWN: if (manager->current->onEvent) manager->current->onEvent(manager->current, &event); // sgeEventApply(&manager->event_state, &event);