import solver
import mnit::tileset
import app::data_store
-import md5
intrude import grid
intrude import level
else if self.level.get_state >= l.l_open then
if game.levels[9].get_state >= l.l_open then self.over += " --- ?/{self.level.gold}"
end
- #self.enabled = l.get_state >= l.l_open
+ self.enabled = l.get_state >= l.l_open or game.cheated
end
redef fun draw(ctx)
do
print "***STATUS** {txt}"
self.tmp_txt = txt
- self.tmp_txt_ttl = 20
+ self.tmp_txt_ttl = 60
self.tmp_txt_color = color
end
# ResizeButton
var button_size = new ResizeButton(self)
+ # Cheat mode enabled?
+ var cheated = false
+
init
do
- load_levels
init_buttons
entities.clear
title
# Play a level in player mode.
fun play(l: Level)
do
+ save # save the previous level grid
level = l
- grid.load(level.str)
+ grid.load(level.saved_str or else level.str)
init_play_menu(false)
if level.status != "" then
statusbar.main_txt = level.status
# Helper function to initialize the menu (and tile) screen
fun init_menu
do
+ save # save the previous level grid
init_game
level = null
var i = levels.first
fun onKeyDown(ev: Event) do
var kc = ev.char_code
if kc == "e" then
+ set_tmp("RUN EDITOR")
grid_edit = grid.copy(true)
edit_grid(grid)
+ else if kc == "c" then
+ if cheated then
+ set_tmp("CHEAT: OFF")
+ snd_duh.play
+ cheated = false
+ else
+ set_tmp("CHEAT: ON")
+ snd_win.play
+ cheated = true
+ end
else if kc == "s" then
if solver == null then
solver = (new FriendzProblem(grid)).solve
else
solver_pause = not solver_pause
end
+ if solver_pause then
+ set_tmp("SOLVER: PAUSED")
+ else
+ set_tmp("SOLVER: ON")
+ end
#solver.step
else if kc == "d" then
if solver == null then
solver = (new FriendzProblem(grid)).solve
solver_pause = true
+ set_tmp("SOLVER: ON")
else
+ solver_pause = true
solver.run_steps(1)
+ set_tmp("SOLVER: ONE STEP")
end
else if kc == "+" then
solver_steps += 100
- print solver_steps
+ set_tmp("SOLVER: {solver_steps} STEPS")
else if kc == "-" then
solver_steps -= 100
- print solver_steps
+ set_tmp("SOLVER: {solver_steps} STEPS")
else for g in entities do
if kc == g.shortcut then
g.click(ev)
end
end
+ fun set_tmp(s: String)
+ do
+ statusbar.set_tmp(s, "cyan")
+ end
+
redef fun load_levels
do
super
for level in levels do
- var score = app.data_store["s{level.str.md5}"]
+ var score = app.data_store["s{level.str}"]
if score isa Int then
level.score = score
end
+ var saved_str = app.data_store["g{level.str}"]
+ if saved_str isa String then
+ print "LOAD {level.name}: {saved_str}"
+ level.saved_str = saved_str
+ end
+ end
+ end
+
+ fun save
+ do
+ var l = level
+ if l != null then
+ l.save
end
end
end
# img loading?
end
- # Maximum wanted frame per second
- var max_fps = 30
-
- # clock used to track FPS
- private var clock = new Clock
+ redef fun on_pause
+ do
+ super
+ game.save
+ end
redef fun frame_core(display)
do
game.step
game.draw(display)
- var dt = clock.lapse
- var target_dt = 1000000000 / max_fps
- if dt.sec == 0 and dt.nanosec < target_dt then
- var sleep_t = target_dt - dt.nanosec
- sys.nanosleep(0, sleep_t)
- end
end
redef fun input(input_event)
end
redef class Level
- redef fun save
+ # Save the score and grid of the level
+ fun save
do
- app.data_store["s{str.md5}"] = if score > 0 then score else null
+ app.data_store["s{str}"] = if score > 0 then score else null
+ var saved = game.grid.save
+ saved_str = saved
+ app.data_store["g{str}"] = saved
+ print "SAVE: {name}: {saved}"
end
+
+ # The saved player grid (to continue games)
+ var saved_str: nullable String = null
end