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

Commit

Permalink
Enemy (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLime1 authored Mar 13, 2023
2 parents a9da49f + 932c66a commit 1d34c35
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 26 deletions.
Binary file added assets/img/enemyrun.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions build/makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
CC = gcc
CFLAGS = -Wall -I../include
LDFLAGS = -lSDL -lSDL_image -lSDL_mixer -lSDL_ttf
OBJS = main.o menu.o music.o text.o stars.o settings.o start.o
OBJS = main.o menu.o music.o text.o stars.o settings.o start.o enemy.o

game: $(OBJS)
$(CC) $(OBJS) $(LDFLAGS) -o game

%.o: ../src/%.c
$(CC) $(CFLAGS) -c $<

.PHONY: clean
clean:
rm -f $(OBJS) game
rm -f $(OBJS) game
Binary file added doc/enemxy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions include/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef CONSTANTS_H
#define CONSTANTS_H

//*screen size
#define SCREEN_W 1280
#define SCREEN_H 720

//*stars stuff
#define STARS_COUNT 100 // number of stars //? need locking
#define STARS_LAYERS 4 // number of stars variations
#define DELTA_TIME 16 // 1000ms / 60fps = 16.6666

//*main menu buttons stuff
#define BUTTON_SPACING 14 // spacing between the buttons (px) //! LOCKED
#define INTIAL_BUTTON_Y 325 // initial button y position (px) //!LOCKED

#endif
30 changes: 30 additions & 0 deletions include/enemy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef ENEMY_H
#define ENEMY_H

typedef struct
{
SDL_Rect img_pos; // position of the sprite on the screen
SDL_Rect img_size; // size of the sprite
SDL_Surface *img; // pointer to the sprite image
int direction; // 0 for idle, 1 for right , 2 for left
float speed; // steps per frame
int max_steps; // maximum number of steps before changing direction
int idle_time; // time when the enemy started idling
int x; // position of the enemy in the grid (screen coordinates)
int y; // position of the enemy in the grid (screen coordinates)
} enemy;

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
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
5 changes: 0 additions & 5 deletions include/menu.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

#ifndef MENU_H
#define MENU_H

#define SCREEN_W 1280
#define SCREEN_H 720 // screen height and width
typedef struct
{

Expand Down Expand Up @@ -46,7 +43,6 @@ void imageDrawClicked_playbutton(SDL_Surface *screen, image img);
void imageDrawClicked_settingsbutton(SDL_Surface *screen, image img);
void imageDrawClicked_quitbutton(SDL_Surface *screen, image img);


void imageDrawHovered_playbutton(SDL_Surface *screen, image img);
void imageDrawHovered_settingsbutton(SDL_Surface *screen, image img);
void imageDrawHovered_quitbutton(SDL_Surface *screen, image img);
Expand All @@ -55,5 +51,4 @@ void imageDrawHovered_quitbutton(SDL_Surface *screen, image img);
//**UNIVERSAL FUNCTION**//
// free buttons


#endif
224 changes: 224 additions & 0 deletions src/enemy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "constants.h"
#include "enemy.h"

void initEnemy(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) + 100; // 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 drawEnemy(SDL_Surface *screen, enemy e)
{
SDL_BlitSurface(e.img, &(e.img_size), screen, &(e.img_pos));
}

void animateEnemy(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 moveEnemy(enemy *e)
{
if (e->direction == 1)
{ // 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 == 2)
{ // 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
}
}
}
}

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
}
}
}
}
28 changes: 21 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,23 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "constants.h" // for constants

#include <SDL/SDL.h>
#include <SDL/SDL_image.h> //for loading images
#include <SDL/SDL_ttf.h> //for loading fonts
#include <SDL/SDL_mixer.h> //for loading sounds

// including the headers

#include "../include/menu.h" //menu header
#include "../include/music.h" //music header
#include "../include/text.h" //text header
#include "../include/stars.h" //stars header
#include "../include/settings.h" //settings header
#include "../include/enemy.h"

// screen
SDL_Surface *screen;
#define SCREEN_W 1280
#define SCREEN_H 720 // screen height and width

#define STARS_COUNT 100 // number of stars //! need locking
#define STARS_LAYERS 4 // number of stars variations
#define DELTA_TIME 16 // 1000ms / 60fps = 16.6666

//* regular -> hovered -> clicked
// images (_C for clicked) (_H for hovered)
Expand Down Expand Up @@ -65,6 +60,10 @@ Mix_Chunk *clickFX;
// text
text author;

// characters
enemy enemy1;
enemy enemy2;

// logic
SDL_Event event;
int loop = 1; // game loop
Expand Down Expand Up @@ -141,6 +140,10 @@ int main()
// loading text
textLoad(&author);

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

//* loading settings menu images
//? maybe add background instead of solid color for settings menu

Expand Down Expand Up @@ -440,6 +443,17 @@ int main()
imageDraw_lvl1(screen, lvl1);
imageDraw_backbutton(screen, backButton); //! used twice, but it's ok for now (universal fucntion)

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))
{
switch (event.type)
Expand Down
Loading

0 comments on commit 1d34c35

Please sign in to comment.