Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
The life view is now a menu attached to the game.
Browse files Browse the repository at this point in the history
It means that its callbacks on_draw, on_key_pressed, on_update, etc. are
now called automatically.
Other HUD elements will do the same.
  • Loading branch information
christopho committed Sep 8, 2012
1 parent d3c7447 commit 3871085
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
18 changes: 17 additions & 1 deletion quests/zsdx/data/hud/hearts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function hearts:initialize(game)
self.game = game
self.surface = sol.surface.create(90, 18)
self.surface:set_transparency_color{0, 0, 0}
self.dst_x = 0
self.dst_y = 0
self.empty_heart_sprite = sol.sprite.create("hud/empty_heart")
self.nb_max_hearts_displayed = game:get_max_life() / 4
self.nb_current_hearts_displayed = game:get_life()
Expand Down Expand Up @@ -132,7 +134,21 @@ function hearts:rebuild_surface()
end
end

function hearts:draw(dst_surface, x, y)
function hearts:set_dst_position(x, y)
self.dst_x = x
self.dst_y = y
end

function hearts:on_draw(dst_surface)

local x, y = self.dst_x, self.dst_y
local width, height = dst_surface:get_size()
if x < 0 then
x = width + x
end
if y < 0 then
y = height + y
end

-- Everything was already drawn on self.surface.
self.surface:draw(dst_surface, x, y)
Expand Down
20 changes: 11 additions & 9 deletions quests/zsdx/data/play_game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ function game:on_started()
-- Set up the HUD.
self.hud = {}
self.hud.hearts = hearts_class:new(self)
self.hud.hearts:set_dst_position(-104, 6)
self:set_hud_enabled(true)
end

function game:on_post_draw(surface)
-- Draw the hud after the game is drawn.
if game:is_hud_enabled() then
local width, height = surface:get_size()
self.hud.hearts:draw(surface, width - 104, 6)
end
end

-- Useful functions for this quest.

function game:is_hud_enabled()
return self.hud_enabled
end

function game:set_hud_enabled(hud_enabled)
game.hud_enabled = hud_enabled

if hud_enabled ~= game.hud_enabled then
game.hud_enabled = hud_enabled

if hud_enabled then
sol.menu.start(self, self.hud.hearts)
else
sol.menu.stop(self.hud.hearts)
end
end
end

-- Returns the item name of a bottle with the specified content, or nil.
Expand Down
3 changes: 2 additions & 1 deletion quests/zsdx/data/screens/savegames.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ function savegame_menu:draw_savegame(slot_index)
slot.player_name_text:draw(self.surface, 87, 61 + slot_index * 27)

if slot.hearts_view ~= nil then
slot.hearts_view:draw(self.surface, 168, 51 + slot_index * 27)
slot.hearts_view:set_dst_position(168, 51 + slot_index * 27)
slot.hearts_view:on_draw(self.surface)
end
end

Expand Down

0 comments on commit 3871085

Please sign in to comment.