From db23ba46bf81ce445e89aca7f766319073123eb9 Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Wed, 18 Nov 2009 23:20:16 +0000
Subject: [PATCH] block and fov

git-svn-id: http://svn.net-core.org/repos/t-engine4@7 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engine/Actor.lua             |  2 +
 game/engine/Entity.lua            |  1 -
 game/engine/Game.lua              |  9 +++++
 game/engine/KeyCommand.lua        |  7 +++-
 game/engine/Map.lua               | 21 ++++++++---
 game/modules/tome/class/Actor.lua |  9 +++--
 game/modules/tome/class/Game.lua  | 61 +++++++++++++++++++++++++++++++
 game/modules/tome/load.lua        | 32 ++--------------
 src/core_lua.c                    | 25 +++++++++++++
 src/main.c                        | 27 ++++++++------
 src/sge2d/src/sgegamestate.c      |  3 --
 11 files changed, 141 insertions(+), 56 deletions(-)
 create mode 100644 game/modules/tome/class/Game.lua

diff --git a/game/engine/Actor.lua b/game/engine/Actor.lua
index 6f4087d9bf..f15367941b 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 e5bae5244d..4d6e390b82 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 de8cc58c59..479b44cbd3 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 0972f4cde1..af8af25dd3 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 b031a09e18..3784045d97 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 71a59cb60d..e7773253ae 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 0000000000..012d1b095c
--- /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 2f528908d7..7c09e01343 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 0bd4de3282..04b4d7e3ac 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 7857ea55d9..fd4a834719 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 0ba58cdcf3..4a14dca3bd 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);
-- 
GitLab