summaryrefslogtreecommitdiff
path: root/games/pacman/pacman-graphic.c
blob: ab440d87df9ce30baf8bec9926c9f1893bba6849 (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
/*
 *  Copyright (C) 2005 Benjamin Otte <otte@gnome.org>
 *
 *  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, 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.
 *
 *  $Id$
 */

#include "libgame/game-private.h"
#include <math.h>
#include "pacman-graphic.h"

enum {
  PROP_0,
  PROP_DIRECTION,
  PROP_COLOR,
  PROP_ANIMATE
};

static GameGraphicClass *parent_class = NULL;

static void
pacman_graphic_draw (GameGraphic *graphic, cairo_t *cr, const GameRectangle *area)
{
  PacmanGraphic *pacman = PACMAN_GRAPHIC (graphic);
  double mouth_direction = 0;
  double mouth_open, eye_x = 0.5, eye_y = 0.25;

  if (!pacman->animate) {
  } else if (pacman->direction == GAME_DIRECTION_DOWN) {
    mouth_direction = GAME_PI / 2;
    eye_x = 0.25;
    eye_y = 0.5;
  } else if (pacman->direction == GAME_DIRECTION_LEFT) {
    mouth_direction = GAME_PI;
  } else if (pacman->direction == GAME_DIRECTION_UP) {
    mouth_direction = 3 * GAME_PI / 2;
    eye_x = 0.75;
    eye_y = 0.5;
  }
  
  if (pacman->animate)
    mouth_open = ABS (pacman->mouth_open);
  else
    mouth_open = 0.5;
  
  game_color_as_cairo_source (cr, &pacman->color);
  cairo_move_to (cr, 0.5, 0.5);
  cairo_arc (cr, 0.5, 0.5, 0.45, 
      mouth_direction + mouth_open * GAME_PI / 4, 
      mouth_direction + 2 * GAME_PI - mouth_open * GAME_PI / 4);
  cairo_line_to (cr, 0.5, 0.5);
  cairo_fill_preserve (cr);
  cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  cairo_set_line_width (cr, 0.06);
  cairo_stroke_preserve (cr);
  cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
  cairo_set_line_width (cr, 0.02);
  cairo_stroke (cr);
  /* eye */
  cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  cairo_arc (cr, eye_x, eye_y, 0.05, 0.0, 2 * GAME_PI);
  cairo_fill (cr);
}

static void
pacman_graphic_get_property (GObject *object, guint param_id, 
			 GValue *value, GParamSpec *pspec)
{
  PacmanGraphic *graphic = PACMAN_GRAPHIC (object);
  
  switch (param_id) {
    case PROP_DIRECTION:
      g_value_set_enum (value, graphic->direction);
      break;
    case PROP_ANIMATE:
      g_value_set_boolean (value, graphic->animate);
      break;
    case PROP_COLOR:
      g_value_set_boxed (value, &graphic->color);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
  }
}

static void
pacman_graphic_set_property (GObject *object, guint param_id, 
			 const GValue *value, GParamSpec *pspec)
{
  PacmanGraphic *graphic = PACMAN_GRAPHIC (object);
  
  switch (param_id) {
    case PROP_DIRECTION:
      if (graphic->direction != (GameDirection) g_value_get_enum (value)) {
	graphic->direction = g_value_get_enum (value);
	game_graphic_invalidate (GAME_GRAPHIC (graphic));
      }
      break;
    case PROP_ANIMATE:
      graphic->animate = g_value_get_boolean (value);
      game_graphic_invalidate (GAME_GRAPHIC (graphic));
      break;
    case PROP_COLOR:
      graphic->color = *((GameColor *) g_value_get_boxed (value));
      game_graphic_invalidate (GAME_GRAPHIC (graphic));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
  }
}

static void
pacman_graphic_tick (GameObject *object)
{
  PacmanGraphic *pacman = PACMAN_GRAPHIC (object);
  
  if (!pacman->animate)
    return;
  pacman->mouth_open += (double) game_game_get_frame_rate (object->game) / 250;
  if (pacman->mouth_open >= 1.0)
    pacman->mouth_open -= 2.0;
  game_graphic_invalidate (GAME_GRAPHIC (pacman));
}

static void
pacman_graphic_class_init (gpointer g_class, gpointer class_data)
{
  GObjectClass *object_class = G_OBJECT_CLASS (g_class);
  GameObjectClass *gameobject_class = GAME_OBJECT_CLASS (g_class);
  GameGraphicClass *graphic_class = GAME_GRAPHIC_CLASS (g_class);
  
  parent_class = g_type_class_peek_parent (g_class);

  object_class->set_property = pacman_graphic_set_property;
  object_class->get_property = pacman_graphic_get_property;

  g_object_class_install_property (object_class, PROP_DIRECTION,
      g_param_spec_enum ("direction", _("direction"), _("direction last moved to"),
	  GAME_TYPE_DIRECTION, GAME_DIRECTION_NONE, G_PARAM_READWRITE));
  g_object_class_install_property (object_class, PROP_ANIMATE,
      g_param_spec_boolean ("animate", _("animate"), _("set to animate graphic"),
	  TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
  g_object_class_install_property (object_class, PROP_COLOR,
      g_param_spec_boxed ("color", _("color"), _("color in which to draw the pacman"),
	  GAME_TYPE_COLOR, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  gameobject_class->tick_priority = 0;
  gameobject_class->tick_func = pacman_graphic_tick;
  
  graphic_class->draw = pacman_graphic_draw;
}

static void
pacman_graphic_init (GTypeInstance *instance, gpointer g_class)
{
  PacmanGraphic *pacman = PACMAN_GRAPHIC (instance);

  pacman->mouth_open = 0.5;
}

GType
pacman_graphic_get_type ()
{
  static GType graphic_type = 0;
  
  if (!graphic_type) {
    static const GTypeInfo graphic_info = {
      sizeof (PacmanGraphicClass),
      NULL,
      NULL,
      pacman_graphic_class_init,
      NULL,
      NULL,
      sizeof (PacmanGraphic),
      0,
      pacman_graphic_init,
    };
    
    graphic_type = g_type_register_static (GAME_TYPE_GRAPHIC, "PacmanGraphic", &graphic_info, 0);
  }
  
  return graphic_type;
}

GameGraphic *
pacman_graphic_new (GameGame *game, const GameColor *color)
{
  return GAME_GRAPHIC (game_game_add_object (game, PACMAN_TYPE_GRAPHIC, 
	"color", color, NULL));
}