Changeset 708 for soft/giet_vm/applications/raycast/game.c
- Timestamp:
- Oct 1, 2015, 4:09:25 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/applications/raycast/game.c
r683 r708 3 3 #include "ctrl.h" 4 4 #include <math.h> 5 6 // Globals 5 #include <stdio.h> 6 7 ////////////////////////////// 8 // Game Maps 9 ////////////////////////////// 7 10 8 11 static Map map[] = … … 70 73 }; 71 74 72 static Game game = 73 { 74 .mapId = 0 75 }; 76 77 static bool g_exit; 78 75 //////////////////////////// 76 // extern variables 77 //////////////////////////// 78 79 extern Game game; 80 81 ////////////////////// 79 82 // Local functions 80 83 ////////////////////// 84 85 //////////////////////////////////// 81 86 static void gameOnBlockHit(int type) 82 87 { 83 g_exit = true; 84 } 85 88 giet_tty_printf("\n[RAYCAST] Fatal collision, killed...\n"); 89 game.exit = 1; 90 } 91 92 /////////////////////////////////////////////// 86 93 static void gameCollision(float opx, float opy) 87 94 { 88 static bool collided_x = false;89 static bool collided_y = false;90 91 float px = game.player.x;92 float py = game.player.y;93 int fpx = floor(px);94 int fpy = floor(py);95 bool colliding_x = false;96 bool colliding_y = false;97 int collide_type_x = 0;98 int collide_type_y = 0;99 int type;95 static unsigned int collided_x = 0; 96 static unsigned int collided_y = 0; 97 98 float px = game.player.x; 99 float py = game.player.y; 100 int fpx = floor(px); 101 int fpy = floor(py); 102 unsigned int colliding_x = 0; 103 unsigned int colliding_y = 0; 104 int collide_type_x = 0; 105 int collide_type_y = 0; 106 int type; 100 107 101 108 // Check for x axis collisions 102 if ((type = gameLocate(floor(px + COLLIDE_GAP), fpy))) { 103 colliding_x = true, collide_type_x = type; 109 if ((type = gameLocate(floor(px + COLLIDE_GAP), fpy))) 110 { 111 colliding_x = 1, collide_type_x = type; 104 112 game.player.x = fpx - COLLIDE_GAP + 1; 105 113 } 106 else if ((type = gameLocate(floor(px - COLLIDE_GAP), fpy))) { 107 colliding_x = true, collide_type_x = type; 114 else if ((type = gameLocate(floor(px - COLLIDE_GAP), fpy))) 115 { 116 colliding_x = 1, collide_type_x = type; 108 117 game.player.x = fpx + COLLIDE_GAP; 109 118 } 110 119 111 120 // Check for y axis collisions 112 if ((type = gameLocate(fpx, floor(py + COLLIDE_GAP)))) { 113 colliding_y = true, collide_type_y = type; 121 if ((type = gameLocate(fpx, floor(py + COLLIDE_GAP)))) 122 { 123 colliding_y = 1, collide_type_y = type; 114 124 game.player.y = fpy - COLLIDE_GAP + 1; 115 125 } 116 else if ((type = gameLocate(fpx, floor(py - COLLIDE_GAP)))) { 117 colliding_y = true, collide_type_y = type; 126 else if ((type = gameLocate(fpx, floor(py - COLLIDE_GAP)))) 127 { 128 colliding_y = 1, collide_type_y = type; 118 129 game.player.y = fpy + COLLIDE_GAP; 119 130 } 120 131 121 132 // Check if we're inside a wall 122 if ((type = gameLocate(fpx, fpy))) { 123 colliding_x = true, collide_type_x = type; 124 colliding_y = true, collide_type_y = type; 125 game.player.x = opx, game.player.y = opy; 133 if ((type = gameLocate(fpx, fpy))) 134 { 135 colliding_x = 1 , collide_type_x = type; 136 colliding_y = 1 , collide_type_y = type; 137 game.player.x = opx , game.player.y = opy; 126 138 } 127 139 … … 136 148 } 137 149 138 static void gameLogic() 139 { 140 float opx = game.player.x; 141 float opy = game.player.y; 142 143 ctrlLogic(&game); 144 gameCollision(opx, opy); 145 } 146 147 static void gameInitMap() 148 { 149 game.map = &map[game.mapId]; 150 game.player.x = game.map->startX; 151 game.player.y = game.map->startY; 150 ///////////////////////// 151 // Exported functions 152 ///////////////////////// 153 154 /////////////// 155 void gameInit() 156 { 157 game.mapId = 0; 158 game.map = &map[0]; 159 game.player.x = game.map->startX; 160 game.player.y = game.map->startY; 152 161 game.player.dir = game.map->startDir; 153 game.timeLeft = TIME_TOTAL; 154 } 155 156 // Exported functions 157 162 game.timeLeft = TIME_TOTAL; 163 game.exit = 0; 164 } 165 166 ////////////////// 167 void gameControl() 168 { 169 // Only six commands are accepted: 170 // - key Q : quit game 171 // - key UP : forward move 172 // - key DOWN : backward move 173 // - key RIGHT : right move 174 // - key LEFT : left move 175 // - any other key : continue 176 // several moves can be made before continue 177 178 char c; 179 unsigned int state = 0; 180 unsigned int done = 0; 181 182 // display prompt 183 giet_tty_printf("\n[RAYCAST] > " ); 184 185 // get one command 186 while ( done == 0 ) 187 { 188 // get one character 189 giet_tty_getc( &c ); 190 191 if (state == 0) // first character 192 { 193 if (c == 0x1B) // ESCAPE : possible player move 194 { 195 state = 1; 196 } 197 else if (c == 0x71) // quit game 198 { 199 game.exit = 1; 200 done = 1; 201 giet_tty_printf("QUIT\n"); 202 } 203 else // continue 204 { 205 done = 1; 206 giet_tty_printf("\n"); 207 } 208 } 209 else if (state == 1) // previous character was ESCAPE 210 { 211 if (c == 0x5B) // BRAKET : possible player move 212 { 213 state = 2; 214 } 215 else // continue 216 { 217 done = 1; 218 giet_tty_printf("\n"); 219 } 220 } 221 else // previous characters were ESCAPE,BRACKET 222 { 223 if (c == 0x41) // UP arrow <=> forward move 224 { 225 game.player.x += PLAYER_MOVE * cos(game.player.dir); 226 game.player.y += PLAYER_MOVE * sin(game.player.dir); 227 giet_tty_printf("GO "); 228 state = 0; 229 } 230 else if (c == 0x42) // DOWN arrow <=> backward move 231 { 232 game.player.x -= PLAYER_MOVE * cos(game.player.dir); 233 game.player.y -= PLAYER_MOVE * sin(game.player.dir); 234 giet_tty_printf("BACK "); 235 state = 0; 236 } 237 else if (c == 0x43) // RIGHT arrow <=> turn right 238 { 239 game.player.dir += PLAYER_ROT; 240 giet_tty_printf("RIGHT "); 241 state = 0; 242 } 243 else if (c == 0x44) // LEFT arrow <=> turn left 244 { 245 game.player.dir -= PLAYER_ROT; 246 giet_tty_printf("LEFT "); 247 state = 0; 248 } 249 else // continue 250 { 251 done = 1; 252 giet_tty_printf("\n"); 253 } 254 } 255 } // end while 256 257 // check new position 258 int hit = gameLocate( game.player.x , game.player.y ); 259 if ( hit ) gameOnBlockHit( hit ); 260 } 261 262 //////////////////////////// 158 263 int gameLocate(int x, int y) 159 264 { 160 265 if ((x < 0 || x >= game.map->w) || 161 (y < 0 || y >= game.map->h)) { 266 (y < 0 || y >= game.map->h)) 267 { 162 268 // Outside the map bounds 163 269 return 1; … … 167 273 } 168 274 169 void gameRun() 170 { 171 gameInitMap(); 172 173 g_exit = false; 174 175 // Game loop 176 while (!g_exit) { 177 gameLogic(); 178 dispRender(&game); 179 } 180 181 if (game.timeLeft == 0) { 182 // Time's up! 183 game.mapId = 0; 184 } 185 else { 186 // Go to next map 187 game.mapId++; 188 } 189 } 190 275 /////////////// 191 276 void gameTick() 192 277 { 193 278 game.timeLeft--; 194 279 195 if (game.timeLeft == 0) { 196 g_exit = true; 197 } 198 } 199 200 Game *gameInstance() 201 { 202 return &game; 203 } 280 if (game.timeLeft == 0) 281 { 282 game.exit = 1; 283 } 284 } 285
Note: See TracChangeset
for help on using the changeset viewer.