~ml-launchpad/ubuntu/natty/gcompris/fix-for-777349

« back to all changes in this revision

Viewing changes to src/boards/target.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Gariepy, Marc Gariepy, Stephane Graber
  • Date: 2010-01-04 17:42:49 UTC
  • mfrom: (1.1.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20100104174249-7bupatd9dtxyhvs4
Tags: 9.0-0ubuntu1
[Marc Gariepy]
* New upstream release (9.0).
* Remove cache.c from POTFILES to avoid FTBFS
* Remove unneeded rm in debian/rules (file no longer exists upstream)

[Stephane Graber]
* Bump Debian standards to 3.8.3
* Add patch system (dpatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* gcompris - target.c
2
 
 *
3
 
 * Copyright (C) 2001 Bruno Coudoin
4
 
 *
5
 
 *   This program is free software; you can redistribute it and/or modify
6
 
 *   it under the terms of the GNU General Public License as published by
7
 
 *   the Free Software Foundation; either version 3 of the License, or
8
 
 *   (at your option) any later version.
9
 
 *
10
 
 *   This program is distributed in the hope that it will be useful,
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *   GNU General Public License for more details.
14
 
 *
15
 
 *   You should have received a copy of the GNU General Public License
16
 
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
17
 
 */
18
 
 
19
 
#include "gcompris/gcompris.h"
20
 
 
21
 
#define SOUNDLISTFILE PACKAGE
22
 
 
23
 
static GcomprisBoard *gcomprisBoard = NULL;
24
 
static gboolean board_paused = TRUE;
25
 
 
26
 
static void              start_board (GcomprisBoard *agcomprisBoard);
27
 
static void              pause_board (gboolean pause);
28
 
static void              end_board (void);
29
 
static gboolean          is_our_board (GcomprisBoard *gcomprisBoard);
30
 
static void              set_level (guint level);
31
 
static int gamewon;
32
 
static void              game_won(void);
33
 
 
34
 
#define VERTICAL_SEPARATION     30
35
 
#define HORIZONTAL_SEPARATION   30
36
 
#define TEXT_COLOR              "white"
37
 
 
38
 
static GnomeCanvasGroup *boardRootItem = NULL;
39
 
static GnomeCanvasGroup *speedRootItem = NULL;
40
 
 
41
 
static double wind_speed;
42
 
static double ang;
43
 
 
44
 
static GnomeCanvasItem  *answer_item = NULL;
45
 
static gchar             answer_string[10];
46
 
static guint             answer_string_index = 0;
47
 
 
48
 
static GnomeCanvasItem *animate_item = NULL;
49
 
static gint             animate_id = 0;
50
 
static gint             animate_item_distance = 0;
51
 
static gint             animate_item_size = 0;
52
 
static double           animate_item_x = 0;
53
 
static double           animate_item_y = 0;
54
 
#define MAX_DART_SIZE 20
55
 
#define MIN_DART_SIZE 3
56
 
 
57
 
static guint            user_points = 0;
58
 
 
59
 
/*
60
 
 * Functions Definition
61
 
 */
62
 
static void             process_ok(void);
63
 
static gint             key_press(guint keyval, gchar *commit_str, gchar *preedit_str);
64
 
static GnomeCanvasItem *target_create_item(GnomeCanvasGroup *parent);
65
 
static void             target_destroy_all_items(void);
66
 
static void             target_next_level(void);
67
 
static gint             item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data);
68
 
static void             animate_items(void);
69
 
static void             launch_dart(double item_x, double item_y);
70
 
 
71
 
/*
72
 
 * distance to target, min wind speed, max wind speed, target1 width(center),
73
 
 * value1 (for target1), ... to target10, value10
74
 
 *
75
 
 * a target width of 0 means no such target
76
 
 */
77
 
#define MAX_NUMBER_OF_TARGET 10
78
 
 
79
 
typedef struct {
80
 
  guint number_of_arrow;
81
 
  guint target_distance;
82
 
  guint target_min_wind_speed;
83
 
  guint target_max_wind_speed;
84
 
  gint target_width_value[MAX_NUMBER_OF_TARGET*2];
85
 
} TargetDefinition;
86
 
 
87
 
 
88
 
/*
89
 
 * Definition of targets one line by level based on TargetDefinition
90
 
 */
91
 
static TargetDefinition targetDefinition[] =
92
 
{
93
 
  { 3, 100, 2, 5,  { 40, 5 , 80, 3, 150, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
94
 
  { 5, 150, 2, 7,  { 30, 10, 50, 5, 150, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
95
 
  { 7, 200, 4, 9,  { 20, 10, 40, 5, 60, 3, 150, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
96
 
  { 7, 200, 5, 10, { 15, 100, 35, 50, 55, 10, 75, 5, 150, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }
97
 
};
98
 
#define NUMBER_OF_TARGET 4
99
 
 
100
 
#define TARGET_CENTER_X 235
101
 
#define TARGET_CENTER_Y 260
102
 
 
103
 
#define SPEED_CENTER_X  660
104
 
#define SPEED_CENTER_Y  125
105
 
 
106
 
static guint target_colors[] = {
107
 
  0xAA0000FF, 0x00AA00FF, 0x0000AAFF, 0xAAAA00FF, 0x00AAAAFF, 0xAA00AAFF, 0xAA0000FF, 0x00AA00FF, 0x0000AAFF, 0xAA0000FF
108
 
};
109
 
 
110
 
static guint number_of_arrow = 0;
111
 
 
112
 
/* Description of this plugin */
113
 
static BoardPlugin menu_bp =
114
 
  {
115
 
    NULL,
116
 
    NULL,
117
 
    "Practice addition with a target game",
118
 
    "Hit the target and count your points",
119
 
    "Bruno Coudoin <bruno.coudoin@free.fr>",
120
 
    NULL,
121
 
    NULL,
122
 
    NULL,
123
 
    NULL,
124
 
    start_board,
125
 
    pause_board,
126
 
    end_board,
127
 
    is_our_board,
128
 
    key_press,
129
 
    process_ok,
130
 
    set_level,
131
 
    NULL,
132
 
    NULL,
133
 
    NULL,
134
 
    NULL
135
 
  };
136
 
 
137
 
/*
138
 
 * Main entry point mandatory for each Gcompris's game
139
 
 * ---------------------------------------------------
140
 
 *
141
 
 */
142
 
 
143
 
GET_BPLUGIN_INFO(target)
144
 
 
145
 
/*
146
 
 * in : boolean TRUE = PAUSE : FALSE = CONTINUE
147
 
 *
148
 
 */
149
 
static void pause_board (gboolean pause)
150
 
{
151
 
  if(gcomprisBoard==NULL)
152
 
    return;
153
 
 
154
 
  if(gamewon == TRUE && pause == FALSE) /* the game is won */
155
 
    {
156
 
      game_won();
157
 
    }
158
 
 
159
 
  if(pause)
160
 
    {
161
 
      if (animate_id) {
162
 
        gtk_timeout_remove (animate_id);
163
 
        animate_id = 0;
164
 
      }
165
 
    }
166
 
  else
167
 
    {
168
 
      if(animate_item) {
169
 
        animate_id = gtk_timeout_add (200, (GtkFunction) animate_items, NULL);
170
 
      }
171
 
    }
172
 
 
173
 
  board_paused = pause;
174
 
}
175
 
 
176
 
/*
177
 
 */
178
 
static void start_board (GcomprisBoard *agcomprisBoard)
179
 
{
180
 
 
181
 
  if(agcomprisBoard!=NULL)
182
 
    {
183
 
      gcomprisBoard=agcomprisBoard;
184
 
 
185
 
      /* disable im_context */
186
 
      gcomprisBoard->disable_im_context = TRUE;
187
 
 
188
 
      gcomprisBoard->level=1;
189
 
      gcomprisBoard->maxlevel=NUMBER_OF_TARGET;
190
 
      gcomprisBoard->sublevel=1;
191
 
      gcomprisBoard->number_of_sublevel=1; /* Go to next level after this number of 'play' */
192
 
 
193
 
      gc_set_background(gnome_canvas_root(gcomprisBoard->canvas), "opt/target_background.jpg");
194
 
 
195
 
      target_next_level();
196
 
 
197
 
      gamewon = FALSE;
198
 
      pause_board(FALSE);
199
 
    }
200
 
}
201
 
/* ======================================= */
202
 
static void end_board ()
203
 
{
204
 
  if(gcomprisBoard!=NULL)
205
 
    {
206
 
      pause_board(TRUE);
207
 
      target_destroy_all_items();
208
 
    }
209
 
  gcomprisBoard = NULL;
210
 
}
211
 
 
212
 
/* ======================================= */
213
 
static void set_level (guint level)
214
 
{
215
 
 
216
 
  if(gcomprisBoard!=NULL)
217
 
    {
218
 
      gcomprisBoard->level=level;
219
 
      gcomprisBoard->sublevel=1;
220
 
      target_next_level();
221
 
    }
222
 
}
223
 
 
224
 
 
225
 
/* ======================================= */
226
 
static gint key_press(guint keyval, gchar *commit_str, gchar *preedit_str)
227
 
{
228
 
  guint c;
229
 
 
230
 
  if(board_paused || !gcomprisBoard)
231
 
    return FALSE;
232
 
 
233
 
  /* Add some filter for control and shift key */
234
 
  switch (keyval)
235
 
    {
236
 
      /* Avoid all this keys to be interpreted by this game */
237
 
    case GDK_Shift_L:
238
 
    case GDK_Shift_R:
239
 
    case GDK_Control_L:
240
 
    case GDK_Control_R:
241
 
    case GDK_Caps_Lock:
242
 
    case GDK_Shift_Lock:
243
 
    case GDK_Meta_L:
244
 
    case GDK_Meta_R:
245
 
    case GDK_Alt_L:
246
 
    case GDK_Alt_R:
247
 
    case GDK_Super_L:
248
 
    case GDK_Super_R:
249
 
    case GDK_Hyper_L:
250
 
    case GDK_Hyper_R:
251
 
    case GDK_Num_Lock:
252
 
      return FALSE;
253
 
    case GDK_KP_Enter:
254
 
    case GDK_Return:
255
 
      process_ok();
256
 
      return TRUE;
257
 
    case GDK_Right:
258
 
    case GDK_Left:
259
 
      break;
260
 
    case GDK_BackSpace:
261
 
      if(answer_string_index>0)
262
 
        {
263
 
          answer_string_index--;
264
 
          answer_string[answer_string_index] = 0;
265
 
        }
266
 
      break;
267
 
    }
268
 
 
269
 
  c = tolower(keyval);
270
 
 
271
 
  /* Limit the user entry to 5 digits */
272
 
  if(c>='0' && c<='9' && answer_string_index<5)
273
 
    {
274
 
      answer_string[answer_string_index++] = c;
275
 
      answer_string[answer_string_index] = 0;
276
 
    }
277
 
 
278
 
  if(answer_item)
279
 
    {
280
 
      gchar *tmpstr = g_strdup_printf(_("Points = %s"), answer_string);
281
 
      gnome_canvas_item_set(answer_item,
282
 
                            "text", tmpstr,
283
 
                            NULL);
284
 
      g_free(tmpstr);
285
 
    }
286
 
 
287
 
  return TRUE;
288
 
}
289
 
 
290
 
 
291
 
/* ======================================= */
292
 
static gboolean is_our_board (GcomprisBoard *gcomprisBoard)
293
 
{
294
 
  if (gcomprisBoard)
295
 
    {
296
 
      if(g_strcasecmp(gcomprisBoard->type, "target")==0)
297
 
        {
298
 
          /* Set the plugin entry */
299
 
          gcomprisBoard->plugin=&menu_bp;
300
 
 
301
 
          return TRUE;
302
 
        }
303
 
    }
304
 
  return FALSE;
305
 
}
306
 
 
307
 
/*-------------------------------------------------------------------------------*/
308
 
/*-------------------------------------------------------------------------------*/
309
 
/* set initial values for the next level */
310
 
static void target_next_level()
311
 
{
312
 
 
313
 
  gc_bar_set(GC_BAR_LEVEL);
314
 
  gc_bar_set_level(gcomprisBoard);
315
 
 
316
 
  target_destroy_all_items();
317
 
  gamewon = FALSE;
318
 
 
319
 
  /* Try the next level */
320
 
  target_create_item(gnome_canvas_root(gcomprisBoard->canvas));
321
 
}
322
 
/* ==================================== */
323
 
/* Destroy all the items */
324
 
static void target_destroy_all_items()
325
 
{
326
 
  if(boardRootItem!=NULL)
327
 
    gtk_object_destroy (GTK_OBJECT(boardRootItem));
328
 
 
329
 
  boardRootItem = NULL;
330
 
 
331
 
  if(speedRootItem!=NULL)
332
 
    gtk_object_destroy (GTK_OBJECT(speedRootItem));
333
 
 
334
 
  animate_item = NULL;
335
 
  answer_item = NULL;
336
 
  answer_string_index = 0;
337
 
  user_points = 0;
338
 
 
339
 
  speedRootItem = NULL;
340
 
}
341
 
 
342
 
/*
343
 
 * Display a random wind speed
344
 
 */
345
 
static void display_windspeed()
346
 
{
347
 
  guint second = 0;
348
 
  guint needle_zoom = 15;
349
 
  gchar *tmpstr;
350
 
  GnomeCanvasPoints *canvasPoints;
351
 
  canvasPoints = gnome_canvas_points_new (2);
352
 
 
353
 
  if(speedRootItem!=NULL)
354
 
    gtk_object_destroy (GTK_OBJECT(speedRootItem));
355
 
 
356
 
  speedRootItem = GNOME_CANVAS_GROUP(
357
 
                                    gnome_canvas_item_new (gnome_canvas_root(gcomprisBoard->canvas),
358
 
                                                           gnome_canvas_group_get_type (),
359
 
                                                           "x", (double) 0,
360
 
                                                           "y", (double) 0,
361
 
                                                           NULL));
362
 
 
363
 
  /* Speed orientation */
364
 
  second = g_random_int()%60;
365
 
  ang = second * M_PI / 30;
366
 
 
367
 
  /* Speed force */
368
 
  wind_speed = targetDefinition[gcomprisBoard->level-1].target_min_wind_speed \
369
 
    + g_random_int()%(targetDefinition[gcomprisBoard->level-1].target_max_wind_speed \
370
 
              - targetDefinition[gcomprisBoard->level-1].target_min_wind_speed);
371
 
 
372
 
  canvasPoints->coords[0]=SPEED_CENTER_X;
373
 
  canvasPoints->coords[1]=SPEED_CENTER_Y;
374
 
  canvasPoints->coords[2]=SPEED_CENTER_X + wind_speed * sin(ang) * needle_zoom;
375
 
  canvasPoints->coords[3]=SPEED_CENTER_Y - wind_speed * cos(ang) * needle_zoom;
376
 
  gnome_canvas_item_new (speedRootItem,
377
 
                         gnome_canvas_line_get_type (),
378
 
                         "points", canvasPoints,
379
 
                         "fill_color", "red",
380
 
                         "width_units", (double)1,
381
 
                         "width_pixels", (guint) 4,
382
 
                         "last_arrowhead", TRUE,
383
 
                         "arrow_shape_a", (double) wind_speed,
384
 
                         "arrow_shape_b", (double) wind_speed-15,
385
 
                         "arrow_shape_c", (double) 5.0,
386
 
                         NULL);
387
 
 
388
 
  gnome_canvas_points_free(canvasPoints);
389
 
 
390
 
  /* Draw the center of the speedometer */
391
 
  gnome_canvas_item_new (speedRootItem,
392
 
                         gnome_canvas_ellipse_get_type(),
393
 
                         "x1", (double)SPEED_CENTER_X-5,
394
 
                         "y1", (double)SPEED_CENTER_Y-5,
395
 
                         "x2", (double)SPEED_CENTER_X+5,
396
 
                         "y2", (double)SPEED_CENTER_Y+5,
397
 
                         "fill_color", "red",
398
 
                         "outline_color", "red",
399
 
                         "width_units", (double)1,
400
 
                         NULL);
401
 
 
402
 
  tmpstr = g_strdup_printf(_("Wind speed = %d\nkilometers/hour"), (guint)wind_speed);
403
 
  gnome_canvas_item_new (speedRootItem,
404
 
                         gnome_canvas_text_get_type (),
405
 
                         "text", tmpstr,
406
 
                         "font", gc_skin_font_board_medium,
407
 
                         "x", (double) SPEED_CENTER_X,
408
 
                         "y", (double) SPEED_CENTER_Y + 110,
409
 
                         "anchor", GTK_ANCHOR_CENTER,
410
 
                         "fill_color", "white",
411
 
                         NULL);
412
 
  g_free(tmpstr);
413
 
 
414
 
}
415
 
 
416
 
 
417
 
 
418
 
/* ==================================== */
419
 
static GnomeCanvasItem *target_create_item(GnomeCanvasGroup *parent)
420
 
{
421
 
  int i;
422
 
  gchar *tmpstr;
423
 
  GnomeCanvasItem *item = NULL;
424
 
 
425
 
  boardRootItem = GNOME_CANVAS_GROUP(
426
 
                                     gnome_canvas_item_new (parent,
427
 
                                                            gnome_canvas_group_get_type (),
428
 
                                                            "x", (double) TARGET_CENTER_X,
429
 
                                                            "y", (double) TARGET_CENTER_Y,
430
 
                                                            NULL));
431
 
  for(i=0; i<MAX_NUMBER_OF_TARGET; i++)
432
 
    {
433
 
      if(targetDefinition[gcomprisBoard->level-1].target_width_value[i*2]>0)
434
 
        {
435
 
          item = gnome_canvas_item_new (boardRootItem,
436
 
                                        gnome_canvas_ellipse_get_type(),
437
 
                                        "x1", (double)-targetDefinition[gcomprisBoard->level-1].target_width_value[i*2],
438
 
                                        "y1", (double)-targetDefinition[gcomprisBoard->level-1].target_width_value[i*2],
439
 
                                        "x2", (double)targetDefinition[gcomprisBoard->level-1].target_width_value[i*2],
440
 
                                        "y2", (double)targetDefinition[gcomprisBoard->level-1].target_width_value[i*2],
441
 
                                        "fill_color_rgba", target_colors[i],
442
 
                                        "outline_color", "black",
443
 
                                        "width_units", (double)1,
444
 
                                        NULL);
445
 
 
446
 
          gnome_canvas_item_lower_to_bottom(item);
447
 
          gtk_signal_connect(GTK_OBJECT(item), "event", (GtkSignalFunc) item_event, NULL);
448
 
 
449
 
          /* Display the value for this target */
450
 
          tmpstr = g_strdup_printf("%d",
451
 
                                   targetDefinition[gcomprisBoard->level-1].target_width_value[i*2+1]);
452
 
          item = gnome_canvas_item_new (boardRootItem,
453
 
                                        gnome_canvas_text_get_type (),
454
 
                                        "text", tmpstr,
455
 
                                        "font", gc_skin_font_board_medium,
456
 
                                        "x", (double) 0,
457
 
                                        "y", (double) targetDefinition[gcomprisBoard->level-1].target_width_value[i*2] - 10,
458
 
                                        "anchor", GTK_ANCHOR_CENTER,
459
 
                                        "fill_color", "white",
460
 
                                        NULL);
461
 
          g_free(tmpstr);
462
 
 
463
 
          gtk_signal_connect(GTK_OBJECT(item), "event", (GtkSignalFunc) item_event, NULL);
464
 
        }
465
 
    }
466
 
 
467
 
  number_of_arrow = targetDefinition[gcomprisBoard->level-1].number_of_arrow;
468
 
 
469
 
  tmpstr = g_strdup_printf(_("Distance to target = %d meters"),
470
 
                           targetDefinition[gcomprisBoard->level-1].target_distance);
471
 
  gnome_canvas_item_new (boardRootItem,
472
 
                         gnome_canvas_text_get_type (),
473
 
                         "text", tmpstr,
474
 
                         "font", gc_skin_font_board_medium,
475
 
                         "x", (double) 0,
476
 
                         "y", (double) BOARDHEIGHT-TARGET_CENTER_Y -45,
477
 
                         "anchor", GTK_ANCHOR_CENTER,
478
 
                         "fill_color", "black",
479
 
                         NULL);
480
 
  g_free(tmpstr);
481
 
 
482
 
  display_windspeed();
483
 
 
484
 
  return NULL;
485
 
}
486
 
/* ==================================== */
487
 
static void game_won()
488
 
{
489
 
  gcomprisBoard->sublevel++;
490
 
 
491
 
  if(gcomprisBoard->sublevel>gcomprisBoard->number_of_sublevel) {
492
 
    /* Try the next level */
493
 
    gcomprisBoard->sublevel=1;
494
 
    gcomprisBoard->level++;
495
 
    if(gcomprisBoard->level>gcomprisBoard->maxlevel) { // the current board is finished : bail out
496
 
      gc_bonus_end_display(GC_BOARD_FINISHED_RANDOM);
497
 
      return;
498
 
    }
499
 
    gc_sound_play_ogg ("sounds/bonus.wav", NULL);
500
 
  }
501
 
  target_next_level();
502
 
}
503
 
 
504
 
static void process_ok()
505
 
{
506
 
  guint answer_points = atoi(answer_string);
507
 
 
508
 
  if(answer_item) {
509
 
 
510
 
    if(answer_points == user_points)
511
 
      {
512
 
        gamewon = TRUE;
513
 
        target_destroy_all_items();
514
 
        gc_bonus_display(gamewon, GC_BONUS_SMILEY);
515
 
      }
516
 
    else
517
 
      {
518
 
        gamewon = FALSE;
519
 
        gc_bonus_display(gamewon, GC_BONUS_SMILEY);
520
 
      }
521
 
  }
522
 
 
523
 
}
524
 
 
525
 
/*
526
 
 * Request score
527
 
 *
528
 
 */
529
 
static void request_score()
530
 
{
531
 
  GdkPixbuf *button_pixmap = NULL;
532
 
  double y_offset = 160;
533
 
  double x_offset = 245;
534
 
  gchar *tmpstr;
535
 
 
536
 
  gc_bar_set(GC_BAR_LEVEL|GC_BAR_OK);
537
 
  button_pixmap = gc_skin_pixmap_load("button_large2.png");
538
 
  gnome_canvas_item_new (boardRootItem,
539
 
                         gnome_canvas_pixbuf_get_type (),
540
 
                         "pixbuf",  button_pixmap,
541
 
                         "x", x_offset,
542
 
                         "y", y_offset,
543
 
                         NULL);
544
 
 
545
 
  tmpstr =  g_strdup_printf(_("Points = %s"), "");
546
 
  answer_item = gnome_canvas_item_new (boardRootItem,
547
 
                                       gnome_canvas_text_get_type (),
548
 
                                       "text", tmpstr,
549
 
                                       "font", gc_skin_font_board_title_bold,
550
 
                                       "x", (double) x_offset + gdk_pixbuf_get_width(button_pixmap)/2,
551
 
                                       "y", (double) y_offset + gdk_pixbuf_get_height(button_pixmap)/2,
552
 
                                       "anchor", GTK_ANCHOR_CENTER,
553
 
                                       "fill_color", "black",
554
 
                                       NULL);
555
 
  g_free(tmpstr);
556
 
 
557
 
  gdk_pixbuf_unref(button_pixmap);
558
 
}
559
 
 
560
 
static void add_points(double x, double y)
561
 
{
562
 
  guint i;
563
 
  double diametre;
564
 
 
565
 
  // Calculate the distance
566
 
  diametre = sqrt(x*x+y*y);
567
 
 
568
 
  for(i=0; i<MAX_NUMBER_OF_TARGET; i++)
569
 
    {
570
 
      if(diametre < targetDefinition[gcomprisBoard->level-1].target_width_value[i*2])
571
 
        {
572
 
          user_points += targetDefinition[gcomprisBoard->level-1].target_width_value[i*2+1];
573
 
          break;
574
 
        }
575
 
    }
576
 
 
577
 
}
578
 
 
579
 
 
580
 
/*
581
 
 * Dart animation
582
 
 *
583
 
 */
584
 
static void animate_items()
585
 
{
586
 
  if(board_paused)
587
 
    return;
588
 
 
589
 
  if(!animate_item)
590
 
    return;
591
 
 
592
 
  // Apply the wind move
593
 
  animate_item_x = animate_item_x + wind_speed * sin(ang);
594
 
  animate_item_y = animate_item_y - wind_speed * cos(ang);
595
 
 
596
 
  gnome_canvas_item_set (animate_item,
597
 
                         "x1", (double)animate_item_x - animate_item_size,
598
 
                         "y1", (double)animate_item_y - animate_item_size,
599
 
                         "x2", (double)animate_item_x + animate_item_size,
600
 
                         "y2", (double)animate_item_y + animate_item_size,
601
 
                         NULL);
602
 
 
603
 
  if(animate_item_size>MIN_DART_SIZE)
604
 
    animate_item_size--;
605
 
 
606
 
  if(--animate_item_distance == 0)
607
 
    {
608
 
      gc_sound_play_ogg ("sounds/brick.wav", NULL);
609
 
 
610
 
      gtk_timeout_remove (animate_id);
611
 
      animate_id = 0;
612
 
      animate_item = NULL;
613
 
 
614
 
      // Calc the point for this dart
615
 
      add_points(animate_item_x, animate_item_y);
616
 
 
617
 
      // Change the wind for the next target
618
 
      display_windspeed();
619
 
    }
620
 
}
621
 
 
622
 
/*
623
 
 *
624
 
 */
625
 
static void launch_dart(double item_x, double item_y)
626
 
{
627
 
 
628
 
  animate_item_x        = item_x;
629
 
  animate_item_y        = item_y;
630
 
  animate_item_size     = MAX_DART_SIZE;
631
 
  animate_item_distance = targetDefinition[gcomprisBoard->level-1].target_distance/10;
632
 
 
633
 
  gc_sound_play_ogg ("sounds/line_end.wav", NULL);
634
 
 
635
 
  animate_item = gnome_canvas_item_new (boardRootItem,
636
 
                                        gnome_canvas_ellipse_get_type(),
637
 
                                        "x1", (double)item_x-MAX_DART_SIZE,
638
 
                                        "y1", (double)item_y-MAX_DART_SIZE,
639
 
                                        "x2", (double)item_x+MAX_DART_SIZE,
640
 
                                        "y2", (double)item_y+MAX_DART_SIZE,
641
 
                                        "fill_color_rgba", 0xFF80FFFF,
642
 
                                        "outline_color", "white",
643
 
                                        "width_units", (double)1,
644
 
                                        NULL);
645
 
 
646
 
  animate_id = gtk_timeout_add (200, (GtkFunction) animate_items, NULL);
647
 
 
648
 
  if(--number_of_arrow == 0)
649
 
    {
650
 
      request_score();
651
 
    }
652
 
 
653
 
}
654
 
 
655
 
/* ==================================== */
656
 
static gint
657
 
item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data)
658
 
{
659
 
  double item_x, item_y;
660
 
 
661
 
  if(board_paused)
662
 
    return FALSE;
663
 
 
664
 
  /* Is there already a dart on air */
665
 
  if(number_of_arrow == 0 || animate_item)
666
 
    return FALSE;
667
 
 
668
 
  switch (event->type)
669
 
    {
670
 
    case GDK_BUTTON_PRESS:
671
 
      switch(event->button.button)
672
 
        {
673
 
        case 1:
674
 
        case 2:
675
 
        case 3:
676
 
          item_x = event->button.x;
677
 
          item_y = event->button.y;
678
 
          gnome_canvas_item_w2i(item->parent, &item_x, &item_y);
679
 
 
680
 
          launch_dart(item_x, item_y);
681
 
 
682
 
          break;
683
 
        default:
684
 
          break;
685
 
        }
686
 
    default:
687
 
      break;
688
 
    }
689
 
  return FALSE;
690
 
}