Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
+collision
Browse files Browse the repository at this point in the history
added test enemy for collision
  • Loading branch information
TheLime1 committed Mar 13, 2023
1 parent f8bf90c commit 932c66a
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 2 deletions.
9 changes: 8 additions & 1 deletion include/enemy.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ void initEnemy(enemy *e);
void drawEnemy(SDL_Surface *screen, enemy e);
void animateEnemy(enemy *e, int direction);
void moveEnemy(enemy *e); //! added new parameter dont forget
// TODO : add collision detection
int collisionBB(SDL_Rect player, SDL_Rect enemy);

//****************************************************

void initEnemytest(enemy *e);
void drawEnemytest(SDL_Surface *screen, enemy e);
void animateEnemytest(enemy *e, int direction);
void moveEnemytest(enemy *e);

#endif
120 changes: 119 additions & 1 deletion src/enemy.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,122 @@ void moveEnemy(enemy *e)
}
}
}
}
}

int collisionBB(SDL_Rect player, SDL_Rect enemy)
{
int collision = 0;
if ((player.x + player.w < enemy.x) || (player.x > enemy.x + enemy.w) || (player.y + player.h < enemy.y) || (player.y > enemy.y + enemy.h))
{
collision = 0; // no collision
}
else
{
collision = 1; // collision
}
return collision;
}

//*******************************

void initEnemytest(enemy *e)
{
e->img = IMG_Load("../assets/img/enemyrun.png");
if (e->img == NULL)
{
printf("Error loading enemy image: %s\n", SDL_GetError());
}
e->direction = 1;
e->speed = 1;
e->max_steps = 100;
e->idle_time = 2000;
e->x = (SCREEN_W / 2 - (e->img->w / 6) - 96 / 2) + 200; // screen width - (width of img / sprite size) - (width of sprite / 2)
e->y = (SCREEN_H / 2 - (e->img->h / 3) - 96 / 2) + 100; // screen height - (height of img / sprite size) - (height of sprite / 2)

e->img_size.x = 0;
e->img_size.y = 0;
e->img_size.w = (e->img->w) / 6; // (width of img / sprite frames) :576/6 =96px
e->img_size.h = (e->img->h) / 3; // (height of img / sprite frames) := 288/3= 96px
e->img_pos.x = e->x;
e->img_pos.y = e->y;
}

void drawEnemytest(SDL_Surface *screen, enemy e)
{
SDL_BlitSurface(e.img, &(e.img_size), screen, &(e.img_pos));
}

void animateEnemytest(enemy *e, int direction) //! added new parameter dont forget
{
static int frame = 0; // static variable to track current frame
int row = 0;
if (direction == 1)
{
row = 0;
}
else if (direction == 2)
{
row = 1;
}
else if (direction == 0)
{
row = 2;
}
e->img_size.x = frame * e->img_size.w; // set x position based on frame
e->img_size.y = row * e->img_size.h; // set y position based on direction

Uint32 current_time = SDL_GetTicks();
static Uint32 last_time = 0;
Uint32 delta_time = current_time - last_time;
if (delta_time >= 100)
{ // add a delay of 100 milliseconds between frames
frame++; // increment frame counter
if (frame >= 6)
{ // if we've reached the end of the sprite sheet, wrap around
frame = 0;
}
last_time = current_time;
}
}

void moveEnemytest(enemy *e)
{
if (e->direction == 2)
{ // move right
animateEnemy(e, 1);
e->img_pos.x += e->speed;
if (e->img_pos.x >= e->x + e->max_steps)
{
e->img_pos.x = e->x + e->max_steps;
e->direction = 0; // change direction to idle
e->idle_time = SDL_GetTicks(); // start idle time
}
}
else if (e->direction == 1)
{ // move left
animateEnemy(e, 2);
e->img_pos.x -= e->speed;
if (e->img_pos.x <= e->x)
{
e->img_pos.x = e->x;
e->direction = 0; // change direction to idle
e->idle_time = SDL_GetTicks(); // start idle time
}
}
else if (e->direction == 0)
{ // idle
animateEnemy(e, 0);
Uint32 current_time = SDL_GetTicks();
if (current_time - e->idle_time >= 2000)
{ // check if idle time is over
if (e->img_pos.x == e->x)
{
e->direction = 1; // change direction to right
}
else if (e->img_pos.x == e->x + e->max_steps)
{
e->direction = 2; // change direction to left
}
}
}
}
10 changes: 10 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ text author;

// characters
enemy enemy1;
enemy enemy2;

// logic
SDL_Event event;
Expand Down Expand Up @@ -141,6 +142,7 @@ int main()

// loading enemy
initEnemy(&enemy1);
initEnemytest(&enemy2);

//* loading settings menu images
//? maybe add background instead of solid color for settings menu
Expand Down Expand Up @@ -443,6 +445,14 @@ int main()

drawEnemy(screen, enemy1);
moveEnemy(&enemy1); //* moveEnemy will call animateEnemy
//************
drawEnemytest(screen, enemy2);
moveEnemytest(&enemy2); //* moveEnemy will call animateEnemy
//************
if (collisionBB(enemy1.img_pos, enemy2.img_pos) == 1)
{
printf(" collision detected \t");
}

while (SDL_PollEvent(&event))
{
Expand Down

0 comments on commit 932c66a

Please sign in to comment.