source: soft/giet_vm/applications/raycast/game.c @ 710

Last change on this file since 710 was 710, checked in by alain, 9 years ago

Remove the unused "ctrl.h" include in game.c

File size: 7.6 KB
Line 
1#include "game.h"
2#include "disp.h"
3#include <math.h>
4#include <stdio.h>
5
6//////////////////////////////
7// Game Maps
8//////////////////////////////
9
10static Map map[] =
11{
12    { // map0
13        .tile =
14        {
15            {1, 0, 0, 0, 0, 1, 0, 0, 1, 1},
16            {0, 0, 0, 0, 0, 1, 0, 0, 0, 2},
17            {0, 0, 0, 0, 1, 0, 0, 0, 1, 1},
18            {0, 0, 0, 1, 3, 0, 3, 0, 0, 0},
19            {0, 0, 0, 1, 3, 0, 3, 0, 0, 1},
20            {0, 0, 0, 1, 3, 0, 3, 0, 0, 0},
21            {0, 0, 0, 1, 1, 0, 3, 0, 0, 1},
22            {4, 0, 0, 0, 0, 0, 1, 0, 0, 0},
23            {4, 0, 0, 0, 0, 0, 1, 0, 0, 1},
24            {0, 4, 4, 4, 4, 0, 0, 0, 1, 0}
25        },
26        .w = 10,
27        .h = 10,
28        .startX = 2.f,
29        .startY = 3.f,
30        .startDir = 70.f * M_PI / 180.f
31    },
32    { // map1
33        .tile =
34        {
35            {0, 1, 0, 1, 0, 3, 0, 0, 0, 0},
36            {0, 1, 0, 0, 0, 3, 0, 0, 0, 0},
37            {0, 0, 0, 1, 0, 3, 0, 0, 0, 0},
38            {1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
39            {4, 2, 4, 1, 3, 0, 3, 3, 3, 0},
40            {4, 0, 0, 1, 3, 3, 3, 0, 0, 0},
41            {4, 0, 0, 1, 3, 0, 0, 0, 3, 3},
42            {4, 0, 0, 0, 0, 0, 4, 0, 0, 3},
43            {4, 0, 0, 0, 0, 0, 4, 0, 0, 0},
44            {4, 4, 4, 4, 4, 4, 4, 0, 1, 0}
45        },
46        .w = 10,
47        .h = 10,
48        .startX = 0.5f,
49        .startY = 0.5f,
50        .startDir = 90.f * M_PI / 180.f
51    },
52    { // map2
53        .tile =
54        {
55            {4, 4, 4, 4, 4, 4, 4, 0, 0, 0},
56            {0, 0, 0, 0, 0, 0, 0, 0, 3, 0},
57            {3, 0, 0, 0, 4, 4, 4, 0, 0, 0},
58            {3, 0, 0, 4, 0, 0, 0, 1, 1, 0},
59            {3, 0, 4, 2, 0, 0, 0, 0, 0, 0},
60            {3, 0, 4, 2, 0, 0, 0, 0, 0, 0},
61            {3, 0, 0, 4, 0, 0, 0, 1, 1, 0},
62            {3, 0, 0, 0, 4, 4, 4, 0, 0, 0},
63            {0, 0, 0, 0, 0, 0, 0, 0, 3, 0},
64            {4, 4, 4, 4, 4, 4, 4, 0, 0, 0}
65        },
66        .w = 10,
67        .h = 10,
68        .startX = 4.5f,
69        .startY = 5.f,
70        .startDir = 0.f * M_PI / 180.f
71    },
72};
73
74////////////////////////////
75// extern variables
76////////////////////////////
77
78extern Game         game;
79
80//////////////////////
81// Local functions
82//////////////////////
83
84////////////////////////////////////
85static void gameOnBlockHit(int type)
86{
87    giet_tty_printf("\n[RAYCAST] Fatal collision, killed...\n");
88    game.exit = 1;
89}
90
91///////////////////////////////////////////////
92static void gameCollision(float opx, float opy)
93{
94    static unsigned int collided_x = 0;
95    static unsigned int collided_y = 0;
96
97    float               px = game.player.x;
98    float               py = game.player.y;
99    int                 fpx = floor(px);
100    int                 fpy = floor(py);
101    unsigned int        colliding_x = 0;
102    unsigned int        colliding_y = 0;
103    int                 collide_type_x = 0;
104    int                 collide_type_y = 0;
105    int                 type;
106
107    // Check for x axis collisions
108    if      ((type = gameLocate(floor(px + COLLIDE_GAP), fpy))) 
109    {
110        colliding_x = 1, collide_type_x = type;
111        game.player.x = fpx - COLLIDE_GAP + 1;
112    }
113    else if ((type = gameLocate(floor(px - COLLIDE_GAP), fpy))) 
114    {
115        colliding_x = 1, collide_type_x = type;
116        game.player.x = fpx + COLLIDE_GAP;
117    }
118
119    // Check for y axis collisions
120    if      ((type = gameLocate(fpx, floor(py + COLLIDE_GAP)))) 
121    {
122        colliding_y = 1, collide_type_y = type;
123        game.player.y = fpy - COLLIDE_GAP + 1;
124    }
125    else if ((type = gameLocate(fpx, floor(py - COLLIDE_GAP)))) 
126    {
127        colliding_y = 1, collide_type_y = type;
128        game.player.y = fpy + COLLIDE_GAP;
129    }
130
131    // Check if we're inside a wall
132    if ((type = gameLocate(fpx, fpy))) 
133    {
134        colliding_x   = 1   , collide_type_x = type;
135        colliding_y   = 1   , collide_type_y = type;
136        game.player.x = opx , game.player.y = opy;
137    }
138
139    // Take action when the player hits a block
140    if (colliding_x && !collided_x)
141        gameOnBlockHit(collide_type_x);
142    if (colliding_y && !collided_y)
143        gameOnBlockHit(collide_type_y);
144
145    collided_x = colliding_x;
146    collided_y = colliding_y;
147}
148
149/////////////////////////
150// Exported functions
151/////////////////////////
152
153///////////////
154void gameInit()
155{
156    game.mapId      = 0;
157    game.map        = &map[0];
158    game.player.x   = game.map->startX;
159    game.player.y   = game.map->startY;
160    game.player.dir = game.map->startDir;
161    game.timeLeft   = TIME_TOTAL;
162    game.exit       = 0;
163}
164
165//////////////////
166void gameControl()
167{
168    // Only six commands are accepted:
169    // - key Q         : quit game
170    // - key UP        : forward move
171    // - key DOWN      : backward move
172    // - key RIGHT     : right move
173    // - key LEFT      : left move
174    // - any other key : continue
175    // several moves can be made before continue
176
177    char c;
178    unsigned int state = 0;
179    unsigned int done  = 0;
180
181    // display prompt
182    giet_tty_printf("\n[RAYCAST] > " );
183
184    // get one command
185    while ( done == 0 )
186    {
187        // get one character       
188        giet_tty_getc( &c );
189
190        if (state == 0) // first character
191        {
192            if      (c == 0x1B)         // ESCAPE : possible player move 
193            {
194                state = 1;
195            }
196            else if (c == 0x71)         // quit game
197            {
198                game.exit = 1;
199                done = 1;
200                giet_tty_printf("QUIT\n");
201            }
202            else                        // continue
203            {
204                done = 1;
205                giet_tty_printf("\n");
206            }
207        } 
208        else if (state == 1) // previous character was ESCAPE
209        {
210            if      (c == 0x5B)        // BRAKET : possible player move
211            {
212                state = 2;
213            }
214            else                       // continue
215            {
216                done = 1;
217                giet_tty_printf("\n");
218            }
219        }
220        else  // previous characters were ESCAPE,BRACKET
221        {
222            if      (c == 0x41)        // UP arrow <=> forward move
223            {
224                game.player.x += PLAYER_MOVE * cos(game.player.dir);
225                game.player.y += PLAYER_MOVE * sin(game.player.dir);
226                giet_tty_printf("GO ");
227                state = 0;
228            }
229            else if (c == 0x42)        // DOWN arrow <=> backward move
230            {
231                game.player.x -= PLAYER_MOVE * cos(game.player.dir);
232                game.player.y -= PLAYER_MOVE * sin(game.player.dir);
233                giet_tty_printf("BACK ");
234                state = 0;
235            }
236            else if (c == 0x43)        // RIGHT arrow <=> turn right     
237            {
238                game.player.dir += PLAYER_ROT;
239                giet_tty_printf("RIGHT ");
240                state = 0;
241            }
242            else if (c == 0x44)        // LEFT arrow <=> turn left     
243            {
244                game.player.dir -= PLAYER_ROT;
245                giet_tty_printf("LEFT ");
246                state = 0;
247            }
248            else                       // continue
249            {
250                done = 1;
251                giet_tty_printf("\n");
252            }
253        }
254    }  // end while
255   
256    // check new position
257    int hit = gameLocate( game.player.x , game.player.y );
258    if ( hit )  gameOnBlockHit( hit );
259}
260
261////////////////////////////
262int gameLocate(int x, int y)
263{
264    if ((x < 0 || x >= game.map->w) ||
265        (y < 0 || y >= game.map->h)) 
266    {
267        // Outside the map bounds
268        return 1;
269    }
270
271    return game.map->tile[y][x];
272}
273
274///////////////
275void gameTick()
276{
277    game.timeLeft--;
278
279    if (game.timeLeft == 0) 
280    {
281        game.exit = 1;
282    }
283}
284
Note: See TracBrowser for help on using the repository browser.