test/snake/snake.c

changeset 240
5ed38debd4a7
parent 238
e22abcd22e47
child 242
6eeb987f1681
equal deleted inserted replaced
239:3b78ad115ccd 240:5ed38debd4a7
65 float speed; 65 float speed;
66 asc_vec2i new_position; 66 asc_vec2i new_position;
67 bool reset_position; 67 bool reset_position;
68 } Player; 68 } Player;
69 69
70 static const unsigned game_field_size = 16; 70 static const unsigned game_field_size = 32;
71 static const unsigned game_field_tile_size = 64; 71 static const unsigned game_field_tile_size = 32;
72 72
73 static void globals_init(void) { 73 static void globals_init(void) {
74 asc_transform_identity(rotations[MOVE_UP]); 74 asc_transform_identity(rotations[MOVE_UP]);
75 asc_transform_roll(rotations[MOVE_LEFT], asc_rad(-90)); 75 asc_transform_roll(rotations[MOVE_LEFT], asc_rad(-90));
76 asc_transform_roll(rotations[MOVE_RIGHT], asc_rad(90)); 76 asc_transform_roll(rotations[MOVE_RIGHT], asc_rad(90));
123 123
124 static void fps_counter_tie_to_corner(AscBehavior *behavior) { 124 static void fps_counter_tie_to_corner(AscBehavior *behavior) {
125 // TODO: this should be replaced with some sort of UI layout manager 125 // TODO: this should be replaced with some sort of UI layout manager
126 AscSceneNode *node = behavior->node; 126 AscSceneNode *node = behavior->node;
127 if (asc_test_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED) || asc_active_window->resized) { 127 if (asc_test_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED) || asc_active_window->resized) {
128 asc_vec2u bottom_right = asc_active_window->dimensions; 128 asc_scene_node_set_position2f(node, ASC_VEC2F(10,
129 asc_vec2u text_size = ((AscText*)node)->dimension; 129 asc_active_window->dimensions.y - ((AscText*)node)->dimension.height - 10
130 asc_scene_node_set_position2f(node, ASC_VEC2F(
131 (int) bottom_right.x - (int) text_size.width - 10,
132 (int) bottom_right.y - (int) text_size.height - 10
133 )); 130 ));
134 } 131 }
135 } 132 }
136 133
137 static void fps_counter_create(void) { 134 static void fps_counter_create(void) {
140 .color = ASC_RGB(255, 255, 255), 137 .color = ASC_RGB(255, 255, 255),
141 .font = asc_font(ASC_FONT_REGULAR, 12), 138 .font = asc_font(ASC_FONT_REGULAR, 12),
142 ); 139 );
143 asc_behavior_add(node, .func = fps_counter_update, .interval = asc_seconds(1)); 140 asc_behavior_add(node, .func = fps_counter_update, .interval = asc_seconds(1));
144 asc_behavior_add(node, fps_counter_tie_to_corner); 141 asc_behavior_add(node, fps_counter_tie_to_corner);
145 asc_ui_add_node(node);
146 }
147
148 static void score_counter_create(void) {
149 AscSceneNode *node = asc_text(
150 .name = "Score Counter",
151 .x = 10, .y = 10,
152 .text = "Score: 0",
153 .color = ASC_RGB(0, 255, 0),
154 .font = asc_font(ASC_FONT_BOLD, 16),
155 );
156 asc_ui_add_node(node); 142 asc_ui_add_node(node);
157 } 143 }
158 144
159 static void player_move(AscBehavior *behavior) { 145 static void player_move(AscBehavior *behavior) {
160 AscSceneNode *node = behavior->node; 146 AscSceneNode *node = behavior->node;
225 .origin_y = game_field_tile_size / 2, 211 .origin_y = game_field_tile_size / 2,
226 ); 212 );
227 asc_scene_add_node(MAIN_SCENE, sprite); 213 asc_scene_add_node(MAIN_SCENE, sprite);
228 Player *player = asc_scene_node_allocate_data(sprite, sizeof(Player)); 214 Player *player = asc_scene_node_allocate_data(sprite, sizeof(Player));
229 player_position(player, 12, 8); 215 player_position(player, 12, 8);
230 player->speed = 2.f; // start with 2 tiles/sec 216 player->speed = 3.f; // start with 3 tiles/sec
231 asc_behavior_add(sprite, player_move); 217 asc_behavior_add(sprite, player_move);
232 return player; 218 return player;
233 } 219 }
234 220
235 static void gamefield_create() { 221 static void gamefield_create() {
236 // TODO: create a proper data structure and a more interesting map than just a basic grid 222 // TODO: create a proper data structure and a more interesting map than just a basic grid
237 AscSceneNode *gamefield = asc_scene_node_empty(); 223 AscSceneNode *gamefield = asc_scene_node_empty();
238 for (unsigned x = 1; x <= game_field_size; x++) { 224 for (unsigned x = 1; x <= game_field_size; x++) {
239 for (unsigned y = 1; y <= game_field_size; y++) { 225 for (unsigned y = 1; y <= game_field_size; y++) {
240 AscSceneNode *tile = asc_rectangle( 226 AscSceneNode *tile = asc_rectangle(
241 .x = x*game_field_tile_size, .y = y*game_field_tile_size, .filled = true, .thickness = 1, 227 .x = x*game_field_tile_size, .y = y*game_field_tile_size, .filled = true, .thickness = 2,
242 .origin_x = game_field_tile_size / 2, .origin_y = game_field_tile_size / 2, 228 .origin_x = game_field_tile_size / 2, .origin_y = game_field_tile_size / 2,
243 .width = game_field_tile_size, .height = game_field_tile_size, 229 .width = game_field_tile_size, .height = game_field_tile_size,
244 .color = ASC_RGB(0, 128, 255), 230 .color = ASC_RGB(16, 50, 160),
245 .border_color = ASC_RGB(64, 196, 255), 231 .border_color = ASC_RGB(20, 84, 128),
246 ); 232 );
247 asc_scene_node_link(gamefield, tile); 233 asc_scene_node_link(gamefield, tile);
248 } 234 }
249 } 235 }
250 asc_scene_node_set_zindex(gamefield, -2); 236 asc_scene_node_set_zindex(gamefield, -2);
321 .ortho.rect = ASC_RECT(0, 0, 307 .ortho.rect = ASC_RECT(0, 0,
322 (game_field_size+1)*game_field_tile_size, 308 (game_field_size+1)*game_field_tile_size,
323 (game_field_size+1)*game_field_tile_size 309 (game_field_size+1)*game_field_tile_size
324 ), 310 ),
325 .viewport_clear = true, 311 .viewport_clear = true,
326 .clear_color = ASC_RGB(0, 128, 90), 312 .clear_color = ASC_RGB(0, 32, 16),
327 .viewport_update_func = main_scene_viewport_update 313 .viewport_update_func = main_scene_viewport_update
328 ); 314 );
329 315
330 // backdrop for letterbox/pillarbox 316 // backdrop for letterbox/pillarbox
331 backdrop_create(); 317 backdrop_create();
333 // the game field 319 // the game field
334 gamefield_create(); 320 gamefield_create();
335 321
336 // create UI elements 322 // create UI elements
337 fps_counter_create(); 323 fps_counter_create();
338 score_counter_create();
339 324
340 // create spaceship 325 // create spaceship
341 Player *spaceship = player_create(); 326 Player *spaceship = player_create();
342 327
343 // Main Loop 328 // Main Loop

mercurial