Created
April 30, 2021 15:48
-
-
Save eduardonunesp/0ef6433bb77466ee33de330808b97ab1 to your computer and use it in GitHub Desktop.
Basic Skeleton for Shinning Force like game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <math.h> | |
#define GRID_SIZE 384 | |
#define GRID_WIDTH 16 | |
#define MAX_PAWNS_IN_GAME 50 | |
typedef struct pawn_s { | |
int idx; | |
int x, y; | |
int atk_dist; | |
} pawn_t; | |
pawn_t *pawns[MAX_PAWNS_IN_GAME]; | |
int grid[GRID_SIZE]; | |
typedef void (cell_itr_handler_t)(int* cell); | |
typedef void (cell_itr_value_handler_t)(int* cell, int value); | |
pawn_t *pawn_init(int idx, int x, int y, int atk_dist) { | |
pawn_t *new_pawn = (pawn_t*) calloc(sizeof(pawn_t), 1); | |
new_pawn->idx = idx; | |
new_pawn->x = x; | |
new_pawn->y = y; | |
new_pawn->atk_dist = atk_dist; | |
return new_pawn; | |
} | |
void pawns_add(pawn_t *pawn) { | |
pawns[pawn->idx] = pawn; | |
grid[pawn->x * GRID_WIDTH + pawn->y] = pawn->idx; | |
} | |
void iterate_grid(cell_itr_handler_t* cell_itr_handler) { | |
int x, y = 0; | |
for (size_t i = 0; i < GRID_SIZE; i++) { | |
if (i % GRID_WIDTH == 0 && i > 0) { | |
x = 0; | |
y++; | |
} | |
cell_itr_handler(&grid[i]); | |
} | |
} | |
void clear_board(cell_itr_handler_t *cell_it_handler) | |
{ | |
for (size_t i = 0; i < GRID_SIZE; i++) { | |
cell_it_handler(&grid[i]); | |
} | |
} | |
void set_board_value(int value, int x, int y) | |
{ | |
grid[x * GRID_WIDTH + y] = value; | |
} | |
void debug_grid(bool with_coordinates) | |
{ | |
int x, y = 0; | |
for (size_t i = 0; i < GRID_SIZE; i++) { | |
if (i % GRID_WIDTH == 0 && i > 0) { | |
printf("\n"); | |
x = 0; | |
y++; | |
} | |
if (with_coordinates) { | |
printf("[%d](%d,%d)\t", grid[i], x++, y); | |
} else { | |
printf("[%d]\t", grid[i]); | |
} | |
} | |
printf("\n"); | |
} | |
bool is_inside_circle(int cx, int cy, int x, int y, int radius) | |
{ | |
float dx = cx - x; | |
float dy = cy - y; | |
float distance = sqrt((dx * dx) + (dy * dy)); | |
return distance <= radius; | |
} | |
/** | |
* Draws circle to marks the position | |
* @param cx Integer with the center of the circle | |
* @param cy Integer with the center of the circle | |
*/ | |
void draw_circle_markers(cell_itr_value_handler_t* cell_itr_value_handler, int pawn_idx) | |
{ | |
int x, y = 0; | |
for (size_t i = 0; i < GRID_SIZE; i++) { | |
if (i % GRID_WIDTH == 0 && i > 0) { | |
x = 0; | |
y++; | |
} | |
pawn_t *pawn = pawns[pawn_idx]; | |
if (pawn) { | |
if (is_inside_circle(pawn->x, pawn->y, x++, y, pawn->atk_dist)) { | |
cell_itr_value_handler(&grid[i], pawn->idx); | |
} | |
} | |
} | |
} | |
void init_cell(int* cell) { | |
*cell = 0; | |
} | |
void clear_cell(int* cell) { | |
*cell = 0; | |
} | |
void mark_cell(int* cell, int value) { | |
*cell = value; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
iterate_grid(&init_cell); | |
iterate_grid(&clear_cell); | |
pawns_add(pawn_init(1, 9, 9, 1)); | |
pawns_add(pawn_init(2, 2, 2, 2)); | |
draw_circle_markers(&mark_cell, 1); | |
draw_circle_markers(&mark_cell, 2); | |
debug_grid(false); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment