halfway through adding player as entity struct

This commit is contained in:
Tobin
2026-04-21 15:16:44 -04:00
parent 4d469695bb
commit 155da173bc
+63 -29
View File
@@ -56,13 +56,56 @@ int is_held(char k)
return 0; return 0;
} }
static int player_x = 100; struct entity
static int player_y = 100; {
static float player_vx = 0.0; int is_player;
static float player_vy = 0.0; int is_static;
static float player_ax = 0.0; int x;
static float player_ay = 9.81; int y;
static int jump_ticks = 0; int width;
int height;
unsigned int color; /* replace this with sprite TODO */
float vx;
float vy;
float ax;
float ay;
struct entity* next;
};
struct entity* first_entity = NULL;
void add_entity(int is_player, int is_static, int x, int y, int width, int height, unsigned int color,
float vx, float vy, float ax, float ay)
{
struct entity* new = malloc(sizeof(struct entity));
new->is_player = is_player;
new->is_static = is_static;
new->x = x;
new->y = y;
new->width = width;
new->height = height;
new->color = color;
new->vx = vx;
new->vy = vy;
new->ax = ax;
new->ay = ay;
new->next = NULL;
if (first_entity == NULL) first_entity = new;
else
{
struct entity* head;
for (head = first_entity; head->next != NULL; head = head->next) ;
head->next = new;
}
}
struct entity* get_player(void)
{
struct entity* head;
for (head = first_entity; head != NULL; head = head->next)
if (head->is_player) return head;
return NULL;
}
void key_down(char k) void key_down(char k)
{ {
@@ -70,8 +113,8 @@ void key_down(char k)
printf("%c pressed!\n", k); printf("%c pressed!\n", k);
switch (k) switch (k)
{ {
case 'a': player_ax = -2; break; case 'a': get_player()->ax = -2; break;
case 'd': player_ax = 2; break; case 'd': get_player()->ax = 2; break;
} }
} }
@@ -132,6 +175,7 @@ void draw_entity(/* struct entity */)
/* get sprite info, etc */ /* get sprite info, etc */
/* FIXME */ /* FIXME */
cat_fill(1, 1, player_x, player_y, 50, 50, 115, 72, 6); cat_fill(1, 1, player_x, player_y, 50, 50, 115, 72, 6);
/* if out of bounds do not draw */
} }
void erase_entities(void) void erase_entities(void)
@@ -151,21 +195,12 @@ static long last_nsec = 0;
#define NS_PER_TICK 30000000 #define NS_PER_TICK 30000000
/* FIXME this is just temporary */
void in_bounds(void)
{
if (player_x < 0) player_x = 0;
if (player_y < 0) player_y = 0;
if (player_x > screen_width - 50) player_x = screen_width - 50;
if (player_y > screen_height- 50) player_y = screen_height - 50;
}
void apply_friction(void) /* FIXME */ void apply_friction(void) /* FIXME */
{ {
if (player_vx > 0) player_vx -= 1; if (get_player()->vx > 0) get_player()->vx -= 1;
else if (player_vx < 0) player_vx += 1; else if (get_player()->vx < 0) get_player()->vx += 1;
if (player_vy > 0) player_vy -= 1; if (get_player()->vy > 0) get_player()->vy -= 1;
else if (player_vy < 0) player_vy += 1; else if (get_player()->vy < 0) get_player()->vy += 1;
} }
void tick(void) void tick(void)
@@ -183,14 +218,13 @@ void tick(void)
/* code to run every tick goes here */ /* code to run every tick goes here */
erase_entities(); erase_entities();
if (is_held(' ')) player_ay = -5; if (is_held(' ')) get_player()->ay = -5;
else player_ay = 9.81; else get_player()->ay = 9.81;
player_vx += player_ax; get_player()->vx += get_player()->ax;
player_vy += player_ay; get_player()->vy += get_player()->ay;
apply_friction(); /* FIXME */ apply_friction(); /* FIXME */
player_x += (int)player_vx; get_player()->x += (int)get_player()->vx;
player_y += (int)player_vy; get_player()->y += (int)get_player()->vy;
in_bounds();
draw_entities(); draw_entities();
} }