summaryrefslogtreecommitdiff
path: root/legacy/edje/doc/examples/embryo_pong.edc
blob: 863b5648a9abea0aacb0baae5feace4264bf4434 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273

#define DEBUG_ENABLE 0
#define FPS 30
#define COLOR_BG 50 50 50 255
#define COLOR_FG 222 222 222 255
#define PAD_SIZE 60
#define PAD_DISTANCE 20
#define BALL_SPEED 5.0
#define BALL_SPEEDUP 0.1
#define BALL_SIZE 10

#if DEBUG_ENABLE
#define DBG(...) { \
   new _buf[128]; \
   snprintf(_buf, sizeof(_buf), __VA_ARGS__); \
   set_text(PART:"dbg", _buf); }
#else
#define DBG(...)
#endif

collections {
   group { name: "main";
      min: 500 300;
      script {
         public ballx, bally;
         public ballspeedx, ballspeedy;
         public player_score, ai_score;

         public game_reset() {
            set_int(ai_score, 0);
            set_int(player_score, 0);
            set_text(PART:"score1", "0");
            set_text(PART:"score2", "0");
            game_init();
         }
         
         public game_init() {
            set_float(ballspeedx, 0.0);
            set_float(ballspeedy, 0.0);
            set_float(ballx, 100);
            set_float(bally, 100);
         }
         
         public start_game() {
            set_float(ballspeedx, BALL_SPEED);
            set_float(ballspeedy, BALL_SPEED * randf());
         }

         public player_wins() {
            new buf[16]
            set_int(player_score, get_int(player_score) + 1);
            snprintf(buf, sizeof(buf), "%d", get_int(player_score));
            set_text(PART:"score1", buf);
            game_init();
         }

         public ai_wins() {
            new buf[16]
            set_int(ai_score, get_int(ai_score) + 1);
            snprintf(buf, sizeof(buf), "%d", get_int(ai_score));
            set_text(PART:"score2", buf);
            game_init();
         }

         public game_loop(count) {
            count++;

            /* get field geometry */
            new fx, fy, fw, fh;
            get_geometry(PART:"bg", fx, fy, fw, fh);

            /* get mouse coords */
            new mx, my;
            get_mouse(mx, my);

            /* get the ball info */
            new Float:bx = get_float(ballx);
            new Float:by = get_float(bally);
            new Float:speedx = get_float(ballspeedx);
            new Float:speedy = get_float(ballspeedy);

            /* move the player pad */
            new pady = my - PAD_SIZE / 2;
            if (pady < 0) pady = 0;
            else if (pady + PAD_SIZE > fh) pady = fh - PAD_SIZE;
            custom_state(PART:"pad1", "default", 0.0);
            set_state_val(PART:"pad1", STATE_REL1_OFFSET, 20, pady);
            set_state(PART:"pad1", "custom", 0.0);

            /* move the AI pad */
            new pad2y = round(by) - PAD_SIZE / 2 - BALL_SIZE / 2;
            if (pad2y < 0) pad2y = 0;
            else if (pad2y + PAD_SIZE > fh) pad2y = fh - PAD_SIZE;
            custom_state(PART:"pad2", "default", 0.0);
            set_state_val(PART:"pad2", STATE_REL1_OFFSET, 20, pad2y);
            set_state(PART:"pad2", "custom", 0.0);
         
            /* calc new ball position */
            bx += speedx;
            by += speedy;

            /* check walls collision */
            if (by < 0)
            {
               speedy = -speedy;
               by = 0;
            }
            else if (by + BALL_SIZE > fh)
            {
               speedy = -speedy;
               by = fh - BALL_SIZE - 1;
            }

            /* check player pad collision */
            if ((speedx < 0) &&
                (bx < PAD_DISTANCE + 10) && (bx > 0) &&
                (by + BALL_SIZE > pady) && (by < pady + PAD_SIZE))
            {
               new Float:dy = by - pady - PAD_SIZE / 2;
               speedy += dy / 10;
               speedx = -speedx + BALL_SPEEDUP;
            }

            /* check AI pad collision */
            else if ((bx + BALL_SIZE > fw - PAD_DISTANCE - 10) &&
                     (bx + BALL_SIZE < fw) &&
                     (by + BALL_SIZE > pad2y) && (by < pad2y + PAD_SIZE))
            {
               new Float:dy = by - pad2y - PAD_SIZE / 2;
               speedy += dy / 10;
               speedx = -speedx - BALL_SPEEDUP;
            }
            
            /* apply the new ball position */
            custom_state(PART:"ball", "default", 0.0);
            set_state_val(PART:"ball", STATE_REL1_OFFSET, round(bx), round(by));
            set_state(PART:"ball", "custom", 0.0);

            /* update global vars */
            set_float(ballx, bx);
            set_float(bally, by);
            set_float(ballspeedx, speedx);
            set_float(ballspeedy, speedy);

            /* AI score a point */
            if (bx < 0) ai_wins();
            /* player score a point */
            if (bx + BALL_SIZE > fw) player_wins();

            /* show debug info (if debug enabled) */
            DBG("loop:%d  [speed %f  %f] [mouse: %d %d] [ball: %f %f]",
                count, speedx, speedy, mx, my, bx, by)

            /* recall the loop in n seconds */
            timer(1.0 / FPS, "game_loop", count);
         }
      }
      parts {
         part { name: "bg";
            type: RECT;
            description { state: "default" 0.0;
               color: COLOR_BG;
            }
         }
         part { name: "net";
            type: RECT;
            description { state: "default" 0.0;
               color: COLOR_FG;
               max: 10 9999;
            }
         }
         part { name: "score1";
            type: TEXT;
            description { state: "default" 0.0;
               color: COLOR_FG;
               rel2.relative: 0.5 0.5;
               rel2.offset: -20 0;
               text {
                  text: "0";
                  font: "Sans";
                  size: 50;
                  align: 1.0 0.0;
               } 
            }
         }
         part { name: "score2";
            type: TEXT;
            description { state: "default" 0.0;
               color: COLOR_FG;
               rel1.relative: 0.5 0.0;
               rel1.offset: 20 0;
               text {
                  text: "0";
                  font: "Sans";
                  size: 50;
                  align: 0.0 0.0;
               } 
            }
         }
         part { name: "pad1";
            type: RECT;
            description { state: "default" 0.0;
               color: COLOR_FG;
               max: 10 PAD_SIZE;
               fixed: 1 1;
               align: 0.0 0.0;
               rel1.offset: PAD_DISTANCE 0;
            }
         }
         part { name: "pad2";
            type: RECT;
            description { state: "default" 0.0;
               color: COLOR_FG;
               max: 10 PAD_SIZE;
               fixed: 1 1;
               align: 1.0 0.0;
               rel2.offset: -PAD_DISTANCE 0;
            }
         }
         part { name: "ball";
            type: RECT;
            description { state: "default" 0.0;
               color: COLOR_FG;
               max: BALL_SIZE BALL_SIZE;
               fixed: 1 1;
               align: 0.0 0.0;
               rel1.offset: 100 100;
            }
         }
         #if DEBUG_ENABLE
         part { name: "dbg";
            type: TEXT;
            description { state: "default" 0.0;
               color: 255 255 255 200;
               text {
                  font: "Sans";
                  size: 12;
                  align: 1.0 1.0;
               }
            }
         }
         #endif
      }
      programs {
         /* on load: reset the game and start the game loop */
         program {
            signal: "load";
            source: "";
            script {
               game_reset();
               timer(0.1 , "game_loop", 0);
            }
         }
         /* mouse left click: start the game, if not yet started */
         program {
            signal: "mouse,down,1";
            source: "bg";
            script {
               if (get_float(ballspeedx) == 0.0)
                  start_game();
            }
         }
         /* mouse right click: restart the game*/
         program {
            signal: "mouse,down,3";
            source: "bg";
            script {
               game_reset();
            }
         }
      }
   }
}