Sun, 16 Nov 2025 23:02:11 +0100
make the camera follow the player + add limited vision
| 0 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * Copyright 2023 Mike Becker. All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions are met: | |
| 7 | * | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * | |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer in the | |
| 13 | * documentation and/or other materials provided with the distribution. | |
| 14 | * | |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 25 | * POSSIBILITY OF SUCH DAMAGE. | |
| 26 | */ | |
| 27 | ||
|
89
e1f682a8a145
use refcounted objects for textures instead of pass-by-value int structs
Mike Becker <universe@uap-core.de>
parents:
88
diff
changeset
|
28 | #include <ascension/core.h> |
| 48 | 29 | #include <ascension/ui.h> |
|
143
4db4f00493ad
prepare implementation of primitives
Mike Becker <universe@uap-core.de>
parents:
141
diff
changeset
|
30 | #include <ascension/sprite.h> |
|
151
42960d0c879b
adds first basic rectangle shader
Mike Becker <universe@uap-core.de>
parents:
149
diff
changeset
|
31 | #include <ascension/2d.h> |
| 242 | 32 | #include <ascension/shader.h> |
|
273
966bfca56b9d
add random spawn positions
Mike Becker <universe@uap-core.de>
parents:
272
diff
changeset
|
33 | #include <ascension/util.h> |
| 48 | 34 | |
|
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
35 | #include <cx/printf.h> |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
36 | #include <cx/linked_list.h> |
| 0 | 37 | |
| 242 | 38 | #define TEXTURE_2D_COUNT 3 |
| 39 | static AscTexture tex2d[TEXTURE_2D_COUNT]; | |
| 40 | #define TEXTURE_PLAYER &tex2d[0] | |
| 41 | #define TEXTURE_PLAYER_COLOR_MAP &tex2d[1] | |
| 42 | #define TEXTURE_BACKDROP &tex2d[2] | |
| 43 | ||
| 44 | #define SHADER_ID_PLAYER 1 | |
|
89
e1f682a8a145
use refcounted objects for textures instead of pass-by-value int structs
Mike Becker <universe@uap-core.de>
parents:
88
diff
changeset
|
45 | |
| 101 | 46 | #define BACKDROP_SCENE asc_window_scene(0) |
| 47 | #define MAIN_SCENE asc_window_scene(1) | |
|
238
e22abcd22e47
make the default window scaling and positioning screen resolution dependent
Mike Becker <universe@uap-core.de>
parents:
234
diff
changeset
|
48 | #define HUD_WIDTH 400 |
|
e22abcd22e47
make the default window scaling and positioning screen resolution dependent
Mike Becker <universe@uap-core.de>
parents:
234
diff
changeset
|
49 | |
| 206 | 50 | enum MoveDirection { |
| 51 | MOVE_UP, | |
| 52 | MOVE_LEFT, | |
| 53 | MOVE_DOWN, | |
| 54 | MOVE_RIGHT | |
| 55 | }; | |
| 56 | ||
| 57 | static asc_transform rotations[4]; | |
|
217
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
58 | static asc_vec2i directions[4]; |
| 206 | 59 | |
| 60 | typedef struct { | |
|
288
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
61 | AscSceneNode *node; |
|
256
60014484121c
remove the asc_col4i datatype in favor of a unified asc_color type
Mike Becker <universe@uap-core.de>
parents:
249
diff
changeset
|
62 | asc_color color; |
| 206 | 63 | enum MoveDirection direction; |
|
216
943980fa37b5
snap the movement to the grid
Mike Becker <universe@uap-core.de>
parents:
215
diff
changeset
|
64 | enum MoveDirection target_direction; |
| 213 | 65 | /** |
|
288
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
66 | * The number of tiles this player can look. |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
67 | */ |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
68 | float view_distance; |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
69 | /** |
| 213 | 70 | * The speed in tiles per second. |
| 71 | */ | |
| 72 | float speed; | |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
73 | /** |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
74 | * A linked list of vec2u elements describing the current trace. |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
75 | */ |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
76 | CxList *trace; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
77 | /** |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
78 | * The new position of the player when @c reset_position is @c true. |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
79 | */ |
|
274
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
80 | asc_vec2u new_position; |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
81 | unsigned health; |
|
274
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
82 | bool alive; |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
83 | bool reset_position; |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
84 | uint8_t number; |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
85 | } Player; |
| 206 | 86 | |
|
245
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
87 | #define GAME_FIELD_SIZE 32 |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
88 | #define GAME_FIELD_TILE_SIZE 32 |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
89 | |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
90 | /** The bit in the tile data indicating if the tile exists. */ |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
91 | #define GAME_FIELD_TILE_EXISTS_FLAG 0x80 |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
92 | /** The bits in the tile data that identify the owner. */ |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
93 | #define GAME_FIELD_TILE_OWNER_MASK 0xF |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
94 | |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
95 | typedef struct { |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
96 | AscSceneNode *nodes[GAME_FIELD_SIZE][GAME_FIELD_SIZE]; |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
97 | int8_t tile_data[GAME_FIELD_SIZE][GAME_FIELD_SIZE]; |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
98 | } GameField; |
| 213 | 99 | |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
100 | |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
101 | #define GAME_STATE_MENU 0 |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
102 | #define GAME_STATE_PLAYING 1 |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
103 | #define GAME_STATE_GAME_OVER 2 |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
104 | |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
105 | typedef struct { |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
106 | int state; |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
107 | GameField *field; |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
108 | Player *players[4]; |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
109 | } GameState; |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
110 | |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
111 | GameState game = {0}; |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
112 | |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
113 | static void globals_init(void) { |
| 206 | 114 | asc_transform_identity(rotations[MOVE_UP]); |
| 115 | asc_transform_roll(rotations[MOVE_LEFT], asc_rad(-90)); | |
| 116 | asc_transform_roll(rotations[MOVE_RIGHT], asc_rad(90)); | |
| 117 | asc_transform_roll(rotations[MOVE_DOWN], asc_rad(180)); | |
|
217
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
118 | directions[MOVE_UP] = ASC_VEC2I(0, -1); |
|
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
119 | directions[MOVE_LEFT] = ASC_VEC2I(-1, 0); |
|
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
120 | directions[MOVE_DOWN] = ASC_VEC2I(0, 1); |
|
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
121 | directions[MOVE_RIGHT] = ASC_VEC2I(1, 0); |
| 206 | 122 | } |
| 123 | ||
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
124 | static void textures_destroy(void) { |
| 242 | 125 | asc_texture_destroy(tex2d, TEXTURE_2D_COUNT); |
|
93
52611a99e574
add memory pool to gl context
Mike Becker <universe@uap-core.de>
parents:
89
diff
changeset
|
126 | } |
|
52611a99e574
add memory pool to gl context
Mike Becker <universe@uap-core.de>
parents:
89
diff
changeset
|
127 | |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
128 | static void textures_init(void) { |
| 242 | 129 | asc_texture_init_2d(tex2d, TEXTURE_2D_COUNT); |
|
238
e22abcd22e47
make the default window scaling and positioning screen resolution dependent
Mike Becker <universe@uap-core.de>
parents:
234
diff
changeset
|
130 | asc_texture_from_file(TEXTURE_PLAYER, "player.png"); |
| 242 | 131 | asc_texture_from_file(TEXTURE_PLAYER_COLOR_MAP, "player-color-map.png"); |
| 101 | 132 | asc_texture_from_file(TEXTURE_BACKDROP, "backdrop.png"); |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
133 | asc_gl_context_add_cleanup_func(asc_active_glctx, textures_destroy); |
|
89
e1f682a8a145
use refcounted objects for textures instead of pass-by-value int structs
Mike Becker <universe@uap-core.de>
parents:
88
diff
changeset
|
134 | } |
|
e1f682a8a145
use refcounted objects for textures instead of pass-by-value int structs
Mike Becker <universe@uap-core.de>
parents:
88
diff
changeset
|
135 | |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
136 | static void backdrop_scale(AscBehavior *behavior) { |
| 213 | 137 | // scale the backdrop to the size of the window |
|
244
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
138 | if (!asc_active_window->resized) return; |
|
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
139 | asc_ptr_cast(AscSprite, sprite, behavior->node); |
|
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
140 | asc_vec2u window_size = asc_active_window->dimensions; |
|
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
141 | asc_sprite_set_size(sprite, window_size); |
|
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
142 | } |
|
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
143 | |
|
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
144 | static void main_scene_frame_scale(AscBehavior *behavior) { |
|
262
b47de42f4598
update viewports of cameras before executing behaviors, so that the new viewport information is available in the behavior functions
Mike Becker <universe@uap-core.de>
parents:
260
diff
changeset
|
145 | if (!asc_active_window->resized) return; |
|
244
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
146 | asc_ptr_cast(AscRectangle, frame, behavior->node); |
|
287
359eaf2a8bd2
make camera independent of the scene
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
147 | asc_rectangle_set_bounds(frame, asc_scene_viewport(MAIN_SCENE)); |
| 213 | 148 | } |
| 149 | ||
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
150 | static void backdrop_create(void) { |
|
263
cc5cd4c98e46
fix that backdrop texture scale was not inverting the ui scale
Mike Becker <universe@uap-core.de>
parents:
262
diff
changeset
|
151 | const float scale = 1.f / asc_active_window->ui_scale; |
| 213 | 152 | AscSceneNode *node = asc_sprite( |
| 153 | .texture = TEXTURE_BACKDROP, | |
|
230
02090b2d147e
replace ugly backdrop texture
Mike Becker <universe@uap-core.de>
parents:
229
diff
changeset
|
154 | .texture_scale_mode = ASC_TEXTURE_SCALE_REPEAT, |
|
238
e22abcd22e47
make the default window scaling and positioning screen resolution dependent
Mike Becker <universe@uap-core.de>
parents:
234
diff
changeset
|
155 | .texture_scale_x = scale, .texture_scale_y = scale, |
| 213 | 156 | ); |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
157 | asc_behavior_add(node, .func = backdrop_scale); |
| 213 | 158 | asc_scene_add_node(BACKDROP_SCENE, node); |
|
244
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
159 | |
|
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
160 | // also add a frame for the main scene |
|
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
161 | // add this to the UI layer so that the border size does not scale |
|
256
60014484121c
remove the asc_col4i datatype in favor of a unified asc_color type
Mike Becker <universe@uap-core.de>
parents:
249
diff
changeset
|
162 | AscSceneNode *frame = asc_rectangle(.thickness = 2, .color = ASC_RGBi(66, 142, 161)); |
|
244
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
163 | asc_behavior_add(frame, .func = main_scene_frame_scale); |
|
ceab8a9f0366
add a frame to the main scene + implement the necessary asc_rectangle_set_bounds() function
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
164 | asc_ui_add_node(frame); |
| 213 | 165 | } |
| 166 | ||
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
167 | static void fps_counter_update(AscBehavior *behavior) { |
|
193
ddf690c0b36b
improves signature of asc_text_printf() and adds documentation
Mike Becker <universe@uap-core.de>
parents:
189
diff
changeset
|
168 | asc_ptr_cast(AscText, node, behavior->node); |
|
165
590a9e822b6b
add frame_rate and frame_factor to context
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
169 | static float last_fps = 0.f; |
|
590a9e822b6b
add frame_rate and frame_factor to context
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
170 | if (fabsf(asc_context.frame_rate - last_fps) > 1) { |
|
590a9e822b6b
add frame_rate and frame_factor to context
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
171 | last_fps = asc_context.frame_rate; |
|
590a9e822b6b
add frame_rate and frame_factor to context
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
172 | asc_text_printf(node, "%.2f FPS", asc_context.frame_rate); |
| 0 | 173 | } |
|
148
9f030f402699
implement interval for behaviors - fixes #383
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
174 | } |
|
9f030f402699
implement interval for behaviors - fixes #383
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
175 | |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
176 | static void fps_counter_tie_to_corner(AscBehavior *behavior) { |
|
148
9f030f402699
implement interval for behaviors - fixes #383
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
177 | // TODO: this should be replaced with some sort of UI layout manager |
|
9f030f402699
implement interval for behaviors - fixes #383
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
178 | AscSceneNode *node = behavior->node; |
|
9f030f402699
implement interval for behaviors - fixes #383
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
179 | if (asc_test_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED) || asc_active_window->resized) { |
|
240
5ed38debd4a7
redesign game field and make it larger (and the player faster)
Mike Becker <universe@uap-core.de>
parents:
238
diff
changeset
|
180 | asc_scene_node_set_position2f(node, ASC_VEC2F(10, |
|
5ed38debd4a7
redesign game field and make it larger (and the player faster)
Mike Becker <universe@uap-core.de>
parents:
238
diff
changeset
|
181 | asc_active_window->dimensions.y - ((AscText*)node)->dimension.height - 10 |
|
189
2c063b225183
remove separate vectors for position, rotation, scale from scene node
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
182 | )); |
|
37
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
183 | } |
|
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
184 | } |
|
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
185 | |
|
260
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
186 | static AscSceneNode *fps_counter_create(void) { |
|
214
9d460888a83e
remove global active font and color (also fixes #693)
Mike Becker <universe@uap-core.de>
parents:
213
diff
changeset
|
187 | AscSceneNode *node = asc_text( |
|
9d460888a83e
remove global active font and color (also fixes #693)
Mike Becker <universe@uap-core.de>
parents:
213
diff
changeset
|
188 | .name = "FPS Counter", |
|
256
60014484121c
remove the asc_col4i datatype in favor of a unified asc_color type
Mike Becker <universe@uap-core.de>
parents:
249
diff
changeset
|
189 | .color = ASC_RGB(1, 1, 1), |
|
214
9d460888a83e
remove global active font and color (also fixes #693)
Mike Becker <universe@uap-core.de>
parents:
213
diff
changeset
|
190 | .font = asc_font(ASC_FONT_REGULAR, 12), |
|
9d460888a83e
remove global active font and color (also fixes #693)
Mike Becker <universe@uap-core.de>
parents:
213
diff
changeset
|
191 | ); |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
192 | asc_behavior_add(node, .func = fps_counter_update, .interval = asc_seconds(1)); |
|
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
193 | asc_behavior_add(node, fps_counter_tie_to_corner); |
|
279
97a1a7fb4f1a
do not use different vocabulary (enable/disable vs. pause/unpause) for the behavior's enabled-state
Mike Becker <universe@uap-core.de>
parents:
278
diff
changeset
|
194 | asc_behavior_disable_all_while_hidden(node); |
|
155
b598b4eb4b44
add new ui.c where several UI functions are now moved to
Mike Becker <universe@uap-core.de>
parents:
154
diff
changeset
|
195 | asc_ui_add_node(node); |
|
260
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
196 | return node; |
| 70 | 197 | } |
| 198 | ||
|
275
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
199 | static void game_over_text_keep_centered(AscBehavior *behavior) { |
|
279
97a1a7fb4f1a
do not use different vocabulary (enable/disable vs. pause/unpause) for the behavior's enabled-state
Mike Becker <universe@uap-core.de>
parents:
278
diff
changeset
|
200 | if (!behavior->reactivated && !asc_active_window->resized) return; |
|
275
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
201 | |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
202 | AscSceneNode *node = behavior->node; |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
203 | // center the "game over" text in the game field viewport |
|
287
359eaf2a8bd2
make camera independent of the scene
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
204 | const asc_rect main_scene_viewport = asc_scene_viewport(MAIN_SCENE); |
|
275
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
205 | asc_scene_node_set_position2f(node, ASC_VEC2F( |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
206 | main_scene_viewport.pos.x + main_scene_viewport.size.x / 2.f, |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
207 | main_scene_viewport.pos.y + main_scene_viewport.size.y / 2.f - 36 |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
208 | )); |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
209 | } |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
210 | |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
211 | static AscSceneNode *game_over_create_text(void) { |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
212 | AscSceneNode *node = asc_text( |
|
278
634fa2996d4e
make behaviors aware of being unpaused
Mike Becker <universe@uap-core.de>
parents:
275
diff
changeset
|
213 | .name = "game_over_text", |
|
634fa2996d4e
make behaviors aware of being unpaused
Mike Becker <universe@uap-core.de>
parents:
275
diff
changeset
|
214 | .text = "Game Over\nPress R to Restart", |
|
634fa2996d4e
make behaviors aware of being unpaused
Mike Becker <universe@uap-core.de>
parents:
275
diff
changeset
|
215 | .color = ASC_RGB(1, 1, 1), |
|
634fa2996d4e
make behaviors aware of being unpaused
Mike Becker <universe@uap-core.de>
parents:
275
diff
changeset
|
216 | .font = asc_font(ASC_FONT_REGULAR, 36), |
|
634fa2996d4e
make behaviors aware of being unpaused
Mike Becker <universe@uap-core.de>
parents:
275
diff
changeset
|
217 | .alignment = ASC_TEXT_ALIGN_CENTER, |
|
634fa2996d4e
make behaviors aware of being unpaused
Mike Becker <universe@uap-core.de>
parents:
275
diff
changeset
|
218 | .centered = true, |
|
634fa2996d4e
make behaviors aware of being unpaused
Mike Becker <universe@uap-core.de>
parents:
275
diff
changeset
|
219 | ); |
|
275
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
220 | |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
221 | asc_scene_node_hide(node); |
|
279
97a1a7fb4f1a
do not use different vocabulary (enable/disable vs. pause/unpause) for the behavior's enabled-state
Mike Becker <universe@uap-core.de>
parents:
278
diff
changeset
|
222 | asc_behavior_add(node, game_over_text_keep_centered, .disable_while_hidden = true); |
|
275
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
223 | asc_ui_add_node(node); |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
224 | |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
225 | return node; |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
226 | } |
|
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
227 | |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
228 | static bool game_field_tile_chown(asc_vec2u coords, Player *player) { |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
229 | unsigned x = coords.x, y = coords.y; |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
230 | asc_ptr_cast(AscRectangle, tile, game.field->nodes[x][y]); |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
231 | int old_owner = game.field->tile_data[x][y] & GAME_FIELD_TILE_OWNER_MASK; |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
232 | if (player == NULL) { |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
233 | asc_clear_flag(game.field->tile_data[x][y], GAME_FIELD_TILE_OWNER_MASK); |
|
256
60014484121c
remove the asc_col4i datatype in favor of a unified asc_color type
Mike Becker <universe@uap-core.de>
parents:
249
diff
changeset
|
234 | tile->color = ASC_RGBi(16, 50, 160); |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
235 | } else { |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
236 | asc_set_flag_masked(game.field->tile_data[x][y], GAME_FIELD_TILE_OWNER_MASK, player->number); |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
237 | tile->color = player->color; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
238 | } |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
239 | int new_owner = game.field->tile_data[x][y] & GAME_FIELD_TILE_OWNER_MASK; |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
240 | return old_owner != new_owner; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
241 | } |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
242 | |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
243 | static void game_field_create() { |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
244 | // TODO: create a more interesting map than just a basic grid |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
245 | AscSceneNode *node = asc_scene_node_empty(); |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
246 | game.field = asc_scene_node_allocate_data(node, sizeof(GameField)); |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
247 | for (unsigned x = 0; x < GAME_FIELD_SIZE; x++) { |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
248 | for (unsigned y = 0; y < GAME_FIELD_SIZE; y++) { |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
249 | AscSceneNode *tile = asc_rectangle( |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
250 | .x = x*GAME_FIELD_TILE_SIZE, .y = y*GAME_FIELD_TILE_SIZE, .filled = true, .thickness = 2, |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
251 | .width = GAME_FIELD_TILE_SIZE, .height = GAME_FIELD_TILE_SIZE, |
|
256
60014484121c
remove the asc_col4i datatype in favor of a unified asc_color type
Mike Becker <universe@uap-core.de>
parents:
249
diff
changeset
|
252 | .color = ASC_RGBi(16, 50, 160), |
|
60014484121c
remove the asc_col4i datatype in favor of a unified asc_color type
Mike Becker <universe@uap-core.de>
parents:
249
diff
changeset
|
253 | .border_color = ASC_RGBi(20, 84, 128), |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
254 | ); |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
255 | |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
256 | game.field->tile_data[x][y] = GAME_FIELD_TILE_EXISTS_FLAG; |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
257 | game.field->nodes[x][y] = tile; |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
258 | |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
259 | asc_scene_node_link(node, tile); |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
260 | } |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
261 | } |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
262 | asc_scene_node_set_zindex(node, -2); |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
263 | asc_scene_add_node(MAIN_SCENE, node); |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
264 | } |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
265 | |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
266 | static asc_vec2u game_field_tile_at_position(asc_vec3f position) { |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
267 | return ASC_VEC2U((int)position.x / GAME_FIELD_TILE_SIZE, (int)position.y / GAME_FIELD_TILE_SIZE); |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
268 | } |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
269 | |
| 242 | 270 | typedef struct { |
| 271 | AscShaderProgram program; | |
| 272 | asc_uniform_loc map_albedo; | |
| 273 | asc_uniform_loc map_color; | |
| 274 | asc_uniform_loc color; | |
| 275 | } PlayerShader; | |
| 276 | ||
| 277 | static void player_shader_init(AscShaderProgram *p, cx_attr_unused int flags) { | |
|
270
3031d1bb5957
further improve names and docu of the uniform location init functions
Mike Becker <universe@uap-core.de>
parents:
269
diff
changeset
|
278 | asc_shader_set_uniform_loc_by_name(p, PlayerShader, map_albedo); |
|
3031d1bb5957
further improve names and docu of the uniform location init functions
Mike Becker <universe@uap-core.de>
parents:
269
diff
changeset
|
279 | asc_shader_set_uniform_loc_by_name(p, PlayerShader, map_color); |
|
3031d1bb5957
further improve names and docu of the uniform location init functions
Mike Becker <universe@uap-core.de>
parents:
269
diff
changeset
|
280 | asc_shader_set_uniform_loc_by_name(p, PlayerShader, color); |
| 242 | 281 | } |
| 282 | ||
| 283 | static AscShaderProgram *player_shader_create(cx_attr_unused int unused) { | |
| 284 | return asc_shader_create((AscShaderCodes) { | |
| 285 | .vtx = {.source_file = "sprite_vtx.glsl"}, | |
| 286 | .frag = {.source_file = "player.glsl",}, | |
| 287 | }, sizeof(PlayerShader), player_shader_init, 0); | |
| 288 | } | |
| 289 | ||
| 290 | static void player_draw(const AscCamera *camera, const AscSceneNode *node) { | |
| 291 | asc_cptr_cast(AscSprite, sprite, node); | |
| 292 | const Player *player = node->user_data; | |
| 293 | ||
| 294 | // TODO: we shall finally add the shader information to the node | |
| 295 | const AscShaderProgram *s = asc_shader_lookup( | |
| 296 | SHADER_ID_PLAYER, player_shader_create, 0 | |
| 297 | ); | |
| 298 | if (asc_shader_use(s, camera)) return; | |
| 299 | asc_cptr_cast(PlayerShader, shader, s); | |
| 300 | ||
| 301 | asc_shader_upload_model_matrix(s, node); | |
| 302 | ||
| 303 | // Bind texture | |
| 304 | asc_texture_bind(TEXTURE_PLAYER, shader->map_albedo, 0); | |
| 305 | asc_texture_bind(TEXTURE_PLAYER_COLOR_MAP, shader->map_color, 1); | |
|
256
60014484121c
remove the asc_col4i datatype in favor of a unified asc_color type
Mike Becker <universe@uap-core.de>
parents:
249
diff
changeset
|
306 | asc_shader_upload_color(shader->color, player->color); |
| 242 | 307 | asc_mesh_draw_triangle_strip(&sprite->mesh); |
| 308 | } | |
| 309 | ||
|
274
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
310 | static void player_position(Player *pl, unsigned x, unsigned y) { |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
311 | pl->new_position.x = x; |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
312 | pl->new_position.y = y; |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
313 | pl->reset_position = true; |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
314 | } |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
315 | |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
316 | static void player_position_random(Player *pl) { |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
317 | // TODO: check if the spawn location is viable when there is more action on the board |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
318 | player_position( |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
319 | pl, |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
320 | 4u + asc_util_rand(GAME_FIELD_SIZE - 8u), |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
321 | 4u + asc_util_rand(GAME_FIELD_SIZE - 8u) |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
322 | ); |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
323 | } |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
324 | |
|
274
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
325 | static void player_main_behavior(AscBehavior *behavior) { |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
326 | AscSceneNode *node = behavior->node; |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
327 | Player *player = node->user_data; |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
328 | |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
329 | if (player->alive && player->health == 0) { |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
330 | player->alive = false; |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
331 | asc_scene_node_hide(node); |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
332 | // TODO: probably we don't want the entire trace to disappear instantly |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
333 | cxListClear(player->trace); |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
334 | // TODO: this should be controlled by another behavior that watches all players |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
335 | game.state = GAME_STATE_GAME_OVER; |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
336 | } |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
337 | |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
338 | // TODO: replace with respawn event |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
339 | if (!player->alive && player->health > 0) { |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
340 | player_position_random(player); |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
341 | player->alive = true; |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
342 | asc_scene_node_show(node); |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
343 | } |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
344 | } |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
345 | |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
346 | static void player_move(AscBehavior *behavior) { |
| 206 | 347 | AscSceneNode *node = behavior->node; |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
348 | Player *player = node->user_data; |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
349 | |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
350 | const float ts = (float) GAME_FIELD_TILE_SIZE; |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
351 | |
|
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
352 | // check if the position is set programmatically |
|
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
353 | if (player->reset_position) { |
|
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
354 | asc_scene_node_set_position2f(node, |
|
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
355 | ASC_VEC2F( |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
356 | ts * (player->new_position.x + .5f), |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
357 | ts * (player->new_position.y + .5f) |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
358 | )); |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
359 | player->reset_position = false; |
|
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
360 | return; |
|
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
361 | } |
|
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
362 | |
|
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
363 | // normal movement |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
364 | const float speed = ts * player->speed * asc_context.frame_factor; |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
365 | const asc_vec2i dir = directions[player->direction]; |
|
217
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
366 | const asc_vec2f movement = asc_vec2f_scale(asc_vec2_itof(dir), speed); |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
367 | |
|
217
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
368 | // check if we are supposed to change the direction |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
369 | if (player->direction == player->target_direction) { |
|
217
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
370 | // move without changing the direction |
|
216
943980fa37b5
snap the movement to the grid
Mike Becker <universe@uap-core.de>
parents:
215
diff
changeset
|
371 | asc_scene_node_move2f(node, movement); |
|
943980fa37b5
snap the movement to the grid
Mike Becker <universe@uap-core.de>
parents:
215
diff
changeset
|
372 | } else { |
|
217
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
373 | // determine axis |
|
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
374 | // and check if we are about to cross the center |
|
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
375 | // this relies on positive positions! |
|
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
376 | bool rotate = false; |
|
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
377 | if (movement.x == 0) { |
|
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
378 | // vertical movement |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
379 | const float y_0 = floorf(node->position.y / ts); |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
380 | const float y_curr = node->position.y / ts - y_0; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
381 | const float y_next = (node->position.y+movement.y) / ts - y_0; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
382 | const bool side_curr = y_curr > 0.5f; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
383 | const bool side_next = y_next > 0.5f; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
384 | rotate = side_curr ^ side_next; |
|
217
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
385 | } else { |
|
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
386 | // horizontal movement |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
387 | const float x0 = floorf(node->position.x / ts); |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
388 | const float x_curr = node->position.x / ts - x0; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
389 | const float x_next = (node->position.x+movement.x) / ts - x0; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
390 | const bool side_curr = x_curr > 0.5f; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
391 | const bool side_next = x_next > 0.5f; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
392 | rotate = side_curr ^ side_next; |
|
217
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
393 | } |
|
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
394 | if (rotate) { |
|
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
395 | // snap position to the center of the tile |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
396 | asc_scene_node_set_position2f(node, |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
397 | ASC_VEC2F( |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
398 | (.5f+floorf(node->position.x / ts)) * ts, |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
399 | (.5f+floorf(node->position.y / ts)) * ts |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
400 | )); |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
401 | player->direction = player->target_direction; |
|
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
402 | asc_scene_node_set_rotation(node, rotations[player->direction]); |
|
216
943980fa37b5
snap the movement to the grid
Mike Becker <universe@uap-core.de>
parents:
215
diff
changeset
|
403 | } else { |
|
217
4b3c974eab44
improve snap-to-grid-movement
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
404 | // changing the direction not permitted, yet, continue movement |
|
216
943980fa37b5
snap the movement to the grid
Mike Becker <universe@uap-core.de>
parents:
215
diff
changeset
|
405 | asc_scene_node_move2f(node, movement); |
|
943980fa37b5
snap the movement to the grid
Mike Becker <universe@uap-core.de>
parents:
215
diff
changeset
|
406 | } |
|
943980fa37b5
snap the movement to the grid
Mike Becker <universe@uap-core.de>
parents:
215
diff
changeset
|
407 | } |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
408 | |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
409 | // die when leaving the game field |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
410 | if (node->position.x < 0 || node->position.y < 0 || |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
411 | node->position.x > GAME_FIELD_SIZE*GAME_FIELD_TILE_SIZE || |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
412 | node->position.y > GAME_FIELD_SIZE*GAME_FIELD_TILE_SIZE) { |
|
274
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
413 | // TODO: replace setting health to zero with a "kill" event |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
414 | player->health = 0; |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
415 | return; |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
416 | } |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
417 | |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
418 | // TODO: collision detection |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
419 | |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
420 | // update the trace, if necessary. |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
421 | // remark: some calculations are repeated here, but they are cheap enough |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
422 | { |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
423 | const asc_vec2u tile_coords = game_field_tile_at_position(node->position); |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
424 | if (tile_coords.x > GAME_FIELD_SIZE || tile_coords.y > GAME_FIELD_SIZE) return; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
425 | if (game_field_tile_chown(tile_coords, player)) { |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
426 | // new owner of the tile! |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
427 | asc_vec2u p = tile_coords; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
428 | cxListAdd(player->trace, &p); |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
429 | if (cxListSize(player->trace) > 7) { |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
430 | // TODO: implement power-up which makes the trace longer |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
431 | cxListRemove(player->trace, 0); |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
432 | } |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
433 | } |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
434 | } |
|
205
d1e44c861426
hack a quick example for both rotation directions
Mike Becker <universe@uap-core.de>
parents:
204
diff
changeset
|
435 | } |
|
d1e44c861426
hack a quick example for both rotation directions
Mike Becker <universe@uap-core.de>
parents:
204
diff
changeset
|
436 | |
|
272
6ed9fb9662c0
move player controls into a behavior
Mike Becker <universe@uap-core.de>
parents:
271
diff
changeset
|
437 | static void player_controls(AscBehavior *behavior) { |
|
6ed9fb9662c0
move player controls into a behavior
Mike Becker <universe@uap-core.de>
parents:
271
diff
changeset
|
438 | Player *player = behavior->node->user_data; |
|
6ed9fb9662c0
move player controls into a behavior
Mike Becker <universe@uap-core.de>
parents:
271
diff
changeset
|
439 | // TODO: different key bindings for different player number in hot seat mode |
|
260
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
440 | if (asc_key_pressed(ASC_KEY(LEFT))) { |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
441 | if (player->direction != MOVE_RIGHT) { |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
442 | player->target_direction = MOVE_LEFT; |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
443 | } |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
444 | } |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
445 | if (asc_key_pressed(ASC_KEY(RIGHT))) { |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
446 | if (player->direction != MOVE_LEFT) { |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
447 | player->target_direction = MOVE_RIGHT; |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
448 | } |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
449 | } |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
450 | if (asc_key_pressed(ASC_KEY(UP))) { |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
451 | if (player->direction != MOVE_DOWN) { |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
452 | player->target_direction = MOVE_UP; |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
453 | } |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
454 | } |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
455 | if (asc_key_pressed(ASC_KEY(DOWN))) { |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
456 | if (player->direction != MOVE_UP) { |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
457 | player->target_direction = MOVE_DOWN; |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
458 | } |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
459 | } |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
460 | } |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
461 | |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
462 | static void player_destroy(CxAllocator *allocator, Player *player) { |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
463 | cxListFree(player->trace); |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
464 | cxFree(allocator, player); |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
465 | } |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
466 | |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
467 | static void player_trace_release_tile(asc_vec2u *coords) { |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
468 | game_field_tile_chown(*coords, NULL); |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
469 | } |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
470 | |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
471 | static Player *player_create(void) { |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
472 | AscSceneNode *node = asc_sprite( |
| 114 | 473 | .name = "Player", |
|
245
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
474 | .width = GAME_FIELD_TILE_SIZE, |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
475 | .height = GAME_FIELD_TILE_SIZE, |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
476 | .origin_x = GAME_FIELD_TILE_SIZE / 2, |
|
0cc396ade3b0
add a simple struct for the game field that may be extended later
Mike Becker <universe@uap-core.de>
parents:
244
diff
changeset
|
477 | .origin_y = GAME_FIELD_TILE_SIZE / 2, |
|
88
6234b7ea48f3
add support for 2d textures in sprite shader - fixes #386
Mike Becker <universe@uap-core.de>
parents:
86
diff
changeset
|
478 | ); |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
479 | asc_scene_add_node(MAIN_SCENE, node); |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
480 | Player *player = asc_scene_node_allocate_data(node, sizeof(Player)); |
|
288
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
481 | player->node = node; |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
482 | player->view_distance = 7.f; // start with 7 tiles |
|
240
5ed38debd4a7
redesign game field and make it larger (and the player faster)
Mike Becker <universe@uap-core.de>
parents:
238
diff
changeset
|
483 | player->speed = 3.f; // start with 3 tiles/sec |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
484 | player->number = 1; |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
485 | player->health = 100; |
|
256
60014484121c
remove the asc_col4i datatype in favor of a unified asc_color type
Mike Becker <universe@uap-core.de>
parents:
249
diff
changeset
|
486 | player->color = ASC_RGB(1, 0, 0); |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
487 | player->trace = cxLinkedListCreateSimple(sizeof(asc_vec2u)); |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
488 | cxDefineDestructor(player->trace, player_trace_release_tile); |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
489 | node->draw_func = player_draw; |
|
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
490 | node->user_data_free_func = (cx_destructor_func2)player_destroy; |
|
274
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
491 | |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
492 | // add behaviors |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
493 | asc_behavior_add(node, player_main_behavior, .always_enabled = true); |
|
272
6ed9fb9662c0
move player controls into a behavior
Mike Becker <universe@uap-core.de>
parents:
271
diff
changeset
|
494 | asc_behavior_add(node, player_controls); |
|
247
3547254742a7
add player's trace and removes the origin offsets from the game field's tiles
Mike Becker <universe@uap-core.de>
parents:
245
diff
changeset
|
495 | asc_behavior_add(node, player_move); |
|
279
97a1a7fb4f1a
do not use different vocabulary (enable/disable vs. pause/unpause) for the behavior's enabled-state
Mike Becker <universe@uap-core.de>
parents:
278
diff
changeset
|
496 | asc_behavior_disable_all_while_hidden(node); |
|
274
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
497 | |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
498 | return player; |
|
88
6234b7ea48f3
add support for 2d textures in sprite shader - fixes #386
Mike Becker <universe@uap-core.de>
parents:
86
diff
changeset
|
499 | } |
|
6234b7ea48f3
add support for 2d textures in sprite shader - fixes #386
Mike Becker <universe@uap-core.de>
parents:
86
diff
changeset
|
500 | |
|
288
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
501 | static void main_scene_viewport_update(AscCamera *camera, asc_vec2u window_size) { |
|
233
bda74199223f
handle the edge-case when a viewport vanishes
Mike Becker <universe@uap-core.de>
parents:
231
diff
changeset
|
502 | // margins |
|
238
e22abcd22e47
make the default window scaling and positioning screen resolution dependent
Mike Becker <universe@uap-core.de>
parents:
234
diff
changeset
|
503 | const unsigned margin = 16; |
|
233
bda74199223f
handle the edge-case when a viewport vanishes
Mike Becker <universe@uap-core.de>
parents:
231
diff
changeset
|
504 | |
|
bda74199223f
handle the edge-case when a viewport vanishes
Mike Becker <universe@uap-core.de>
parents:
231
diff
changeset
|
505 | // space for score, power-ups, etc. |
|
238
e22abcd22e47
make the default window scaling and positioning screen resolution dependent
Mike Becker <universe@uap-core.de>
parents:
234
diff
changeset
|
506 | const unsigned left_area = (unsigned) (asc_active_window->ui_scale*HUD_WIDTH); |
|
229
5c05b5164a16
first idea of what the main scene could look like
Mike Becker <universe@uap-core.de>
parents:
227
diff
changeset
|
507 | |
|
233
bda74199223f
handle the edge-case when a viewport vanishes
Mike Becker <universe@uap-core.de>
parents:
231
diff
changeset
|
508 | // calculate how many pixels need to be removed from width and height |
|
bda74199223f
handle the edge-case when a viewport vanishes
Mike Becker <universe@uap-core.de>
parents:
231
diff
changeset
|
509 | const unsigned rw = 2*margin + left_area; |
|
bda74199223f
handle the edge-case when a viewport vanishes
Mike Becker <universe@uap-core.de>
parents:
231
diff
changeset
|
510 | const unsigned rh = 2*margin; |
|
bda74199223f
handle the edge-case when a viewport vanishes
Mike Becker <universe@uap-core.de>
parents:
231
diff
changeset
|
511 | |
|
bda74199223f
handle the edge-case when a viewport vanishes
Mike Becker <universe@uap-core.de>
parents:
231
diff
changeset
|
512 | // check if there is still a viewport left and chicken out when not |
|
bda74199223f
handle the edge-case when a viewport vanishes
Mike Becker <universe@uap-core.de>
parents:
231
diff
changeset
|
513 | if (window_size.width < rw || window_size.height < rh) { |
|
288
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
514 | camera->viewport = ASC_RECT(0, 0, 0, 0); |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
515 | return; |
|
233
bda74199223f
handle the edge-case when a viewport vanishes
Mike Becker <universe@uap-core.de>
parents:
231
diff
changeset
|
516 | } |
|
bda74199223f
handle the edge-case when a viewport vanishes
Mike Becker <universe@uap-core.de>
parents:
231
diff
changeset
|
517 | window_size.width -= rw; |
|
bda74199223f
handle the edge-case when a viewport vanishes
Mike Becker <universe@uap-core.de>
parents:
231
diff
changeset
|
518 | window_size.height -= rh; |
|
229
5c05b5164a16
first idea of what the main scene could look like
Mike Becker <universe@uap-core.de>
parents:
227
diff
changeset
|
519 | |
|
99
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
520 | // Compute scaling and offsets |
| 105 | 521 | unsigned viewport_size, offset_x = 0, offset_y = 0; |
|
99
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
522 | if (window_size.width > window_size.height) { |
|
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
523 | // Wider window: letterbox (black bars on top/bottom) |
|
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
524 | offset_x = (window_size.width - window_size.height) / 2; |
|
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
525 | viewport_size = window_size.height; |
|
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
526 | } else { |
|
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
527 | // Taller window: pillarbox (black bars on sides) |
|
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
528 | offset_y = (window_size.height - window_size.width) / 2; |
|
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
529 | viewport_size = window_size.width; |
|
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
530 | } |
|
229
5c05b5164a16
first idea of what the main scene could look like
Mike Becker <universe@uap-core.de>
parents:
227
diff
changeset
|
531 | offset_x += left_area + margin; |
|
5c05b5164a16
first idea of what the main scene could look like
Mike Becker <universe@uap-core.de>
parents:
227
diff
changeset
|
532 | offset_y += margin; |
|
99
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
533 | |
|
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
534 | // Set the viewport to the scaled and centered region |
|
288
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
535 | camera->viewport = ASC_RECT(offset_x, offset_y, viewport_size, viewport_size); |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
536 | } |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
537 | |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
538 | static void main_scene_camera_update(AscCamera *camera) { |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
539 | // follow the player |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
540 | // TODO: for hot seat / split screen updates we need to attach custom data to the camera |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
541 | // s.t. the camera knows which player to follow |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
542 | const Player *player = game.players[0]; |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
543 | |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
544 | const AscSceneNode *player_node = player->node; |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
545 | const float view_distance = player->view_distance; |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
546 | const float cam_view_distance = (1+view_distance) * GAME_FIELD_TILE_SIZE; |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
547 | const float proj_size = 2 * cam_view_distance; |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
548 | |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
549 | // TODO: create camera API to avoid direct access to the matrices |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
550 | |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
551 | // update the projection matrix |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
552 | asc_mat4f_ortho_update_size(camera->projection, proj_size, proj_size); |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
553 | |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
554 | // update the view matrix |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
555 | asc_vec2f pos = asc_scene_node_get_position2f(player_node); |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
556 | pos.x = -pos.x + cam_view_distance; |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
557 | pos.y = -pos.y + cam_view_distance; |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
558 | asc_transform_translate2f(camera->view, pos); |
|
99
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
559 | } |
|
ac143db979dc
add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
560 | |
| 105 | 561 | int main(void) { |
| 0 | 562 | asc_context_initialize(); |
|
33
e7ddb52facd3
add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
563 | if (asc_has_error()) { |
|
e7ddb52facd3
add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
564 | SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, |
|
89
e1f682a8a145
use refcounted objects for textures instead of pass-by-value int structs
Mike Becker <universe@uap-core.de>
parents:
88
diff
changeset
|
565 | "Fatal Error",asc_get_error(), NULL); |
|
33
e7ddb52facd3
add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
566 | return 1; |
|
e7ddb52facd3
add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
567 | } |
| 0 | 568 | |
| 206 | 569 | // initialize globals |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
570 | globals_init(); |
| 206 | 571 | |
|
231
0da563c4e39c
make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents:
230
diff
changeset
|
572 | // create the window |
|
257
67d7b79997df
remove AscWindowSettings struct
Mike Becker <universe@uap-core.de>
parents:
256
diff
changeset
|
573 | asc_window_initialize(0, asc_gl_context_settings_default(4, 0)); |
|
231
0da563c4e39c
make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents:
230
diff
changeset
|
574 | asc_window_set_title(0, "Snake"); |
|
238
e22abcd22e47
make the default window scaling and positioning screen resolution dependent
Mike Becker <universe@uap-core.de>
parents:
234
diff
changeset
|
575 | asc_window_set_size(0, asc_vec2_ftou( |
|
287
359eaf2a8bd2
make camera independent of the scene
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
576 | asc_vec2f_scale(ASC_VEC2F(600+HUD_WIDTH, 600), asc_ui_scale_auto()))); |
|
238
e22abcd22e47
make the default window scaling and positioning screen resolution dependent
Mike Becker <universe@uap-core.de>
parents:
234
diff
changeset
|
577 | asc_window_center(0); |
|
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
578 | |
| 101 | 579 | // load textures |
|
234
a1d70b8018c1
improve structure of the game code and add a function to set the player position
Mike Becker <universe@uap-core.de>
parents:
233
diff
changeset
|
580 | textures_init(); |
| 101 | 581 | |
|
263
cc5cd4c98e46
fix that backdrop texture scale was not inverting the ui scale
Mike Becker <universe@uap-core.de>
parents:
262
diff
changeset
|
582 | // initialize backdrop scene |
|
287
359eaf2a8bd2
make camera independent of the scene
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
583 | AscCamera backdrop_camera; |
|
359eaf2a8bd2
make camera independent of the scene
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
584 | asc_camera_init(&backdrop_camera, |
| 101 | 585 | .type = ASC_CAMERA_ORTHO, |
|
288
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
586 | .viewport_update_func = asc_camera_ortho_update_size |
|
121
ede9a9e92ff9
add viewport_clear flag to camera settings
Mike Becker <universe@uap-core.de>
parents:
118
diff
changeset
|
587 | ); |
|
287
359eaf2a8bd2
make camera independent of the scene
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
588 | asc_scene_init(BACKDROP_SCENE, "backdrop", &backdrop_camera); |
|
263
cc5cd4c98e46
fix that backdrop texture scale was not inverting the ui scale
Mike Becker <universe@uap-core.de>
parents:
262
diff
changeset
|
589 | backdrop_create(); |
|
cc5cd4c98e46
fix that backdrop texture scale was not inverting the ui scale
Mike Becker <universe@uap-core.de>
parents:
262
diff
changeset
|
590 | |
|
287
359eaf2a8bd2
make camera independent of the scene
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
591 | // Initialize the main scene |
|
359eaf2a8bd2
make camera independent of the scene
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
592 | AscCamera main_camera; |
|
359eaf2a8bd2
make camera independent of the scene
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
593 | asc_camera_init(&main_camera, |
|
100
5231da78831e
change asc_scene_init() to also request parameters for camera initialization
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
594 | .type = ASC_CAMERA_ORTHO, |
|
121
ede9a9e92ff9
add viewport_clear flag to camera settings
Mike Becker <universe@uap-core.de>
parents:
118
diff
changeset
|
595 | .viewport_clear = true, |
|
256
60014484121c
remove the asc_col4i datatype in favor of a unified asc_color type
Mike Becker <universe@uap-core.de>
parents:
249
diff
changeset
|
596 | .clear_color = ASC_RGBi(0, 32, 16), |
|
288
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
597 | .viewport_update_func = main_scene_viewport_update, |
|
8796f03aac26
make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
598 | .update_func = main_scene_camera_update |
|
121
ede9a9e92ff9
add viewport_clear flag to camera settings
Mike Becker <universe@uap-core.de>
parents:
118
diff
changeset
|
599 | ); |
|
287
359eaf2a8bd2
make camera independent of the scene
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
600 | asc_scene_init(MAIN_SCENE, "main", &main_camera); |
|
89
e1f682a8a145
use refcounted objects for textures instead of pass-by-value int structs
Mike Becker <universe@uap-core.de>
parents:
88
diff
changeset
|
601 | |
|
260
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
602 | // create the fps counter |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
603 | AscSceneNode *fps_counter = fps_counter_create(); |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
604 | asc_scene_node_hide(fps_counter); |
| 213 | 605 | |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
606 | // create game over text |
|
275
2256af1440db
move game over text to the UI scene
Mike Becker <universe@uap-core.de>
parents:
274
diff
changeset
|
607 | AscSceneNode *text_game_over = game_over_create_text(); |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
608 | |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
609 | // initialize the game state |
|
260
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
610 | // TODO: add a main menu and start with the menu |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
611 | game.state = GAME_STATE_PLAYING; |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
612 | game.players[0] = player_create(); |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
613 | game_field_create(); |
|
88
6234b7ea48f3
add support for 2d textures in sprite shader - fixes #386
Mike Becker <universe@uap-core.de>
parents:
86
diff
changeset
|
614 | |
|
33
e7ddb52facd3
add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
615 | // Main Loop |
|
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
616 | do { |
| 0 | 617 | // quit application on any error |
|
33
e7ddb52facd3
add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
618 | if (asc_has_error()) { |
|
e7ddb52facd3
add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
619 | SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, |
|
65
9c44c55d327a
consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
620 | "Fatal Error", asc_get_error(), |
|
9c44c55d327a
consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
621 | asc_active_window->window); |
|
33
e7ddb52facd3
add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
622 | asc_clear_error(); |
|
63
e3cacdd636e4
implement mouse motion and key press events
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
623 | asc_context_quit(); |
|
e3cacdd636e4
implement mouse motion and key press events
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
624 | } |
|
e3cacdd636e4
implement mouse motion and key press events
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
625 | |
|
260
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
626 | // game states |
|
272
6ed9fb9662c0
move player controls into a behavior
Mike Becker <universe@uap-core.de>
parents:
271
diff
changeset
|
627 | // TODO: move this into a behavior - probably add a state machine that enables/disables behaviors |
|
6ed9fb9662c0
move player controls into a behavior
Mike Becker <universe@uap-core.de>
parents:
271
diff
changeset
|
628 | if (game.state == GAME_STATE_GAME_OVER) { |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
629 | asc_scene_node_show(text_game_over); |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
630 | if (asc_key_pressed(ASC_KEY(R))) { |
|
274
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
631 | // TODO: instead of setting the health, send a "respawn" event to the behavior |
|
ba7f043f9fdf
move the player's life "controller" to a behavior
Mike Becker <universe@uap-core.de>
parents:
273
diff
changeset
|
632 | game.players[0]->health = 100; |
|
265
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
633 | // TODO: re-load the "level" |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
634 | game.state = GAME_STATE_PLAYING; |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
635 | asc_scene_node_hide(text_game_over); |
|
5c915d01bdc0
add simple game over screen
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
636 | } |
|
200
cf0579d3bbc4
add rotation functions, but rotation with special point of origin is still broken
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
637 | } |
|
cf0579d3bbc4
add rotation functions, but rotation with special point of origin is still broken
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
638 | |
|
140
d190fe5315bd
add dynamic reload of all shaders
Mike Becker <universe@uap-core.de>
parents:
123
diff
changeset
|
639 | // debug-key for clearing the shader registry |
|
141
cd82643bb6d9
implement edge-triggered key press/release
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
640 | if (asc_key_pressed(ASC_KEY(S))) { |
|
140
d190fe5315bd
add dynamic reload of all shaders
Mike Becker <universe@uap-core.de>
parents:
123
diff
changeset
|
641 | asc_shader_clear_registry(); |
|
d190fe5315bd
add dynamic reload of all shaders
Mike Becker <universe@uap-core.de>
parents:
123
diff
changeset
|
642 | } |
|
d190fe5315bd
add dynamic reload of all shaders
Mike Becker <universe@uap-core.de>
parents:
123
diff
changeset
|
643 | |
|
260
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
644 | // show/hide the FPS counter |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
645 | if (asc_key_pressed(ASC_KEY(F2))) { |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
646 | asc_scene_node_toggle_visibility(fps_counter); |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
647 | } |
|
2649d04f0287
start giving the game code more structure
Mike Becker <universe@uap-core.de>
parents:
257
diff
changeset
|
648 | |
|
63
e3cacdd636e4
implement mouse motion and key press events
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
649 | // quit application on ESC key press |
|
141
cd82643bb6d9
implement edge-triggered key press/release
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
650 | if (asc_key_pressed(ASC_KEY(ESCAPE))) { |
|
63
e3cacdd636e4
implement mouse motion and key press events
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
651 | asc_context_quit(); |
|
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
652 | } |
|
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
653 | } while (asc_loop_next()); |
|
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
654 | |
|
89
e1f682a8a145
use refcounted objects for textures instead of pass-by-value int structs
Mike Becker <universe@uap-core.de>
parents:
88
diff
changeset
|
655 | // cleanup |
| 0 | 656 | asc_context_destroy(); |
| 657 | return 0; | |
| 658 | } | |
| 659 |