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

tiles tooltips ...

git-svn-id: http://svn.net-core.org/repos/t-engine4@9 51575b47-30f0-44d4-a5cc-537603b46e54
parent 6b2573f9
No related branches found
No related tags found
No related merge requests found
File added
......@@ -14,6 +14,9 @@ function _M:init(t)
self.color_r = t.color_r or 0
self.color_g = t.color_g or 0
self.color_b = t.color_b or 0
self.color_br = t.color_br or -1
self.color_bg = t.color_bg or -1
self.color_bb = t.color_bb or -1
self.block_sight = t.block_sight
self.block_move = t.block_move
......
......@@ -2,10 +2,12 @@ require "engine.class"
module(..., package.seeall, class.make)
function _M:init(w, h, max, fontname, fontsize, color)
function _M:init(w, h, max, fontname, fontsize, color, bgcolor)
self.color = color or {255,255,255}
self.bgcolor = bgcolor or {0,0,0}
self.w, self.h = w, h
self.font = core.display.newFont(fontname or "/data/font/Vera.ttf", fontsize or 10)
self.font_h = self.font:lineSkip()
self.surface = core.display.newSurface(w, h)
self.log = {}
getmetatable(self).__call = _M.call
......@@ -32,11 +34,11 @@ function _M:display()
self.changed = false
-- Erase and the display the map
self.surface:erase()
self.surface:erase(self.bgcolor[1], self.bgcolor[2], self.bgcolor[3])
local i = 1
while i < self.h do
if not self.log[i] then break end
self.surface:drawString(self.font, self.log[i], 0, (i-1) * 16, self.color[1], self.color[2], self.color[3])
self.surface:drawString(self.font, self.log[i], 0, (i-1) * self.font_h, self.color[1], self.color[2], self.color[3])
i = i + 1
end
return self.surface
......
require "engine.class"
local Entity = require "engine.Entity"
local Tiles = require "engine.Tiles"
module(..., package.seeall, class.make)
......@@ -8,19 +9,24 @@ OBJECT = 10
ACTOR = 20
displayOrder = { ACTOR, OBJECT, TERRAIN }
rememberDisplayOrder = { TERRAIN }
function _M:init(w, h)
self.tiles = Tiles.new(16, 16)
self.w, self.h = w, h
self.map = {}
self.lites = {}
self.seens = {}
self.remembers = {}
for i = 0, w * h - 1 do self.map[i] = {} end
getmetatable(self).__call = _M.call
setmetatable(self.lites, {__call = function(t, x, y, v) if v ~= nil then t[x + y * w] = v end return t[x + y * w] end})
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(w * 16, h * 16)
self.fov = core.fov.new(_M.opaque, _M.apply, self)
self.fov_lite = core.fov.new(_M.opaque, _M.applyLite, self)
self.changed = true
end
......@@ -55,32 +61,41 @@ function _M:display()
self.surface:erase()
if not self.multi_display then
-- Version without multi display
local e, si
local z
local order
for i = 0, self.w - 1 do for j = 0, self.h - 1 do
local e, si = nil, 1
while not e and si <= #displayOrder do e = self(i, j, displayOrder[si]) si = si + 1 end
local z = i + j * self.w
if e then
if self.seens[z] then
self.surface:putChar(e.display, i, j, e.color_r, e.color_g, e.color_b)
elseif self.remembers[z] then
self.surface:putChar(e.display, i, j, e.color_r/3, e.color_g/3, e.color_b/3)
e, si = nil, 1
z = i + j * self.w
order = displayOrder
if self.seens[z] or self.remembers[z] then
if not self.seens[z] then order = rememberDisplayOrder end
while not e and si <= #order do e = self(i, j, order[si]) si = si + 1 end
if e then
if self.seens[z] then
self.surface:merge(self.tiles:get(e.display, e.color_r, e.color_g, e.color_b, e.color_br, e.color_bg, e.color_bb), i * 16, j * 16)
elseif self.remembers[z] then
self.surface:merge(self.tiles:get(e.display, e.color_r/3, e.color_g/3, e.color_b/3, e.color_br/3, e.color_bg/3, e.color_bb/3), i * 16, j * 16)
end
end
end
self.seens[z] = nil
end end
else
-- Version with multi display
local e, si = nil, 1
--[[
local z, e, si = nil, nil, 1
for i = 0, self.w - 1 do for j = 0, self.h - 1 do
z = i + j * self.w
z, e, si = 0, nil, #displayOrder
while si >= 1 do
e = self(i, j, displayOrder[si])
z = i + j * self.w
if e then
if self.seens[z] then
self.surface:putChar(e.display, i, j, e.color_r, e.color_g, e.color_b)
elseif self.remembers[z] then
self.surface:putChar(e.display, i, j, e.color_r/3, e.color_g/3, e.color_b/3)
self.surface:merge(self.tiles:get(e.display, e.color_r, e.color_g, e.color_b, e.color_br, e.color_bg, e.color_bb), i * 16, j * 16)
elseif self.remembers[z] then
self.surface:merge(self.tiles:get(e.display, e.color_r/3, e.color_g/3, e.color_b/3, e.color_br/3, e.color_bg/3, e.color_bb/3), i * 16, j * 16)
end
end
......@@ -88,11 +103,15 @@ function _M:display()
end
self.seens[z] = nil
end end
]]
end
return self.surface
end
function _M:close()
self.tiles:close()
self.fovLite:close()
self.fovLite = nil
self.fov:close()
self.fov = nil
end
......@@ -105,6 +124,15 @@ end
function _M:apply(x, y)
if x < 0 or x >= self.w or y < 0 or y >= self.h then return end
if self.lites[x + y * self.w] then
self.seens[x + y * self.w] = true
self.remembers[x + y * self.w] = true
end
end
function _M:applyLite(x, y)
if x < 0 or x >= self.w or y < 0 or y >= self.h then return end
self.lites[x + y * self.w] = true
self.seens[x + y * self.w] = true
self.remembers[x + y * self.w] = true
end
......@@ -113,7 +141,14 @@ 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
local p = e:check(what, x, y, ...)
if p then return p end
end
end
end
function _M:liteAll(x, y, w, h)
for i = x, x + w - 1 do for j = y, y + h - 1 do
self.lites(i, j, true)
end end
end
require "engine.class"
module(..., package.seeall, class.make)
function _M:init(w, h, fontname, fontsize)
self.w, self.h = w, h
self.font = core.display.newFont(fontname or "/data/font/VeraMono.ttf", fontsize or 14)
self.repo = {}
end
function _M:get(char, fr, fg, fb, br, bg, bb)
local fgidx = 65536 * fr + 256 + fg + fb
local bgidx
if br >= 0 and bg >= 0 and bb >= 0 then
bgidx = 65536 * br + 256 + bg + bb
else
bgidx = "none"
end
if self.repo[char] and self.repo[char][fgidx] and self.repo[char][fgidx][bgidx] then
return self.repo[char][fgidx][bgidx]
else
local s = core.display.newSurface(self.w, self.h)
if br >= 0 then
s:erase(br, bg, bb)
end
local w, h = self.font:size(char)
s:drawString(self.font, char, (self.w - w) / 2, (self.h - h) / 2, fr, fg, fb)
self.repo[char] = self.repo[char] or {}
self.repo[char][fgidx] = self.repo[char][fgidx] or {}
self.repo[char][fgidx][bgidx] = s
return s
end
end
require "engine.class"
module(..., package.seeall, class.make)
function _M:init(fontname, fontsize, color, bgcolor)
self.color = color or {255,255,255}
self.bgcolor = bgcolor or {0,0,0}
self.w, self.h = w, h
self.font = core.display.newFont(fontname or "/data/font/Vera.ttf", fontsize or 10)
self.font_h = self.font:lineSkip()
self.max = max or 400
self.changed = true
end
function _M:set(str, ...)
self.text = str:format(...)
self.changed = true
end
function _M:display()
-- If nothing changed, return the same surface as before
if not self.changed then return self.surface end
self.changed = false
local w, h = self.font:size(self.text)
self.surface = core.display.newSurface(w + 4, h + 4)
-- Erase and the display the map
self.surface:erase(self.bgcolor[1], self.bgcolor[2], self.bgcolor[3])
self.surface:drawString(self.font, self.text, 0, 0, self.color[1], self.color[2], self.color[3])
return self.surface
end
......@@ -19,6 +19,9 @@ function _M:move(x, y, force)
end
function _M:bumped(x, y, e)
self.game.log("%s bumped into %s!", tostring(e.name), tostring(self.name))
-- Dont bump yourself!
if e ~= self then
self.game.log("%s bumped into %s!", tostring(e.name), tostring(self.name))
end
return true
end
......@@ -2,6 +2,7 @@ require "engine.class"
require "engine.GameTurnBased"
require "engine.KeyCommand"
require "engine.LogDisplay"
local Tooltip = require "engine.Tooltip"
local Map = require "engine.Map"
local Level = require "engine.Level"
local Entity = require "engine.Entity"
......@@ -14,11 +15,15 @@ function _M:init()
engine.GameTurnBased.init(self, engine.KeyCommand.new(), 1000, 100)
self:setupCommands()
self.log = engine.LogDisplay.new(400, 150)
self.tooltip = engine.Tooltip.new(nil, nil, {255,255,255}, {30,30,30})
self.log = engine.LogDisplay.new(400, 150, nil, nil, nil, {255,255,255}, {30,30,30})
self.log("Welcome to Tales of Middle Earth!")
local map = Map.new(40, 20)
local floor = Entity.new{display='#', color_r=100, color_g=100, color_b=100}
-- map:liteAll(0, 0, map.w, map.h)
local floor = Entity.new{display='.', color_r=100, color_g=100, color_b=100, color_br=0, color_bg=50, color_bb=0}
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}
......@@ -41,11 +46,11 @@ function _M:init()
local level = Level.new(map)
self:setLevel(level)
self.player = Player.new(self, {name="player!", display='.', color_r=125, color_g=125, color_b=0})
self.player = Player.new(self, {name="player", display='@', color_r=230, color_g=230, color_b=230})
self.player:move(4, 3, true)
level:addEntity(self.player)
local m = NPC.new(self, {name="monster!", display='#', color_r=125, color_g=125, color_b=255})
local m = NPC.new(self, {name="monster", display='D', color_r=125, color_g=125, color_b=255})
m.energy.mod = 0.38
m:move(1, 3, true)
level:addEntity(m)
......@@ -59,11 +64,20 @@ function _M:tick()
end
function _M:display()
self.log:display():toScreen(0, 16 * 20 + 5)
if self.level and self.level.map then
local s = self.level.map:display()
if s then s:toScreen(0, 0) end
local mx, my = core.mouse.get()
local tt = self.level.map:checkAllEntity(math.floor(mx / 16), math.floor(my / 16), "tooltip")
if tt then
self.tooltip:set(tt)
local t = self.tooltip:display()
if t then t:toScreen(mx, my) end
end
end
self.log:display():toScreen(0, 16 * 20 + 5)
end
function _M:setupCommands()
......@@ -109,6 +123,11 @@ function _M:setupCommands()
self.paused = false
end
end,
_KP5 = function()
if self.player:move(self.player.x, self.player.y) then
self.paused = false
end
end,
_KP6 = function()
if self.player:move(self.player.x + 1, self.player.y) then
self.paused = false
......@@ -129,6 +148,12 @@ function _M:setupCommands()
self.paused = false
end
end,
[{"_x","ctrl"}] = function()
os.exit()
end,
[{"_RETURN","alt"}] = function()
core.display.fullscreen()
end,
}
self.key:setCurrent()
end
......@@ -5,12 +5,14 @@ module(..., package.seeall, class.inherit(tome.class.Actor))
function _M:init(game, t)
tome.class.Actor.init(self, game, t)
self.tooltip = "This is you!"
end
function _M:move(x, y, force)
local moved = tome.class.Actor.move(self, x, y, force)
if self.x and self.y then
self.game.level.map.fov(self.x, self.y, 20)
self.game.level.map.fov_lite(self.x, self.y, 4)
self.game.level.map.seens(self.x, self.y, true)
end
return moved
......
......@@ -108,6 +108,27 @@ static const struct luaL_reg fov_reg[] =
{NULL, NULL},
};
/******************************************************************
******************************************************************
* Mouse *
******************************************************************
******************************************************************/
static int lua_get_mouse(lua_State *L)
{
int x = 0, y = 0;
int buttons = SDL_GetMouseState(&x, &y);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
return 2;
}
static const struct luaL_reg mouselib[] =
{
{"get", lua_get_mouse},
{NULL, NULL},
};
/******************************************************************
******************************************************************
* Keys *
......@@ -161,6 +182,13 @@ static const struct luaL_reg gamelib[] =
* Display *
******************************************************************
******************************************************************/
static int sdl_fullscreen(lua_State *L)
{
SDL_WM_ToggleFullScreen(screen);
return 0;
}
static int sdl_new_font(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
......@@ -182,6 +210,35 @@ static int sdl_free_font(lua_State *L)
return 1;
}
static int sdl_font_size(lua_State *L)
{
TTF_Font **f = (TTF_Font**)auxiliar_checkclass(L, "sdl{font}", 1);
const char *str = luaL_checkstring(L, 2);
int w, h;
if (!TTF_SizeUTF8(*f, str, &w, &h))
{
lua_pushnumber(L, w);
lua_pushnumber(L, h);
return 2;
}
return 0;
}
static int sdl_font_height(lua_State *L)
{
TTF_Font **f = (TTF_Font**)auxiliar_checkclass(L, "sdl{font}", 1);
lua_pushnumber(L, TTF_FontHeight(*f));
return 1;
}
static int sdl_font_lineskip(lua_State *L)
{
TTF_Font **f = (TTF_Font**)auxiliar_checkclass(L, "sdl{font}", 1);
lua_pushnumber(L, TTF_FontLineSkip(*f));
return 1;
}
static int sdl_surface_drawstring(lua_State *L)
{
SDL_Surface **s = (SDL_Surface**)auxiliar_checkclass(L, "sdl{surface}", 1);
......@@ -198,7 +255,6 @@ static int sdl_surface_drawstring(lua_State *L)
if (txt)
{
sgeDrawImage(*s, txt, x, y);
SDL_FreeSurface(txt);
}
......@@ -222,7 +278,8 @@ static int sdl_new_surface(lua_State *L)
screen->format->Gmask,
screen->format->Bmask,
screen->format->Amask
);
);
sgeUseAlpha(*s);
return 1;
}
......@@ -253,7 +310,10 @@ static int lua_display_char(lua_State *L)
static int sdl_surface_erase(lua_State *L)
{
SDL_Surface **s = (SDL_Surface**)auxiliar_checkclass(L, "sdl{surface}", 1);
SDL_FillRect(*s, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
int r = lua_tonumber(L, 2);
int g = lua_tonumber(L, 3);
int b = lua_tonumber(L, 4);
SDL_FillRect(*s, NULL, SDL_MapRGB(screen->format, r, g, b));
return 0;
}
......@@ -269,8 +329,22 @@ static int sdl_surface_toscreen(lua_State *L)
return 0;
}
static int sdl_surface_merge(lua_State *L)
{
SDL_Surface **dst = (SDL_Surface**)auxiliar_checkclass(L, "sdl{surface}", 1);
SDL_Surface **src = (SDL_Surface**)auxiliar_checkclass(L, "sdl{surface}", 2);
int x = luaL_checknumber(L, 3);
int y = luaL_checknumber(L, 4);
if (dst && *dst && src && *src)
{
sgeDrawImage(*dst, *src, x, y);
}
return 0;
}
static const struct luaL_reg displaylib[] =
{
{"fullscreen", sdl_fullscreen},
{"newFont", sdl_new_font},
{"newSurface", sdl_new_surface},
{NULL, NULL},
......@@ -281,6 +355,7 @@ static const struct luaL_reg sdl_surface_reg[] =
{"__gc", sdl_free_surface},
{"close", sdl_free_surface},
{"erase", sdl_surface_erase},
{"merge", sdl_surface_merge},
{"toScreen", sdl_surface_toscreen},
{"putChar", lua_display_char},
{"drawString", sdl_surface_drawstring},
......@@ -291,6 +366,9 @@ static const struct luaL_reg sdl_font_reg[] =
{
{"__gc", sdl_free_font},
{"close", sdl_free_font},
{"size", sdl_font_size},
{"height", sdl_font_height},
{"lineSkip", sdl_font_lineskip},
{NULL, NULL},
};
......@@ -301,6 +379,7 @@ int luaopen_core(lua_State *L)
auxiliar_newclass(L, "sdl{font}", sdl_font_reg);
luaL_openlib(L, "core.fov", fovlib, 0);
luaL_openlib(L, "core.display", displaylib, 0);
luaL_openlib(L, "core.mouse", mouselib, 0);
luaL_openlib(L, "core.key", keylib, 0);
luaL_openlib(L, "core.game", gamelib, 0);
return 1;
......
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