Update the discussion of themeing in the manual, and put a note in the wps tags appen...
[kugel-rb.git] / apps / plugins / boomshine.lua
blobc160fddf0d4be5ee5bc7ca698faf04616b054f95
1 --[[
2 __________ __ ___.
3 Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 \/ \/ \/ \/ \/
8 $Id$
10 Port of Chain Reaction (which is based on Boomshine) to Rockbox in Lua.
11 See http://www.yvoschaap.com/chainrxn/ and http://www.k2xl.com/games/boomshine/
13 Copyright (C) 2009 by Maurus Cuelenaere
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License
17 as published by the Free Software Foundation; either version 2
18 of the License, or (at your option) any later version.
20 This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 KIND, either express or implied.
23 ]]--
25 require "actions"
27 local CYCLETIME = rb.HZ / 50
28 local HAS_TOUCHSCREEN = rb.action_get_touchscreen_press ~= nil
29 local DEFAULT_BALL_SIZE = rb.LCD_HEIGHT > rb.LCD_WIDTH and rb.LCD_WIDTH / 30
30 or rb.LCD_HEIGHT / 30
31 local MAX_BALL_SPEED = DEFAULT_BALL_SIZE / 2
32 local DEFAULT_FOREGROUND_COLOR = rb.lcd_get_foreground ~= nil
33 and rb.lcd_get_foreground()
34 or 0
36 local levels = {
37 -- {GOAL, TOTAL_BALLS},
38 {1, 5},
39 {2, 10},
40 {4, 15},
41 {6, 20},
42 {10, 25},
43 {15, 30},
44 {18, 35},
45 {22, 40},
46 {30, 45},
47 {37, 50},
48 {48, 55},
49 {55, 60}
52 local Ball = {
53 size = DEFAULT_BALL_SIZE,
54 exploded = false,
55 implosion = false
58 function Ball:new(o)
59 if o == nil then
60 o = {
61 x = math.random(0, rb.LCD_WIDTH - self.size),
62 y = math.random(0, rb.LCD_HEIGHT - self.size),
63 color = random_color(),
64 up_speed = Ball:generateSpeed(),
65 right_speed = Ball:generateSpeed(),
66 explosion_size = math.random(2*self.size, 4*self.size),
67 life_duration = math.random(rb.HZ, rb.HZ*5)
69 end
71 setmetatable(o, self)
72 self.__index = self
73 return o
74 end
76 function Ball:generateSpeed()
77 local speed = math.random(-MAX_BALL_SPEED, MAX_BALL_SPEED)
78 if speed == 0 then
79 speed = 1 -- Make sure all balls move
80 end
82 return speed
83 end
85 function Ball:draw()
86 --[[
87 I know these aren't circles, but as there's no current circle
88 implementation in Rockbox, rectangles will just do fine (drawing
89 circles from within Lua is far too slow).
90 ]]--
91 set_foreground(self.color)
92 rb.lcd_fillrect(self.x, self.y, self.size, self.size)
93 end
95 function Ball:step()
96 if self.exploded then
97 if self.implosion and self.size > 0 then
98 self.size = self.size - 2
99 self.x = self.x + 1 -- We do this because we want to stay centered
100 self.y = self.y + 1
101 elseif self.size < self.explosion_size then
102 self.size = self.size + 2
103 self.x = self.x - 1 -- We do this for the same reasons as above
104 self.y = self.y - 1
106 return
109 self.x = self.x + self.right_speed
110 self.y = self.y + self.up_speed
111 if (self.right_speed > 0 and self.x + self.size >= rb.LCD_WIDTH) or
112 (self.right_speed < 0 and self.x <= 0) then
113 self.right_speed = -self.right_speed
115 if (self.up_speed > 0 and self.y + self.size >= rb.LCD_HEIGHT) or
116 (self.up_speed < 0 and self.y <= 0) then
117 self.up_speed = -self.up_speed
121 function Ball:checkHit(other)
122 if (other.x + other.size >= self.x) and (self.x + self.size >= other.x) and
123 (other.y + other.size >= self.y) and (self.y + self.size >= other.y) then
124 assert(not self.exploded)
125 self.exploded = true
126 self.death_time = rb.current_tick() + self.life_duration
127 if not other.exploded then
128 other.exploded = true
129 other.death_time = rb.current_tick() + other.life_duration
131 return true
134 return false
137 local Cursor = {
138 size = DEFAULT_BALL_SIZE*2,
139 x = rb.LCD_WIDTH/2,
140 y = rb.LCD_HEIGHT/2
143 function Cursor:new()
144 return self
147 function Cursor:do_action(action)
148 if action == rb.actions.ACTION_TOUCHSCREEN and HAS_TOUCHSCREEN then
149 _, self.x, self.y = rb.action_get_touchscreen_press()
150 return true
151 elseif action == rb.actions.ACTION_KBD_SELECT then
152 return true
153 elseif (action == rb.actions.ACTION_KBD_RIGHT) then
154 self.x = self.x + self.size
155 elseif (action == rb.actions.ACTION_KBD_LEFT) then
156 self.x = self.x - self.size
157 elseif (action == rb.actions.ACTION_KBD_UP) then
158 self.y = self.y - self.size
159 elseif (action == rb.actions.ACTION_KBD_DOWN) then
160 self.y = self.y + self.size
163 if self.x > rb.LCD_WIDTH then
164 self.x = 0
165 elseif self.x < 0 then
166 self.x = rb.LCD_WIDTH
169 if self.y > rb.LCD_HEIGHT then
170 self.y = 0
171 elseif self.y < 0 then
172 self.y = rb.LCD_HEIGHT
175 return false
178 function Cursor:draw()
179 set_foreground(DEFAULT_FOREGROUND_COLOR)
181 rb.lcd_hline(self.x - self.size/2, self.x - self.size/4, self.y - self.size/2)
182 rb.lcd_hline(self.x + self.size/4, self.x + self.size/2, self.y - self.size/2)
183 rb.lcd_hline(self.x - self.size/2, self.x - self.size/4, self.y + self.size/2)
184 rb.lcd_hline(self.x + self.size/4, self.x + self.size/2, self.y + self.size/2)
185 rb.lcd_vline(self.x - self.size/2, self.y - self.size/2, self.y - self.size/4)
186 rb.lcd_vline(self.x - self.size/2, self.y + self.size/4, self.y + self.size/2)
187 rb.lcd_vline(self.x + self.size/2, self.y - self.size/2, self.y - self.size/4)
188 rb.lcd_vline(self.x + self.size/2, self.y + self.size/4, self.y + self.size/2)
190 rb.lcd_hline(self.x - self.size/4, self.x + self.size/4, self.y)
191 rb.lcd_vline(self.x, self.y - self.size/4, self.y + self.size/4)
194 function draw_positioned_string(bottom, right, str)
195 local _, w, h = rb.font_getstringsize(str, rb.FONT_UI)
197 rb.lcd_putsxy((rb.LCD_WIDTH-w)*right, (rb.LCD_HEIGHT-h)*bottom, str)
200 function set_foreground(color)
201 if rb.lcd_set_foreground ~= nil then
202 rb.lcd_set_foreground(color)
206 function random_color()
207 if rb.lcd_rgbpack ~= nil then --color target
208 return rb.lcd_rgbpack(math.random(1,255), math.random(1,255), math.random(1,255))
211 return math.random(1, rb.LCD_DEPTH)
214 function start_round(level, goal, nrBalls, total)
215 local player_added, score, exit, nrExpandedBalls = false, 0, false, 0
216 local balls, explodedBalls = {}, {}
217 local cursor = Cursor:new()
219 -- Initialize the balls
220 for _=1,nrBalls do
221 table.insert(balls, Ball:new())
224 -- Make sure there are no unwanted touchscreen presses
225 rb.button_clear_queue()
227 while true do
228 local endtick = rb.current_tick() + CYCLETIME
230 -- Check if the round is over
231 if #explodedBalls == 0 and player_added then
232 break
235 -- Check for actions
236 local action = rb.get_action(rb.contexts.CONTEXT_KEYBOARD, 0)
237 if(action == rb.actions.ACTION_KBD_ABORT) then
238 exit = true
239 break
241 if not player_added and cursor:do_action(action) then
242 local player = Ball:new({
243 x = cursor.x,
244 y = cursor.y,
245 color = DEFAULT_FOREGROUND_COLOR,
246 size = 10,
247 explosion_size = 3*DEFAULT_BALL_SIZE,
248 exploded = true,
249 death_time = rb.current_tick() + rb.HZ * 3
251 table.insert(explodedBalls, player)
252 player_added = true
255 -- Check for hits
256 for i, ball in ipairs(balls) do
257 for _, explodedBall in ipairs(explodedBalls) do
258 if ball:checkHit(explodedBall) then
259 score = score + 100*level
260 nrExpandedBalls = nrExpandedBalls + 1
261 table.insert(explodedBalls, ball)
262 table.remove(balls, i)
263 break
268 -- Check if we're dead yet
269 for i, explodedBall in ipairs(explodedBalls) do
270 if rb.current_tick() >= explodedBall.death_time then
271 if explodedBall.size > 0 then
272 explodedBall.implosion = true -- We should be dying
273 else
274 table.remove(explodedBalls, i) -- We're imploded!
279 -- Drawing phase
280 rb.lcd_clear_display()
282 set_foreground(DEFAULT_FOREGROUND_COLOR)
283 draw_positioned_string(0, 0, string.format("%d balls expanded", nrExpandedBalls))
284 draw_positioned_string(0, 1, string.format("Level %d", level))
285 draw_positioned_string(1, 1, string.format("%d level points", score))
286 draw_positioned_string(1, 0, string.format("%d total points", total+score))
288 for _, ball in ipairs(balls) do
289 ball:step()
290 ball:draw()
293 for _, explodedBall in ipairs(explodedBalls) do
294 explodedBall:step()
295 explodedBall:draw()
298 if not HAS_TOUCHSCREEN and not player_added then
299 cursor:draw()
302 -- Push framebuffer to the LCD
303 rb.lcd_update()
305 if rb.current_tick() < endtick then
306 rb.sleep(endtick - rb.current_tick())
307 else
308 rb.yield()
312 return exit, score, nrExpandedBalls
315 -- Helper function to display a message
316 function display_message(...)
317 local message = string.format(...)
318 local _, w, h = rb.font_getstringsize(message, rb.FONT_UI)
319 local x, y = (rb.LCD_WIDTH - w) / 2, (rb.LCD_HEIGHT - h) / 2
321 rb.lcd_clear_display()
322 set_foreground(DEFAULT_FOREGROUND_COLOR)
323 if w > rb.LCD_WIDTH then
324 rb.lcd_puts_scroll(x/w, y/h, message)
325 else
326 rb.lcd_putsxy(x, y, message)
328 rb.lcd_update()
330 rb.sleep(rb.HZ * 2)
332 rb.lcd_stop_scroll() -- Stop our scrolling message
335 if HAS_TOUCHSCREEN then
336 rb.touchscreen_set_mode(rb.TOUCHSCREEN_POINT)
338 rb.backlight_force_on()
340 local idx, highscore = 1, 0
341 while levels[idx] ~= nil do
342 local goal, nrBalls = levels[idx][1], levels[idx][2]
344 display_message("Level %d: get %d out of %d balls", idx, goal, nrBalls)
346 local exit, score, nrExpandedBalls = start_round(idx, goal, nrBalls, highscore)
347 if exit then
348 break -- Exiting..
349 else
350 if nrExpandedBalls >= goal then
351 display_message("You won!")
352 idx = idx + 1
353 highscore = highscore + score
354 else
355 display_message("You lost!")
360 if idx > #levels then
361 display_message("You finished the game with %d points!", highscore)
362 else
363 display_message("You made it till level %d with %d points!", idx, highscore)
366 -- Restore user backlight settings
367 rb.backlight_use_settings()