Majority of codebase refactor complete.

This commit is contained in:
Slipyx 2018-07-18 02:24:38 -04:00
parent 352b6f481a
commit e91c1f170d
6 changed files with 301 additions and 268 deletions

230
game.c
View File

@ -1,4 +1,5 @@
#include "game.h"
#include "world.h"
// global game state
game_state_t* gs = NULL;
@ -42,50 +43,6 @@ static void G_InitAssets( SDL_Renderer* r ) {
Util_FreeTileSurfaces();
}
// sets player position to current level's player start
void P_Spawn() {
// reset view
gs->view_x = 0;
gs->view_y = 0;
gs->scroll_x = 0;
// reset player jump
gs->ps.jump_timer = 0;
gs->ps.on_ground = 0;
gs->ps.do_jump = 0;
// hardcoded player starts
switch ( gs->current_level ) {
case 0: case 4: case 5: case 7: case 9: gs->ps.tx = 2; gs->ps.ty = 8; break;
case 1: gs->ps.tx = 1; gs->ps.ty = 8; break;
case 2: gs->ps.tx = 2; gs->ps.ty = 5; break;
case 3: gs->ps.tx = 1; gs->ps.ty = 5; break;
case 6: gs->ps.tx = 1; gs->ps.ty = 2; break;
case 8: gs->ps.tx = 6; gs->ps.ty = 1; break;
default: break;
}
gs->ps.px = gs->ps.tx * TILE_SIZE;
gs->ps.py = gs->ps.ty * TILE_SIZE;
}
// sets new beginning state for current level
void W_StartLevel() {
P_Spawn();
// reset items
gs->ps.gun = 0;
gs->ps.jetpack = 0;
gs->ps.trophy = 0;
gs->ps.check_door = 0;
}
// hard reset current level from original data
void W_ResetLevel() {
Util_GetLevel( gs->current_level, &gs->levels[gs->current_level] );
W_StartLevel();
}
// poll input
static void G_CheckInput() {
SDL_Event ev;
@ -135,190 +92,6 @@ static void G_ClearInput() {
gs->ps.try_down = 0;
}
// pickup functionality and remove from world
void P_PickupItem() {
uint8_t tx = gs->ps.check_pickup_x;
uint8_t ty = gs->ps.check_pickup_y;
if ( !tx || !ty ) return;
uint8_t til = gs->levels[gs->current_level].tiles[ty * 100 + tx];
// pickup functionality here
if ( til == 4 ) gs->ps.jetpack = 0xff;
if ( til == 10 ) {
gs->ps.score += 1000;
gs->ps.trophy = 1;
}
if ( til == 20 ) gs->ps.gun = 1;
// remove
gs->levels[gs->current_level].tiles[ty * 100 + tx] = 0;
gs->ps.check_pickup_x = 0;
gs->ps.check_pickup_y = 0;
}
// returns 1 if passed pixel point is not within a solid tile
uint8_t W_IsClear( uint16_t px, uint16_t py ) {
uint8_t tx, ty; // tile pos
uint8_t til; // tile index
// pixel point to tile pos
tx = px / TILE_SIZE; ty = py / TILE_SIZE;
// tile index at level's tx, ty pos
til = gs->levels[gs->current_level].tiles[ty * 100 + tx];
// solid tiles
if ( til == 1 || til == 3 || til == 5 ) return 0;
if ( til >= 15 && til <= 19 ) return 0;
if ( til >= 21 && til <= 24 ) return 0;
if ( til >= 29 && til <= 30 ) return 0;
// kill tiles
if ( til == 6 || til == 25 || til == 36 ) {
P_Spawn();
}
// pickups
if ( til == 10 || til == 4 || til == 20 || (til >= 47 && til <= 52) ) {
gs->ps.check_pickup_x = tx;
gs->ps.check_pickup_y = ty;
}
// door
if ( til == 2 ) {
gs->ps.check_door = 1;
}
return 1;
}
// update collision point clear flags
void P_UpdateCollision() {
// 8 points of collision; relative to top left of tile 56 neutral frame (20x16)
// 0, 1 = top left, top right
gs->ps.col_point[0] = W_IsClear( gs->ps.px + 4, gs->ps.py - 0 );
gs->ps.col_point[1] = W_IsClear( gs->ps.px + 10, gs->ps.py - 0 );
// 2, 3 = right edge
gs->ps.col_point[2] = W_IsClear( gs->ps.px + 12, gs->ps.py + 2 );
gs->ps.col_point[3] = W_IsClear( gs->ps.px + 12, gs->ps.py + 14 );
// 4, 5 = bottom edge
gs->ps.col_point[4] = W_IsClear( gs->ps.px + 10, gs->ps.py + 16 );
gs->ps.col_point[5] = W_IsClear( gs->ps.px + 4, gs->ps.py + 16 );
// 6, 7 = left edge
gs->ps.col_point[6] = W_IsClear( gs->ps.px + 2, gs->ps.py + 14 );
gs->ps.col_point[7] = W_IsClear( gs->ps.px + 2, gs->ps.py + 2 );
// update on_ground flag if a bottom point (4,5) is not clear
gs->ps.on_ground = (!gs->ps.col_point[4] || !gs->ps.col_point[5]);
}
// validate input whose try flags were set
void P_VerifyInput() {
// right; col points 2, 3
if ( gs->ps.try_right && gs->ps.col_point[2] && gs->ps.col_point[3] ) {
gs->ps.do_right = 1;
}
// left; col points 6, 7
if ( gs->ps.try_left && gs->ps.col_point[6] && gs->ps.col_point[7] ) {
gs->ps.do_left = 1;
}
// jump; on_ground and col points 0, 1
if ( gs->ps.try_jump && gs->ps.on_ground && !gs->ps.do_jump
&& (gs->ps.col_point[0] && gs->ps.col_point[1]) ) {
gs->ps.do_jump = 1;
}
// reset jump timer if contact a ground while still "jumping"
if ( gs->ps.try_jump && gs->ps.on_ground && gs->ps.jump_timer )
gs->ps.jump_timer = 0;
}
// apply validated player movement
void P_Move() {
// update player's tile pos
// sample x towards the center
gs->ps.tx = (gs->ps.px + TILE_SIZE / 2) / TILE_SIZE;
gs->ps.ty = gs->ps.py / TILE_SIZE;
if ( gs->ps.do_right ) {
gs->ps.px += 2;
gs->ps.do_right = 0;
}
if ( gs->ps.do_left ) {
gs->ps.px -= 2;
gs->ps.do_left = 0;
}
if ( gs->ps.do_jump ) {
if ( !gs->ps.jump_timer )
gs->ps.jump_timer = 25;
if ( gs->ps.col_point[0] && gs->ps.col_point[1] ) {
if ( gs->ps.jump_timer > 12 )
gs->ps.py -= 2;
if ( gs->ps.jump_timer >= 7 && gs->ps.jump_timer <= 12 )
gs->ps.py -= 1;
gs->ps.jump_timer--;
} else gs->ps.jump_timer = 0;
//gs->ps.jump_timer--;
if ( !gs->ps.jump_timer )
gs->ps.do_jump = 0;
}
}
// apply gravity to player
void P_ApplyGravity() {
if ( !gs->ps.do_jump && !gs->ps.on_ground ) {
// check below sprite
if ( W_IsClear( gs->ps.px + 4, gs->ps.py + 17 ) && W_IsClear( gs->ps.px + 10, gs->ps.py + 17 ) )
gs->ps.py += 2;
else { // align to tile
uint8_t not_align = gs->ps.py % TILE_SIZE;
if ( not_align ) {
gs->ps.py = not_align < (TILE_SIZE / 2) ?
gs->ps.py - not_align : gs->ps.py + TILE_SIZE - not_align;
}
}
}
}
// level-wide state update
void W_Update() {
// check if at door and has trophy
if ( gs->ps.check_door ) {
if ( gs->ps.trophy ) {
if ( gs->current_level < 9 ) {
gs->current_level++;
W_StartLevel();
} else {
// finshed level 10
gs->quit = 1;
}
} else { // no trophy
gs->ps.check_door = 0;
}
}
}
// update game view based on set scroll values
void W_ScrollView() {
// scroll view if dave is about to move off view
if ( gs->ps.tx - gs->view_x >= 18 )
gs->scroll_x = 15;
if ( gs->ps.tx - gs->view_x < 2 )
gs->scroll_x = -15;
// do the scroll
if ( gs->scroll_x > 0 ) {
if ( gs->view_x == 80 ) gs->scroll_x = 0;
else { gs->view_x++; gs->scroll_x--; }
}
if ( gs->scroll_x < 0 ) {
if ( gs->view_x == 0 ) gs->scroll_x = 0;
else { gs->view_x--; gs->scroll_x++; }
}
}
// main update routine
static void G_Update() {
// update collision point flags
@ -475,4 +248,3 @@ int main( int argc, char** argv ) {
return EXIT_SUCCESS;
}

42
game.h
View File

@ -6,42 +6,7 @@
// for dealing with resources in EXE
#include "util/util.h"
// dave player state
typedef struct {
uint8_t tx, ty; // tile pos
uint16_t px, py; // pixel pos
uint16_t score;
uint8_t lives;
// on ground flag
uint8_t on_ground;
// input flags
uint8_t try_right;
uint8_t try_left;
uint8_t try_jump;
uint8_t try_fire;
uint8_t try_jetpack;
uint8_t try_up, try_down; // jetpack/climbing
uint8_t do_right;
uint8_t do_left;
uint8_t do_jump;
uint8_t do_fire;
uint8_t do_jetpack;
uint8_t do_up, do_down;
uint8_t jump_timer;
// pickup tile pos
uint8_t check_pickup_x;
uint8_t check_pickup_y;
// door was hit, check if passable
uint8_t check_door;
// item flags; jetpack is also fuel count
uint8_t trophy, gun, jetpack;
// collision point clear flags; 1 = clear
uint8_t col_point[8];
} player_state_t;
#include "player.h"
// global game state
typedef struct {
@ -64,9 +29,8 @@ typedef struct {
} game_assets_t;
// level tile size in pixels
const uint8_t TILE_SIZE = 16;
#define TILE_SIZE 16
// fixed frame delay
const uint8_t FRAME_DELAY = 33;
#define FRAME_DELAY 33
#endif

143
player.c Normal file
View File

@ -0,0 +1,143 @@
#include "game.h"
#include "world.h"
extern game_state_t* gs;
// sets player position to current level's player start
void P_Spawn() {
// reset view
gs->view_x = 0;
gs->view_y = 0;
gs->scroll_x = 0;
// reset player jump
gs->ps.jump_timer = 0;
gs->ps.on_ground = 0;
gs->ps.do_jump = 0;
// hardcoded player starts
switch ( gs->current_level ) {
case 0: case 4: case 5: case 7: case 9: gs->ps.tx = 2; gs->ps.ty = 8; break;
case 1: gs->ps.tx = 1; gs->ps.ty = 8; break;
case 2: gs->ps.tx = 2; gs->ps.ty = 5; break;
case 3: gs->ps.tx = 1; gs->ps.ty = 5; break;
case 6: gs->ps.tx = 1; gs->ps.ty = 2; break;
case 8: gs->ps.tx = 6; gs->ps.ty = 1; break;
default: break;
}
gs->ps.px = gs->ps.tx * TILE_SIZE;
gs->ps.py = gs->ps.ty * TILE_SIZE;
}
// pickup functionality and remove from world
void P_PickupItem() {
uint8_t tx = gs->ps.check_pickup_x;
uint8_t ty = gs->ps.check_pickup_y;
if ( !tx || !ty ) return;
uint8_t til = gs->levels[gs->current_level].tiles[ty * 100 + tx];
// pickup functionality here
if ( til == 4 ) gs->ps.jetpack = 0xff;
if ( til == 10 ) {
gs->ps.score += 1000;
gs->ps.trophy = 1;
}
if ( til == 20 ) gs->ps.gun = 1;
// remove
gs->levels[gs->current_level].tiles[ty * 100 + tx] = 0;
gs->ps.check_pickup_x = 0;
gs->ps.check_pickup_y = 0;
}
// update collision point clear flags
void P_UpdateCollision() {
// 8 points of collision; relative to top left of tile 56 neutral frame (20x16)
// 0, 1 = top left, top right
gs->ps.col_point[0] = W_IsClear( gs->ps.px + 4, gs->ps.py - 0 );
gs->ps.col_point[1] = W_IsClear( gs->ps.px + 10, gs->ps.py - 0 );
// 2, 3 = right edge
gs->ps.col_point[2] = W_IsClear( gs->ps.px + 12, gs->ps.py + 2 );
gs->ps.col_point[3] = W_IsClear( gs->ps.px + 12, gs->ps.py + 14 );
// 4, 5 = bottom edge
gs->ps.col_point[4] = W_IsClear( gs->ps.px + 10, gs->ps.py + 16 );
gs->ps.col_point[5] = W_IsClear( gs->ps.px + 4, gs->ps.py + 16 );
// 6, 7 = left edge
gs->ps.col_point[6] = W_IsClear( gs->ps.px + 2, gs->ps.py + 14 );
gs->ps.col_point[7] = W_IsClear( gs->ps.px + 2, gs->ps.py + 2 );
// update on_ground flag if a bottom point (4,5) is not clear
gs->ps.on_ground = (!gs->ps.col_point[4] || !gs->ps.col_point[5]);
}
// validate input whose try flags were set
void P_VerifyInput() {
// right; col points 2, 3
if ( gs->ps.try_right && gs->ps.col_point[2] && gs->ps.col_point[3] ) {
gs->ps.do_right = 1;
}
// left; col points 6, 7
if ( gs->ps.try_left && gs->ps.col_point[6] && gs->ps.col_point[7] ) {
gs->ps.do_left = 1;
}
// jump; on_ground and col points 0, 1
if ( gs->ps.try_jump && gs->ps.on_ground && !gs->ps.do_jump
&& (gs->ps.col_point[0] && gs->ps.col_point[1]) ) {
gs->ps.do_jump = 1;
}
// reset jump timer if contact a ground while still "jumping"
if ( gs->ps.try_jump && gs->ps.on_ground && gs->ps.jump_timer )
gs->ps.jump_timer = 0;
}
// apply validated player movement
void P_Move() {
// update player's tile pos
// sample x towards the center
gs->ps.tx = (gs->ps.px + TILE_SIZE / 2) / TILE_SIZE;
gs->ps.ty = gs->ps.py / TILE_SIZE;
if ( gs->ps.do_right ) {
gs->ps.px += 2;
gs->ps.do_right = 0;
}
if ( gs->ps.do_left ) {
gs->ps.px -= 2;
gs->ps.do_left = 0;
}
if ( gs->ps.do_jump ) {
if ( !gs->ps.jump_timer )
gs->ps.jump_timer = 25;
if ( gs->ps.col_point[0] && gs->ps.col_point[1] ) {
if ( gs->ps.jump_timer > 12 )
gs->ps.py -= 2;
if ( gs->ps.jump_timer >= 7 && gs->ps.jump_timer <= 12 )
gs->ps.py -= 1;
gs->ps.jump_timer--;
} else gs->ps.jump_timer = 0;
//gs->ps.jump_timer--;
if ( !gs->ps.jump_timer )
gs->ps.do_jump = 0;
}
}
// apply gravity to player
void P_ApplyGravity() {
if ( !gs->ps.do_jump && !gs->ps.on_ground ) {
// check below sprite
if ( W_IsClear( gs->ps.px + 4, gs->ps.py + 17 ) && W_IsClear( gs->ps.px + 10, gs->ps.py + 17 ) )
gs->ps.py += 2;
else { // align to tile
uint8_t not_align = gs->ps.py % TILE_SIZE;
if ( not_align ) {
gs->ps.py = not_align < (TILE_SIZE / 2) ?
gs->ps.py - not_align : gs->ps.py + TILE_SIZE - not_align;
}
}
}
}

49
player.h Normal file
View File

@ -0,0 +1,49 @@
// player state and movement
#ifndef __PLAYER_H
#define __PLAYER_H
// player state
typedef struct {
uint8_t tx, ty; // tile pos
uint16_t px, py; // pixel pos
uint16_t score;
uint8_t lives;
// on ground flag
uint8_t on_ground;
// input flags
uint8_t try_right;
uint8_t try_left;
uint8_t try_jump;
uint8_t try_fire;
uint8_t try_jetpack;
uint8_t try_up, try_down; // jetpack/climbing
uint8_t do_right;
uint8_t do_left;
uint8_t do_jump;
uint8_t do_fire;
uint8_t do_jetpack;
uint8_t do_up, do_down;
uint8_t jump_timer;
// pickup tile pos
uint8_t check_pickup_x;
uint8_t check_pickup_y;
// door was hit, check if passable
uint8_t check_door;
// item flags; jetpack is also fuel count
uint8_t trophy, gun, jetpack;
// collision point clear flags; 1 = clear
uint8_t col_point[8];
} player_state_t;
void P_Spawn();
void P_PickupItem();
void P_UpdateCollision();
void P_VerifyInput();
void P_Move();
void P_ApplyGravity();
#endif

94
world.c Normal file
View File

@ -0,0 +1,94 @@
#include "game.h"
#include "world.h"
//#include "player.h"
extern game_state_t* gs;
// sets new beginning state for current level
void W_StartLevel() {
P_Spawn();
// reset items
gs->ps.gun = 0;
gs->ps.jetpack = 0;
gs->ps.trophy = 0;
gs->ps.check_door = 0;
}
// hard reset current level from original data
void W_ResetLevel() {
Util_GetLevel( gs->current_level, &gs->levels[gs->current_level] );
W_StartLevel();
}
// returns 1 if passed pixel point is not within a solid tile
uint8_t W_IsClear( uint16_t px, uint16_t py ) {
uint8_t tx, ty; // tile pos
uint8_t til; // tile index
// pixel point to tile pos
tx = px / TILE_SIZE; ty = py / TILE_SIZE;
// tile index at level's tx, ty pos
til = gs->levels[gs->current_level].tiles[ty * 100 + tx];
// solid tiles
if ( til == 1 || til == 3 || til == 5 ) return 0;
if ( til >= 15 && til <= 19 ) return 0;
if ( til >= 21 && til <= 24 ) return 0;
if ( til >= 29 && til <= 30 ) return 0;
// kill tiles
if ( til == 6 || til == 25 || til == 36 ) {
P_Spawn();
}
// pickups
if ( til == 10 || til == 4 || til == 20 || (til >= 47 && til <= 52) ) {
gs->ps.check_pickup_x = tx;
gs->ps.check_pickup_y = ty;
}
// door
if ( til == 2 ) {
gs->ps.check_door = 1;
}
return 1;
}
// level-wide state update
void W_Update() {
// check if at door and has trophy
if ( gs->ps.check_door ) {
if ( gs->ps.trophy ) {
if ( gs->current_level < 9 ) {
gs->current_level++;
W_StartLevel();
} else {
// finshed level 10
gs->quit = 1;
}
} else { // no trophy
gs->ps.check_door = 0;
}
}
}
// update game view based on set scroll values
void W_ScrollView() {
// scroll view if dave is about to move off view
if ( gs->ps.tx - gs->view_x >= 18 )
gs->scroll_x = 15;
if ( gs->ps.tx - gs->view_x < 2 )
gs->scroll_x = -15;
// do the scroll
if ( gs->scroll_x > 0 ) {
if ( gs->view_x == 80 ) gs->scroll_x = 0;
else { gs->view_x++; gs->scroll_x--; }
}
if ( gs->scroll_x < 0 ) {
if ( gs->view_x == 0 ) gs->scroll_x = 0;
else { gs->view_x--; gs->scroll_x++; }
}
}

11
world.h Normal file
View File

@ -0,0 +1,11 @@
// world/level state
#ifndef __WORLD_H
#define __WORLD_H
void W_StartLevel();
void W_ResetLevel();
uint8_t W_IsClear( uint16_t px, uint16_t py );
void W_Update();
void W_ScrollView();
#endif