Imported Upstream version 1.7.1
[platform/upstream/edje.git] / src / examples / embryo_pong.edc
1
2 #define DEBUG_ENABLE 0
3 #define FPS 30
4 #define COLOR_BG 50 50 50 255
5 #define COLOR_FG 222 222 222 255
6 #define PAD_SIZE 60
7 #define PAD_DISTANCE 20
8 #define BALL_SPEED 5.0
9 #define BALL_SPEEDUP 0.1
10 #define BALL_SIZE 10
11
12
13 collections {
14    group { name: "main";
15       min: 500 300;
16       script {
17          public ballx, bally;
18          public ballspeedx, ballspeedy;
19          public player_score, ai_score;
20
21          public game_reset() {
22             set_int(ai_score, 0);
23             set_int(player_score, 0);
24             set_text(PART:"score1", "0");
25             set_text(PART:"score2", "0");
26             game_init();
27          }
28          
29          public game_init() {
30             set_float(ballspeedx, 0.0);
31             set_float(ballspeedy, 0.0);
32             set_float(ballx, 100);
33             set_float(bally, 100);
34          }
35          
36          public start_game() {
37             set_float(ballspeedx, BALL_SPEED);
38             set_float(ballspeedy, BALL_SPEED * randf());
39          }
40
41          public player_wins() {
42             new buf[16]
43             set_int(player_score, get_int(player_score) + 1);
44             snprintf(buf, sizeof(buf), "%d", get_int(player_score));
45             set_text(PART:"score1", buf);
46             game_init();
47          }
48
49          public ai_wins() {
50             new buf[16]
51             set_int(ai_score, get_int(ai_score) + 1);
52             snprintf(buf, sizeof(buf), "%d", get_int(ai_score));
53             set_text(PART:"score2", buf);
54             game_init();
55          }
56
57          public game_loop(count) {
58             count++;
59
60             /* get field geometry */
61             new fx, fy, fw, fh;
62             get_geometry(PART:"bg", fx, fy, fw, fh);
63
64             /* get mouse coords */
65             new mx, my;
66             get_mouse(mx, my);
67
68             /* get the ball info */
69             new Float:bx = get_float(ballx);
70             new Float:by = get_float(bally);
71             new Float:speedx = get_float(ballspeedx);
72             new Float:speedy = get_float(ballspeedy);
73
74             /* move the player pad */
75             new pady = my - PAD_SIZE / 2;
76             if (pady < 0) pady = 0;
77             else if (pady + PAD_SIZE > fh) pady = fh - PAD_SIZE;
78             custom_state(PART:"pad1", "default", 0.0);
79             set_state_val(PART:"pad1", STATE_REL1_OFFSET, 20, pady);
80             set_state(PART:"pad1", "custom", 0.0);
81
82             /* move the AI pad */
83             new pad2y = round(by) - PAD_SIZE / 2 - BALL_SIZE / 2;
84             if (pad2y < 0) pad2y = 0;
85             else if (pad2y + PAD_SIZE > fh) pad2y = fh - PAD_SIZE;
86             custom_state(PART:"pad2", "default", 0.0);
87             set_state_val(PART:"pad2", STATE_REL1_OFFSET, 20, pad2y);
88             set_state(PART:"pad2", "custom", 0.0);
89          
90             /* calc new ball position */
91             bx += speedx;
92             by += speedy;
93
94             /* check walls collision */
95             if (by < 0)
96             {
97                speedy = -speedy;
98                by = 0;
99             }
100             else if (by + BALL_SIZE > fh)
101             {
102                speedy = -speedy;
103                by = fh - BALL_SIZE - 1;
104             }
105
106             /* check player pad collision */
107             if ((speedx < 0) &&
108                 (bx < PAD_DISTANCE + 10) && (bx > 0) &&
109                 (by + BALL_SIZE > pady) && (by < pady + PAD_SIZE))
110             {
111                new Float:dy = by - pady - PAD_SIZE / 2;
112                speedy += dy / 10;
113                speedx = -speedx + BALL_SPEEDUP;
114             }
115
116             /* check AI pad collision */
117             else if ((bx + BALL_SIZE > fw - PAD_DISTANCE - 10) &&
118                      (bx + BALL_SIZE < fw) &&
119                      (by + BALL_SIZE > pad2y) && (by < pad2y + PAD_SIZE))
120             {
121                new Float:dy = by - pad2y - PAD_SIZE / 2;
122                speedy += dy / 10;
123                speedx = -speedx - BALL_SPEEDUP;
124             }
125             
126             /* apply the new ball position */
127             custom_state(PART:"ball", "default", 0.0);
128             set_state_val(PART:"ball", STATE_REL1_OFFSET, round(bx), round(by));
129             set_state(PART:"ball", "custom", 0.0);
130
131             /* update global vars */
132             set_float(ballx, bx);
133             set_float(bally, by);
134             set_float(ballspeedx, speedx);
135             set_float(ballspeedy, speedy);
136
137             /* AI score a point */
138             if (bx < 0) ai_wins();
139             /* player score a point */
140             if (bx + BALL_SIZE > fw) player_wins();
141
142             /* show debug info (if debug enabled) */
143 #if DEBUG_ENABLE
144             new _buf[128];
145             snprintf(_buf, sizeof(_buf),
146                      "loop:%d  [speed %f  %f] [mouse: %d %d] [ball: %f %f]",
147                      count, speedx, speedy, mx, my, bx, by);
148             set_text(PART:"dbg", _buf);
149 #endif
150
151             /* recall the loop in n seconds */
152             timer(1.0 / FPS, "game_loop", count);
153          }
154       }
155       parts {
156          part { name: "bg";
157             type: RECT;
158             description { state: "default" 0.0;
159                color: COLOR_BG;
160             }
161          }
162          part { name: "net";
163             type: RECT;
164             description { state: "default" 0.0;
165                color: COLOR_FG;
166                max: 10 9999;
167             }
168          }
169          part { name: "score1";
170             type: TEXT;
171             description { state: "default" 0.0;
172                color: COLOR_FG;
173                rel2.relative: 0.5 0.5;
174                rel2.offset: -20 0;
175                text {
176                   text: "0";
177                   font: "Sans";
178                   size: 50;
179                   align: 1.0 0.0;
180                } 
181             }
182          }
183          part { name: "score2";
184             type: TEXT;
185             description { state: "default" 0.0;
186                color: COLOR_FG;
187                rel1.relative: 0.5 0.0;
188                rel1.offset: 20 0;
189                text {
190                   text: "0";
191                   font: "Sans";
192                   size: 50;
193                   align: 0.0 0.0;
194                } 
195             }
196          }
197          part { name: "pad1";
198             type: RECT;
199             description { state: "default" 0.0;
200                color: COLOR_FG;
201                max: 10 PAD_SIZE;
202                fixed: 1 1;
203                align: 0.0 0.0;
204                rel1.offset: PAD_DISTANCE 0;
205             }
206          }
207          part { name: "pad2";
208             type: RECT;
209             description { state: "default" 0.0;
210                color: COLOR_FG;
211                max: 10 PAD_SIZE;
212                fixed: 1 1;
213                align: 1.0 0.0;
214                rel2.offset: -PAD_DISTANCE 0;
215             }
216          }
217          part { name: "ball";
218             type: RECT;
219             description { state: "default" 0.0;
220                color: COLOR_FG;
221                max: BALL_SIZE BALL_SIZE;
222                fixed: 1 1;
223                align: 0.0 0.0;
224                rel1.offset: 100 100;
225             }
226          }
227          #if DEBUG_ENABLE
228          part { name: "dbg";
229             type: TEXT;
230             description { state: "default" 0.0;
231                color: 255 255 255 200;
232                text {
233                   font: "Sans";
234                   size: 12;
235                   align: 1.0 1.0;
236                }
237             }
238          }
239          #endif
240       }
241       programs {
242          /* on load: reset the game and start the game loop */
243          program {
244             signal: "load";
245             source: "";
246             script {
247                game_reset();
248                timer(0.1 , "game_loop", 0);
249             }
250          }
251          /* mouse left click: start the game, if not yet started */
252          program {
253             signal: "mouse,down,1";
254             source: "bg";
255             script {
256                if (get_float(ballspeedx) == 0.0)
257                   start_game();
258             }
259          }
260          /* mouse right click: restart the game*/
261          program {
262             signal: "mouse,down,3";
263             source: "bg";
264             script {
265                game_reset();
266             }
267          }
268       }
269    }
270 }