Skip to content

Commit

Permalink
Refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
commandblockguy committed Feb 2, 2021
1 parent de1970c commit 139d168
Show file tree
Hide file tree
Showing 48 changed files with 915 additions and 918 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ add_executable(dummy
src/level.h
src/main.cpp
src/util/util.cpp
src/util/util.h src/objects/tank.cpp src/objects/tank.h src/objects/shell.cpp src/objects/shell.h src/objects/mine.cpp src/objects/mine.h src/globals.cpp src/globals.h src/util/profiler.c src/util/profiler.h src/graphics/gui.cpp src/graphics/gui.h src/graphics/partial_redraw.cpp src/graphics/partial_redraw.h src/graphics/dynamic_sprites.cpp src/graphics/dynamic_sprites.h src/ai/ai_state.h src/objects/physicsbody.cpp src/objects/physicsbody.h src/input.cpp src/input.h src/fwd.h src/util/trig.cpp src/util/trig.h src/util/cpp_internals.cpp src/physics/physics.h src/game.cpp src/game.h src/objects/mine_detector.cpp src/objects/mine_detector.h src/util/debug_malloc.h src/graphics/tiles.cpp src/graphics/tiles.h src/graphics/repalettize.cpp src/graphics/repalettize.h src/graphics/tank_sprite.cpp src/graphics/tank_sprite.h)
src/util/util.h src/objects/tank.cpp src/objects/tank.h src/objects/shell.cpp src/objects/shell.h src/objects/mine.cpp src/objects/mine.h src/util/profiler.c src/util/profiler.h src/graphics/partial_redraw.cpp src/graphics/partial_redraw.h src/graphics/dynamic_sprites.cpp src/graphics/dynamic_sprites.h src/ai/ai_state.h src/objects/physicsbody.cpp src/objects/physicsbody.h src/input.cpp src/input.h src/fwd.h src/util/trig.cpp src/util/trig.h src/util/cpp_internals.cpp src/physics/physics.h src/game.cpp src/game.h src/objects/mine_detector.cpp src/objects/mine_detector.h src/util/debug_malloc.h src/graphics/tiles.cpp src/graphics/tiles.h src/graphics/repalettize.cpp src/graphics/repalettize.h src/graphics/tank_sprite.cpp src/graphics/tank_sprite.h src/gui/error.cpp src/gui/error.h src/gui/pause.cpp src/gui/pause.h src/gui/kill_counts.cpp src/gui/kill_counts.h src/gui/transition.cpp src/gui/transition.h src/gui/banner.cpp src/gui/banner.h src/gui/aim_indicator.cpp src/gui/aim_indicator.h)
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ Developed using the [CE C Toolchain](https://github.com/CE-Programming/toolchain
The [C libraries](https://github.com/CE-Programming/libraries/releases/latest) are required to run this program.

As this project is still in alpha, GitHub releases may be significantly behind until I get the final release out.

Feel free to ask questions or roast my terrible code quality in the project's [Cemetech thread](https://www.cemetech.net/forum/viewtopic.php?t=14718).
26 changes: 13 additions & 13 deletions src/ai/ai.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "ai.h"

#include "../util/profiler.h"
#include "../globals.h"
#include "../game.h"

void ai_process_move(Tank *tank) {
profiler_add(ai_move);
Expand Down Expand Up @@ -85,7 +85,7 @@ void move_random(Tank *tank) {
}
}
tank->tread_rot += randInt(0, DEGREES_TO_ANGLE(6)) - DEGREES_TO_ANGLE(3);
tank->set_velocity(Tank::velocities[tank->type]);
tank->set_velocity(tank->velocity());
profiler_end(ai_move_random);
}

Expand Down Expand Up @@ -119,7 +119,7 @@ void aim_random(Tank *tank) {
//todo: add some visualizations as I have absolutely no idea wtf is going on here
//it worked well in my head, okay?
void aim_reflect(Tank *tank) {
ai_fire_reflect_state_t *ai = &tank->ai_fire.reflect;
struct ai_fire_reflect_state *ai = &tank->ai_fire.reflect;
if(!tank->can_shoot()) return;
//Loop through all X values, then all Y values
if(tank->ai_fire.reflect.scan_dir == AXIS_X) {
Expand All @@ -128,7 +128,7 @@ void aim_reflect(Tank *tank) {
ai->scan_pos = 1;
ai->scan_dir = AXIS_Y;
point_at_player(tank, game.player);
if(pointing_at_target(tank, game.player, Tank::max_bounces[tank->type], false)) {
if(pointing_at_target(tank, game.player, tank->max_bounces(), false)) {
tank->fire_shell();
}
return; //I know this kinda skips a tick but whatever
Expand All @@ -140,7 +140,7 @@ void aim_reflect(Tank *tank) {
if(left != (game.player->center_x() < rX)) return;
//if the specified X line was a mirror, where would the target appear to be?
//lineseg between it and the center of tank
line_seg_t line;
struct line_seg line;
line.x1 = tank->center_x();
line.y1 = tank->center_y();
line.x2 = 2 * rX - game.player->position_x - game.player->width / 2;
Expand All @@ -149,7 +149,7 @@ void aim_reflect(Tank *tank) {
int yInt = y_intercept(&line, rX);
uint8_t xT = x - !left;
uint8_t yT = COORD_TO_Y_TILE(yInt);
tile_t tile = tiles[yT][xT];
tile_t tile = game.tiles[yT][xT];
#ifdef DBG_DRAW
gfx_SetColor(COL_RED);
drawLine(&line);
Expand All @@ -165,7 +165,7 @@ void aim_reflect(Tank *tank) {
}
//if so, check if pointing_at_target
tank->barrel_rot = fast_atan2(line.y2 - line.y1, line.x2 - line.x1);
if(pointing_at_target(tank, game.player, Tank::max_bounces[tank->type], false)) {
if(pointing_at_target(tank, game.player, tank->max_bounces(), false)) {
//if so, fire
tank->fire_shell();
}
Expand All @@ -175,7 +175,7 @@ void aim_reflect(Tank *tank) {
ai->scan_pos = 1;
ai->scan_dir = AXIS_X;
point_at_player(tank, game.player);
if(pointing_at_target(tank, game.player, Tank::max_bounces[tank->type], false)) {
if(pointing_at_target(tank, game.player, tank->max_bounces(), false)) {
tank->fire_shell();
}
return; //I know this kinda skips a tick but whatever
Expand All @@ -187,7 +187,7 @@ void aim_reflect(Tank *tank) {
if(up != (game.player->center_y() < rY)) return;
//if the specified X line was a mirror, where would the target appear to be?
//lineseg between it and the center of tank
line_seg_t line;
struct line_seg line;
line.x1 = tank->center_x();
line.y1 = tank->center_y();
line.x2 = game.player->center_x();
Expand All @@ -196,7 +196,7 @@ void aim_reflect(Tank *tank) {
int xInt = x_intercept(&line, rY);
uint8_t xT = COORD_TO_X_TILE(xInt);
uint8_t yT = y - !up;
tile_t tile = tiles[yT][xT];
tile_t tile = game.tiles[yT][xT];
#ifdef DBG_DRAW
gfx_SetColor(COL_RED);
drawLine(&line);
Expand All @@ -208,7 +208,7 @@ void aim_reflect(Tank *tank) {
if(!TILE_HEIGHT(tile) || TILE_TYPE(tile) == DESTROYED) return;
//if so, check if pointing_at_target
tank->barrel_rot = fast_atan2(line.y2 - line.y1, line.x2 - line.x1);
if(pointing_at_target(tank, game.player, Tank::max_bounces[tank->type], false)) {
if(pointing_at_target(tank, game.player, tank->max_bounces(), false)) {
//if so, fire
tank->fire_shell();
}
Expand Down Expand Up @@ -237,7 +237,7 @@ tile_t get_tile_at_offset(Tank *tank, angle_t angle_offset, int distance) {
int8_t tile_y = COORD_TO_X_TILE(y);
if(tile_x < 0 || tile_x >= LEVEL_SIZE_X) return 1;
if(tile_y < 0 || tile_y >= LEVEL_SIZE_Y) return 1;
return tiles[COORD_TO_Y_TILE(y)][COORD_TO_X_TILE(x)];
return game.tiles[COORD_TO_Y_TILE(y)][COORD_TO_X_TILE(x)];
}

// todo: check tank's future position
Expand All @@ -247,7 +247,7 @@ bool pointing_at_target(Tank *tank, PhysicsBody *target, uint8_t max_bounces, __
angle_t angle = tank->barrel_rot;
for(uint8_t bounces = 0; bounces <= max_bounces; bounces++) {
bool reflectAxis;
line_seg_t line;
struct line_seg line;
profiler_add(raycast);
reflectAxis = raycast(posX, posY, angle, &line);
profiler_end(raycast);
Expand Down
44 changes: 22 additions & 22 deletions src/ai/ai_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@
#include <cstdint>
#include "../util/util.h"

typedef struct {
struct ai_fire_random_state {
bool clockwise;
} ai_fire_random_state_t;
};

typedef struct {} ai_fire_current_state_t;
struct ai_fire_current_state {};

typedef struct {
struct ai_fire_reflect_state {
uint8_t scan_dir; //0 = X, 1 = Y
uint8_t scan_pos;
} ai_fire_reflect_state_t;
};

typedef struct {} ai_fire_future_state_t;
struct ai_fire_future_state {};

typedef union {
ai_fire_random_state_t random;
ai_fire_current_state_t current;
ai_fire_reflect_state_t reflect;
ai_fire_future_state_t future;
} ai_fire_state_t;
union ai_fire_state {
struct ai_fire_random_state random;
struct ai_fire_current_state current;
struct ai_fire_reflect_state reflect;
struct ai_fire_future_state future;
};

typedef struct {
struct ai_move_random_state {
uint8_t cur_dir;
} ai_move_random_state_t;
};

typedef struct {} ai_move_toward_state_t;
struct ai_move_toward_state {};

typedef struct {
struct ai_move_away_state {
uint target_x;
uint target_y;
} ai_move_away_state_t;
};

typedef union ai_move {
ai_move_random_state_t random;
ai_move_toward_state_t toward;
ai_move_away_state_t away;
} ai_move_state_t;
union ai_move_state {
struct ai_move_random_state random;
struct ai_move_toward_state toward;
struct ai_move_away_state away;
};

#endif //TANKS_AI_STATE_H
27 changes: 13 additions & 14 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@
#include <new>
#include <debug.h>
#include "objects/tank.h"
#include "globals.h"
#include "graphics/graphics.h"
#include "graphics/gui.h"
#include "input.h"
#include "util/profiler.h"
#include "graphics/tank_sprite.h"
#include "gui/error.h"
#include "gui/transition.h"

bool start_mission(const serialized_tank_t *ser_tanks) {
struct game game;

bool start_mission(const struct serialized_tank *ser_tanks) {
dbg_printf("starting mission\n");

for(auto & obj : PhysicsBody::objects) {
obj->active = false;
}
PhysicsBody::remove_inactive();
PhysicsBody::remove_all();

game.num_tanks = 0;

for(uint8_t i = 0; i < game.level.num_tanks; i++) {
if(!game.alive_tanks[i]) continue;
//dbg_printf("tank created: %p\n", tank);
Tank *tank = new (std::nothrow) Tank(&ser_tanks[i], i);
Tank *tank = new(std::nothrow) Tank(&ser_tanks[i], i);
if(!tank) {
ERROR("Failed to allocate tank");
}
}

for(uint8_t x = 1; x < LEVEL_SIZE_X - 1; x++) {
for(uint8_t y = 1; y < LEVEL_SIZE_Y - 1; y++) {
if(TILE_TYPE(tiles[y][x]) == DESTROYED)
tiles[y][x] = TILE_HEIGHT(tiles[y][x]) | DESTRUCTIBLE;
if(TILE_TYPE(game.tiles[y][x]) == DESTROYED)
game.tiles[y][x] = TILE_HEIGHT(game.tiles[y][x]) | DESTRUCTIBLE;
}
}

Expand All @@ -47,7 +46,7 @@ bool start_mission(const serialized_tank_t *ser_tanks) {
return true;
}

uint8_t play_mission(const serialized_tank_t *ser_tanks) {
uint8_t play_mission(const struct serialized_tank *ser_tanks) {
start_mission(ser_tanks);
while(true) {
profiler_start(total);
Expand All @@ -64,7 +63,7 @@ uint8_t play_mission(const serialized_tank_t *ser_tanks) {
profiler_start(pb_collision);
process_collisions();
profiler_end(pb_collision);
for(auto & object : PhysicsBody::objects) {
for(auto &object : PhysicsBody::objects) {
object->tick();
}
PhysicsBody::remove_inactive();
Expand Down Expand Up @@ -98,10 +97,10 @@ uint8_t play_mission(const serialized_tank_t *ser_tanks) {
}
}

uint8_t play_level(const void *comp_tiles, const serialized_tank_t *ser_tanks) {
uint8_t play_level(const void *comp_tiles, const struct serialized_tank *ser_tanks) {
decompress_tiles(comp_tiles);

for(uint8_t i = 0; i < game.level.num_tanks; i++){
for(uint8_t i = 0; i < game.level.num_tanks; i++) {
game.alive_tanks[i] = true;
}

Expand Down
26 changes: 23 additions & 3 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,28 @@

#include "objects/tank.h"

bool start_mission(const serialized_tank_t *ser_tanks); //Start a mission and reset various tank things.
uint8_t play_level(const void *comp_tiles, const serialized_tank_t *ser_tanks);
uint8_t play_mission(const serialized_tank_t *ser_tanks);
// Game status
enum {
QUIT = 1, NEXT_LEVEL, RETRY, LOSE, ERROR
};

struct game {
struct level level; //level currently being played
uint8_t mission; //The mission number, always displayed 1 higher than stored. Also used as an index for levels.
tile_t tiles[LEVEL_SIZE_Y][LEVEL_SIZE_X]; //Currently active tilemap data
uint8_t lives; //Number of remaining tanks. This includes the tank that is currently in use, so a value of 1 means that the game will end the next time the tank is hit.
uint8_t total_kills; //Number of enemy tanks destroyed.
uint8_t kills[NUM_TANK_TYPES];
uint8_t num_tanks;
bool alive_tanks[MAX_NUM_TANKS];
Tank *player;
uint24_t tick;
};

extern struct game game;

bool start_mission(const struct serialized_tank *ser_tanks); //Start a mission and reset various tank things.
uint8_t play_level(const void *comp_tiles, const struct serialized_tank *ser_tanks);
uint8_t play_mission(const struct serialized_tank *ser_tanks);

#endif //TANKS_GAME_H
5 changes: 0 additions & 5 deletions src/globals.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions src/globals.h

This file was deleted.

13 changes: 4 additions & 9 deletions src/graphics/graphics.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include "graphics.h"

#include <debug.h>

#include "../globals.h"
#include "../util/profiler.h"
#include "partial_redraw.h"
#include "dynamic_sprites.h"
#include "gui.h"
#include "../data/gfx/enemy_pal.h"
#include "tiles.h"
#include "../game.h"
#include "../gui/banner.h"
#include "../gui/aim_indicator.h"

bool needs_redraw;

Expand Down Expand Up @@ -72,7 +71,7 @@ void render() {
gfx_SetClipRegion(SCREEN_X_CONST(0), SCREEN_Y_CONST(-TILE_SIZE), SCREEN_X_CONST(LEVEL_SIZE_X * TILE_SIZE),
SCREEN_Y_CONST((LEVEL_SIZE_Y - 2) * TILE_SIZE));
for(uint8_t layer = 0; layer < 3; layer++) {
for(auto *it: PhysicsBody::objects) {
for(auto it: PhysicsBody::objects) {
it->render(layer);
}
}
Expand All @@ -87,7 +86,3 @@ void render() {

profiler_end(graphics);
}

void draw_line(line_seg_t *ls) {
gfx_Line(SCREEN_X(ls->x1), SCREEN_Y(ls->y1), SCREEN_X(ls->x2), SCREEN_Y(ls->y2));
}
4 changes: 0 additions & 4 deletions src/graphics/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,9 @@ void init_graphics();

void render(); //Render tilemap, tanks, and UI during the game loop

void draw_line(line_seg_t *ls);

void get_sprite_footprint(gfx_region_t *out, const PhysicsBody *phys, gfx_sprite_t **sprites, const uint8_t *offsets_x,
const uint8_t *offsets_y, uint8_t anim);

void get_enemy_palette_map(uint8_t *out, tank_type_t type);

extern bool needs_redraw; // set if the entire map should be redrawn

#endif
Loading

0 comments on commit 139d168

Please sign in to comment.