]> gitweb.factorcode.org Git - factor.git/blob - extra/tetris/tetris.factor
255144d4ec7db1377433079e591fabeb92d8a467
[factor.git] / extra / tetris / tetris.factor
1 ! Copyright (C) 2006, 2007, 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors timers arrays calendar kernel make math math.rectangles
4 math.parser namespaces sequences system tetris.game tetris.gl ui.gadgets
5 ui.gadgets.labels ui.gadgets.worlds ui.gadgets.status-bar ui.gestures
6 ui.render ui ;
7 IN: tetris
8
9 TUPLE: tetris-gadget < gadget { tetris tetris } { timer } ;
10
11 : <tetris-gadget> ( tetris -- gadget )
12     tetris-gadget new swap >>tetris ;
13
14 M: tetris-gadget pref-dim* drop { 200 400 } ;
15
16 : update-status ( gadget -- )
17     dup tetris>> [
18         [ "Level: " % level # ]
19         [ " Score: " % score>> # ]
20         [ paused?>> [ " (Paused)" % ] when ] tri
21     ] "" make swap show-status ;
22
23 M: tetris-gadget draw-gadget* ( gadget -- )
24     [
25         [ dim>> first2 ] [ tetris>> ] bi draw-tetris
26     ] keep update-status ;
27
28 : new-tetris ( gadget -- gadget )
29     [ <new-tetris> ] change-tetris ;
30
31 : unless-paused ( tetris quot -- )
32     over tetris>> paused?>> [
33         2drop
34     ] [
35         call
36     ] if ; inline
37
38 tetris-gadget H{
39     { T{ button-down f f 1 }     [ request-focus ] }
40     { T{ key-down f f "UP" }     [ [ tetris>> rotate-right ] unless-paused ] }
41     { T{ key-down f f "d" }      [ [ tetris>> rotate-left ] unless-paused ] }
42     { T{ key-down f f "f" }      [ [ tetris>> rotate-right ] unless-paused ] }
43     { T{ key-down f f "e" }      [ [ tetris>> rotate-left ] unless-paused ] }
44     { T{ key-down f f "u" }      [ [ tetris>> rotate-right ] unless-paused ] }
45     { T{ key-down f f "LEFT" }   [ [ tetris>> move-left ] unless-paused ] }
46     { T{ key-down f f "RIGHT" }  [ [ tetris>> move-right ] unless-paused ] }
47     { T{ key-down f f "DOWN" }   [ [ tetris>> move-down ] unless-paused ] }
48     { T{ key-down f f " " }      [ [ tetris>> move-drop ] unless-paused ] }
49     { T{ key-down f f "p" }      [ tetris>> toggle-pause ] }
50     { T{ key-down f f "n" }      [ new-tetris drop ] }
51 } set-gestures
52
53 : tick ( gadget -- )
54     [ tetris>> ?update ] [ relayout-1 ] bi ;
55
56 M: tetris-gadget graft* ( gadget -- )
57     [ [ tick ] curry 100 milliseconds every ] keep timer<< ;
58
59 M: tetris-gadget ungraft* ( gadget -- )
60     [ stop-timer f ] change-timer drop ;
61
62 : tetris-window ( -- )
63     [
64         <default-tetris> <tetris-gadget>
65         "Tetris" open-status-window
66     ] with-ui ;
67
68 MAIN: tetris-window