halfway through adding player as entity struct
This commit is contained in:
@@ -56,13 +56,56 @@ int is_held(char k)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int player_x = 100;
|
||||
static int player_y = 100;
|
||||
static float player_vx = 0.0;
|
||||
static float player_vy = 0.0;
|
||||
static float player_ax = 0.0;
|
||||
static float player_ay = 9.81;
|
||||
static int jump_ticks = 0;
|
||||
struct entity
|
||||
{
|
||||
int is_player;
|
||||
int is_static;
|
||||
int x;
|
||||
int y;
|
||||
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)
|
||||
{
|
||||
@@ -70,8 +113,8 @@ void key_down(char k)
|
||||
printf("%c pressed!\n", k);
|
||||
switch (k)
|
||||
{
|
||||
case 'a': player_ax = -2; break;
|
||||
case 'd': player_ax = 2; break;
|
||||
case 'a': get_player()->ax = -2; break;
|
||||
case 'd': get_player()->ax = 2; break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,6 +175,7 @@ void draw_entity(/* struct entity */)
|
||||
/* get sprite info, etc */
|
||||
/* FIXME */
|
||||
cat_fill(1, 1, player_x, player_y, 50, 50, 115, 72, 6);
|
||||
/* if out of bounds do not draw */
|
||||
}
|
||||
|
||||
void erase_entities(void)
|
||||
@@ -151,21 +195,12 @@ static long last_nsec = 0;
|
||||
|
||||
#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 */
|
||||
{
|
||||
if (player_vx > 0) player_vx -= 1;
|
||||
else if (player_vx < 0) player_vx += 1;
|
||||
if (player_vy > 0) player_vy -= 1;
|
||||
else if (player_vy < 0) player_vy += 1;
|
||||
if (get_player()->vx > 0) get_player()->vx -= 1;
|
||||
else if (get_player()->vx < 0) get_player()->vx += 1;
|
||||
if (get_player()->vy > 0) get_player()->vy -= 1;
|
||||
else if (get_player()->vy < 0) get_player()->vy += 1;
|
||||
}
|
||||
|
||||
void tick(void)
|
||||
@@ -183,14 +218,13 @@ void tick(void)
|
||||
|
||||
/* code to run every tick goes here */
|
||||
erase_entities();
|
||||
if (is_held(' ')) player_ay = -5;
|
||||
else player_ay = 9.81;
|
||||
player_vx += player_ax;
|
||||
player_vy += player_ay;
|
||||
if (is_held(' ')) get_player()->ay = -5;
|
||||
else get_player()->ay = 9.81;
|
||||
get_player()->vx += get_player()->ax;
|
||||
get_player()->vy += get_player()->ay;
|
||||
apply_friction(); /* FIXME */
|
||||
player_x += (int)player_vx;
|
||||
player_y += (int)player_vy;
|
||||
in_bounds();
|
||||
get_player()->x += (int)get_player()->vx;
|
||||
get_player()->y += (int)get_player()->vy;
|
||||
draw_entities();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user