Skip to content
Snippets Groups Projects
Commit db23ba46 authored by dg's avatar dg
Browse files

block and fov

git-svn-id: http://svn.net-core.org/repos/t-engine4@7 51575b47-30f0-44d4-a5cc-537603b46e54
parent 466c09e2
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -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
......@@ -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
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
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()
......@@ -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;
}
......@@ -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.
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment