Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/boards/clickgame.c
blob: e1351b60632da92a1425dcce9cbfc81550e14e0a (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/* gcompris - clickgame.c
 *
 * Time-stamp: <2006/08/21 23:30:07 bruno>
 *
 * Copyright (C) 2000 Bruno Coudoin
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef __APPLE__
#   include <sys/types.h>
#endif
#include <string.h>

#include "gcompris/gcompris.h"
#include "gcompris/pixbuf_util.h"

#include <libart_lgpl/art_affine.h>

#define SOUNDLISTFILE PACKAGE

static gboolean board_paused = TRUE;

static GList *item_list = NULL;
static GList *item2del_list = NULL;

static GcomprisBoard *gcomprisBoard = NULL;

static gint dummy_id = 0;
static gint animate_id = 0;
static gint drop_items_id = 0;

static  GList *imagelist = NULL;

typedef struct {
  double speed;
  gint currentItem;
  GnomeCanvasItem *rootitem;
  GList *item_list;
} FishItem;

static void	 clickgame_start (GcomprisBoard *agcomprisBoard);
static void	 clickgame_pause (gboolean pause);
static void	 clickgame_end (void);
static gboolean	 clickgame_is_our_board (GcomprisBoard *gcomprisBoard);
static void	 clickgame_set_level (guint level);
static void	 clickgame_config(void);

static FishItem *clickgame_create_item(GnomeCanvasGroup *parent);
static gint	 clickgame_drop_items (GtkWidget *widget, gpointer data);
static gint	 clickgame_move_items (GtkWidget *widget, gpointer data);
static gint	 clickgame_animate_items (GtkWidget *widget, gpointer data);
static void	 clickgame_destroy_item(FishItem *fishitem);
static void	 clickgame_destroy_items(void);
static void	 clickgame_destroy_all_items(void);
static void	 setup_item(FishItem *fishitem);
static void	 load_random_pixmap(void);
static void	 clickgame_next_level(void);

static int gamewon;
static void	 game_won(void);

static  guint32              fallSpeed = 0;
static  double               speed = 0.0;
static  double               imageZoom = 0.0;

// List of images to use in the game
static gchar *pixmapList[] =
  {
    "fishes/blueking2_%d.png",
    "fishes/butfish_%d.png",
    "fishes/cichlid1_%d.png",
    "fishes/cichlid4_%d.png",
    "fishes/collaris_%d.png",
    "fishes/discus2_%d.png",
    "fishes/discus3_%d.png",
    "fishes/eel_%d.png",
    "fishes/f00_%d.png",
    "fishes/f01_%d.png",
    "fishes/f02_%d.png",
    "fishes/f03_%d.png",
    "fishes/f04_%d.png",
    "fishes/f05_%d.png",
    "fishes/f06_%d.png",
    "fishes/f07_%d.png",
    "fishes/f08_%d.png",
    "fishes/f09_%d.png",
    "fishes/f10_%d.png",
    "fishes/f11_%d.png",
    "fishes/f12_%d.png",
    "fishes/f13_%d.png",
    "fishes/manta_%d.png",
    "fishes/newf1_%d.png",
    "fishes/QueenAngel_%d.png",
    "fishes/shark1_%d.png",
    "fishes/six_barred_%d.png",
    "fishes/teeth_%d.png",
  };
#define NUMBER_OF_IMAGES G_N_ELEMENTS(pixmapList)

/* Description of this plugin */
static BoardPlugin menu_bp =
{
   NULL,
   NULL,
   "Click On Me",
   "Left-Click with the mouse on all swimming fish before they leave the fishtank",
   "Bruno Coudoin <bruno.coudoin@free.fr>",
   NULL,
   NULL,
   NULL,
   NULL,
   clickgame_start,
   clickgame_pause,
   clickgame_end,
   clickgame_is_our_board,
   NULL,
   NULL,
   clickgame_set_level,
   clickgame_config,
   NULL,
   NULL,
   NULL
};

/*
 * Main entry point mandatory for each Gcompris's game
 * ---------------------------------------------------
 *
 */

GET_BPLUGIN_INFO(clickgame)

/*
 * in : boolean TRUE = PAUSE : FALSE = CONTINUE
 *
 */
static void clickgame_pause (gboolean pause)
{
  if(gcomprisBoard==NULL)
    return;

  if(gamewon == TRUE && pause == FALSE) /* the game is won */
    {
      game_won();
    }

  if(pause)
    {
      if (dummy_id) {
	gtk_timeout_remove (dummy_id);
	dummy_id = 0;
      }
      if (animate_id) {
	gtk_timeout_remove (animate_id);
	animate_id = 0;
      }
      if (drop_items_id) {
	gtk_timeout_remove (drop_items_id);
	drop_items_id = 0;
      }
    }
  else
    {
      if(!drop_items_id) {
	drop_items_id = gtk_timeout_add (200,
					 (GtkFunction) clickgame_drop_items, NULL);
      }
      if(!dummy_id) {
	dummy_id = gtk_timeout_add (200, (GtkFunction) clickgame_move_items, NULL);
      }
      if(!animate_id) {
	animate_id = gtk_timeout_add (200, (GtkFunction) clickgame_animate_items, NULL);
      }
    }

  board_paused = pause;
}

/*
 */
static void clickgame_start (GcomprisBoard *agcomprisBoard)
{
  if(agcomprisBoard!=NULL)
    {
      gcomprisBoard = agcomprisBoard;

      /* set initial values for this level */
      gcomprisBoard->level = 1;
      gcomprisBoard->maxlevel=6;
      gcomprisBoard->number_of_sublevel=10; /* Go to next level after this number of 'play' */
      gc_score_start(SCORESTYLE_NOTE,
			   gcomprisBoard->width - 220,
			   gcomprisBoard->height - 50,
			   gcomprisBoard->number_of_sublevel);
      gc_bar_set(GC_BAR_LEVEL);

      clickgame_next_level();

      clickgame_pause(FALSE);

    }

}

static void
clickgame_end ()
{
  if(gcomprisBoard!=NULL)
    {
      clickgame_pause(TRUE);
      gc_score_end();
      clickgame_destroy_all_items();
      gcomprisBoard->level = 1;       // Restart this game to zero
    }
  gcomprisBoard = NULL;
}

static void
clickgame_set_level (guint level)
{

  if(gcomprisBoard!=NULL)
    {
      gcomprisBoard->level=level;
      clickgame_next_level();
    }
}

static gboolean
clickgame_is_our_board (GcomprisBoard *gcomprisBoard)
{
  if (gcomprisBoard)
    {
      if(g_strcasecmp(gcomprisBoard->type, "clickgame")==0)
	{
	  /* Set the plugin entry */
	  gcomprisBoard->plugin=&menu_bp;

	  return TRUE;
	}
    }
  return FALSE;
}

static void
clickgame_config ()
{
  if(gcomprisBoard!=NULL)
    {
      clickgame_pause(TRUE);
    }
}

/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/

/* set initial values for the next level */
static void clickgame_next_level()
{

  switch(gcomprisBoard->level)
    {
    case 1:
      gc_set_background(gnome_canvas_root(gcomprisBoard->canvas),
			      "clickgame/nur00523.jpg");
      break;
    case 2:
      gc_set_background(gnome_canvas_root(gcomprisBoard->canvas),
      			      "clickgame/nur03006.jpg");
      break;
    case 3:
      gc_set_background(gnome_canvas_root(gcomprisBoard->canvas),
			      "clickgame/nur03011.jpg");
      break;
    case 4:
      gc_set_background(gnome_canvas_root(gcomprisBoard->canvas),
			      "clickgame/nur03010.jpg");
      break;
    case 5:
      gc_set_background(gnome_canvas_root(gcomprisBoard->canvas),
			      "clickgame/nur03013.jpg");
      break;
    default:
      gc_set_background(gnome_canvas_root(gcomprisBoard->canvas),
			      "clickgame/nur03505.jpg");
    }

  gc_bar_set_level(gcomprisBoard);

  /* Try the next level */
  speed=100+(40/(gcomprisBoard->level));
  fallSpeed=5000-gcomprisBoard->level*200;
  /* Make the images tend to 0.5 ratio */
  imageZoom=0.5+(0.5/(gcomprisBoard->level));
  gcomprisBoard->sublevel=0;
  gc_score_set(gcomprisBoard->sublevel);

}


static void clickgame_animate_item(FishItem *fishitem)
{
  gint currentItem;

  /* Manage the animation */
  currentItem = fishitem->currentItem;
  fishitem->currentItem++;
  if(fishitem->currentItem >= g_list_length(fishitem->item_list))
    fishitem->currentItem=0;

  gnome_canvas_item_show((GnomeCanvasItem *)g_list_nth_data(fishitem->item_list,
							    fishitem->currentItem));

  gnome_canvas_item_hide((GnomeCanvasItem *)g_list_nth_data(fishitem->item_list,
							    currentItem));
}

static void clickgame_move_item(FishItem *fishitem)
{
  double x1, y1, x2, y2;

  GnomeCanvasItem *item = fishitem->rootitem;

  gnome_canvas_item_move(item, fishitem->speed, 0.0);


  gnome_canvas_item_get_bounds    (item,
				   &x1,
				   &y1,
				   &x2,
				   &y2);

  if(fishitem->speed>0)
    {
      if(x1>gcomprisBoard->width) {
	item2del_list = g_list_append (item2del_list, fishitem);
	gc_sound_play_ogg ("crash", NULL);
      }
    }
  else
    {
      if(x2<0) {
	item2del_list = g_list_append (item2del_list, fishitem);
	gc_sound_play_ogg ("crash", NULL);
      }
    }

}

static void clickgame_destroy_item(FishItem *fishitem)
{
  GnomeCanvasItem *item = fishitem->rootitem;

  item_list = g_list_remove (item_list, fishitem);
  item2del_list = g_list_remove (item2del_list, fishitem);
  gtk_object_destroy (GTK_OBJECT(item));

  g_list_free(fishitem->item_list);
  g_free(fishitem);
}

/* Destroy items that falls out of the canvas */
static void clickgame_destroy_items()
{
  FishItem *fishitem;

  while(g_list_length(item2del_list)>0)
    {
      fishitem = g_list_nth_data(item2del_list, 0);
      clickgame_destroy_item(fishitem);
    }
}

/* Destroy all the items */
static void clickgame_destroy_all_items()
{
  FishItem *fishitem;

  while(g_list_length(item_list)>0)
    {
      fishitem = g_list_nth_data(item_list, 0);
      clickgame_destroy_item(fishitem);
    }
  if(item_list) {
    g_list_free(item_list);
    item_list = NULL;
  }

  if(imagelist) {
    g_list_free(imagelist);
    imagelist = NULL;
  }
}

/*
 * This does the moves of the game items on the play canvas
 *
 */
static gint clickgame_move_items (GtkWidget *widget, gpointer data)
{
  g_list_foreach (item_list, (GFunc) clickgame_move_item, NULL);

  /* Destroy items that falls out of the canvas */
  clickgame_destroy_items();

  dummy_id = gtk_timeout_add (speed,
			      (GtkFunction) clickgame_move_items, NULL);

  return(FALSE);
}

/*
 * This does the icon animation
 *
 */
static gint clickgame_animate_items (GtkWidget *widget, gpointer data)
{
  g_list_foreach (item_list, (GFunc) clickgame_animate_item, NULL);

  animate_id = gtk_timeout_add (1000,
			      (GtkFunction) clickgame_animate_items, NULL);

  return(FALSE);
}

static FishItem *clickgame_create_item(GnomeCanvasGroup *parent)
{
  GdkPixbuf *pixmap = NULL;
  GdkPixbuf *pixmap2 = NULL;
  GnomeCanvasItem *rootitem, *item;
  FishItem *fishitem;
  double x;
  gint i, length;

  /* Avoid to have too much items displayed */
  if(g_list_length(item_list)>5)
    return NULL;

  load_random_pixmap();

  fishitem = g_malloc(sizeof(FishItem));
  fishitem->currentItem   = 0;
  fishitem->speed = (double)(g_random_int()%(60))/10 - 3;
  fishitem->item_list = NULL;

  pixmap = (GdkPixbuf *)g_list_nth_data(imagelist, 0);

  if(pixmap==NULL)
    return NULL;

  if(fishitem->speed<0)
    {
      x = (double) gcomprisBoard->width;
      fishitem->speed=MIN(fishitem->speed, -1);
    }
  else
    {
      x = (double) -gdk_pixbuf_get_width(pixmap)*imageZoom;
      fishitem->speed=MAX(fishitem->speed, 1);
    }

  rootitem = \
    gnome_canvas_item_new (parent,
			   gnome_canvas_group_get_type (),
			   "x", x,
			   "y", (double)(g_random_int()%(gcomprisBoard->height-
						 (guint)(gdk_pixbuf_get_height(pixmap)*
							 imageZoom))),
			   NULL);


  fishitem->rootitem = rootitem;

  length = g_list_length(imagelist);
  for(i=0; i<length; i++)
    {
      pixmap = (GdkPixbuf *)g_list_nth_data(imagelist, i);

      pixmap2 = pixbuf_copy_mirror(pixmap, (fishitem->speed<0?TRUE:FALSE), FALSE);

      gdk_pixbuf_unref(pixmap);

      item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
				    gnome_canvas_pixbuf_get_type (),
				    "pixbuf", pixmap2,
				    "x", 0.0,
				    "y", 0.0,
				    "width", (double) gdk_pixbuf_get_width(pixmap)*imageZoom,
				    "height", (double) gdk_pixbuf_get_height(pixmap)*imageZoom,
				    "width_set", TRUE,
				    "height_set", TRUE,
				    NULL);
      gdk_pixbuf_unref(pixmap2);

      if(i==fishitem->currentItem)
	gnome_canvas_item_show(item);
      else
	gnome_canvas_item_hide(item);

      fishitem->item_list = g_list_append (fishitem->item_list, item);
    }

  for(i=0; i<length; i++)
    {
      pixmap = (GdkPixbuf *)g_list_nth_data(imagelist, 0);
      imagelist = g_list_remove(imagelist, pixmap);
    }
  item_list = g_list_append (item_list, fishitem);

  return (fishitem);
}

static void clickgame_add_new_item()
{
  setup_item (clickgame_create_item(gnome_canvas_root(gcomprisBoard->canvas)));
}

/*
 * This is called on a low frequency and is used to drop new items
 *
 */
static gint clickgame_drop_items (GtkWidget *widget, gpointer data)
{
  clickgame_add_new_item();

  drop_items_id = gtk_timeout_add (fallSpeed,
				   (GtkFunction) clickgame_drop_items, NULL);
  return (FALSE);
}

/* ==================================== */
static void game_won()
{
  gcomprisBoard->sublevel++;

  if(gcomprisBoard->sublevel>=gcomprisBoard->number_of_sublevel) {
    /* Try the next level */
    gcomprisBoard->sublevel=0;
    gcomprisBoard->level++;
    if(gcomprisBoard->level>gcomprisBoard->maxlevel) { // the current board is finished : bail out
      gc_bonus_end_display(BOARD_FINISHED_RANDOM);
      return;
    }
    gc_sound_play_ogg ("sounds/bonus.ogg", NULL);
  }
  clickgame_next_level();
}

static gint
item_event(GnomeCanvasItem *item, GdkEvent *event, FishItem *fishitem)
{
   static double x, y;
   double new_x, new_y;
   GdkCursor *fleur;
   static int dragging;
   double item_x, item_y;

   if(!gcomprisBoard)
     return FALSE;

   if(board_paused)
     return FALSE;

   item_x = event->button.x;
   item_y = event->button.y;
   gnome_canvas_item_w2i(item->parent, &item_x, &item_y);

   switch (event->type)
     {
     case GDK_BUTTON_PRESS:
       switch(event->button.button)
         {
         case 1:
           if (event->button.state & GDK_SHIFT_MASK)
             {
               x = item_x;
               y = item_y;

               fleur = gdk_cursor_new(GDK_FLEUR);
               gc_canvas_item_grab(item,
                                      GDK_POINTER_MOTION_MASK |
                                      GDK_BUTTON_RELEASE_MASK,
                                      fleur,
                                      event->button.time);
               gdk_cursor_destroy(fleur);
               dragging = TRUE;
             }
           else
             {
	       clickgame_destroy_item(fishitem);
	       gc_sound_play_ogg ("sounds/gobble.ogg", NULL);

	       gcomprisBoard->sublevel++;
	       gc_score_set(gcomprisBoard->sublevel);

	       if(gcomprisBoard->sublevel>=gcomprisBoard->number_of_sublevel) {
		 gamewon = TRUE;
		 clickgame_destroy_all_items();
		 gc_bonus_display(gamewon, BONUS_FLOWER);
		 return FALSE;
	       }
	       /* Drop a new item now to speed up the game */
	       if(g_list_length(item_list)==0)
		 {
		   if (drop_items_id) {
		     /* Remove pending new item creation to sync the falls */
		     gtk_timeout_remove (drop_items_id);
		     drop_items_id = 0;
		   }
		   if(!drop_items_id) {
		     drop_items_id = gtk_timeout_add (0,
						      (GtkFunction) clickgame_drop_items,
						      NULL);
		   }
		 }
             }
           break;

         case 3:
	   /* Speed up the fish */
	   if(fishitem->speed>0)
	     fishitem->speed = MAX(fishitem->speed+1 , 5);
	   else
	     fishitem->speed = MIN(fishitem->speed-1, -5);
	   break;

         case 2:
	   /* Slow the fish */
	   if(fishitem->speed>0)
	     fishitem->speed = MAX(fishitem->speed-1, 1);
	   else
	     fishitem->speed = MIN(fishitem->speed+1, -1);
	   break;

         case 4:
	   /* fish up */
	   gnome_canvas_item_move(item, 0.0, -3.0);
	   break;

         case 5:
	   /* fish down */
	   gnome_canvas_item_move(item, 0.0, 3.0);
	   break;

         default:
           break;
         }
       break;

     case GDK_MOTION_NOTIFY:
       if (dragging && (event->motion.state & GDK_BUTTON1_MASK))
         {
           new_x = item_x;
           new_y = item_y;

           gnome_canvas_item_move(item, new_x - x, new_y - y);
           x = new_x;
           y = new_y;
         }
       break;

     case GDK_BUTTON_RELEASE:
       if(dragging)
	 {
	   gc_canvas_item_ungrab(item, event->button.time);
	   dragging = FALSE;
	 }
       break;

     default:
       break;
     }

   return FALSE;
 }

static void
setup_item(FishItem *fishitem)
{
  if(fishitem)
      gtk_signal_connect(GTK_OBJECT(fishitem->rootitem), "event",
			 (GtkSignalFunc) item_event,
			 fishitem);
}

/*
 * Return the list of pixmap in the global
 * imagelist
 */
static void load_random_pixmap()
{
  GdkPixbuf *pixmap;
  int i, j;
  gchar *str = NULL;
  gboolean cont = TRUE;

  i=g_random_int()%(NUMBER_OF_IMAGES);

  /* First image */
  j=0;

  while(cont)
    {
      gchar *url;
      str = g_strdup(pixmapList[i]);

      url = gc_file_find_absolute(str, j++);

      if(!url)
	{
	  cont = FALSE;
	}
      else
	{
	  pixmap = gc_pixmap_load (str, j-1);

	  if(pixmap) {
	    imagelist = g_list_append (imagelist, pixmap);
	  } else {
	    cont = FALSE;
	  }
	}
      g_free(str);
    }

}