|
| 1 | +#include <SDL/SDL.h> |
| 2 | +#include <SDL/SDL_image.h> |
| 3 | +#include "constants.h" |
| 4 | +#include "enemy.h" |
| 5 | + |
| 6 | +void initEnemy(enemy *e) |
| 7 | +{ |
| 8 | + e->img = IMG_Load("../assets/img/enemyrun.png"); |
| 9 | + if (e->img == NULL) |
| 10 | + { |
| 11 | + printf("Error loading enemy image: %s\n", SDL_GetError()); |
| 12 | + } |
| 13 | + e->direction = 1; |
| 14 | + e->speed = 1; |
| 15 | + e->max_steps = 100; |
| 16 | + e->idle_time = 2000; |
| 17 | + e->x = (SCREEN_W / 2 - (e->img->w / 6) - 96 / 2) + 100; // screen width - (width of img / sprite size) - (width of sprite / 2) |
| 18 | + e->y = (SCREEN_H / 2 - (e->img->h / 3) - 96 / 2) + 100; // screen height - (height of img / sprite size) - (height of sprite / 2) |
| 19 | + |
| 20 | + e->img_size.x = 0; |
| 21 | + e->img_size.y = 0; |
| 22 | + e->img_size.w = (e->img->w) / 6; // (width of img / sprite frames) :576/6 =96px |
| 23 | + e->img_size.h = (e->img->h) / 3; // (height of img / sprite frames) := 288/3= 96px |
| 24 | + e->img_pos.x = e->x; |
| 25 | + e->img_pos.y = e->y; |
| 26 | +} |
| 27 | + |
| 28 | +void drawEnemy(SDL_Surface *screen, enemy e) |
| 29 | +{ |
| 30 | + SDL_BlitSurface(e.img, &(e.img_size), screen, &(e.img_pos)); |
| 31 | +} |
| 32 | + |
| 33 | +void animateEnemy(enemy *e, int direction) //! added new parameter dont forget |
| 34 | +{ |
| 35 | + static int frame = 0; // static variable to track current frame |
| 36 | + int row = 0; |
| 37 | + if (direction == 1) |
| 38 | + { |
| 39 | + row = 0; |
| 40 | + } |
| 41 | + else if (direction == 2) |
| 42 | + { |
| 43 | + row = 1; |
| 44 | + } |
| 45 | + else if (direction == 0) |
| 46 | + { |
| 47 | + row = 2; |
| 48 | + } |
| 49 | + e->img_size.x = frame * e->img_size.w; // set x position based on frame |
| 50 | + e->img_size.y = row * e->img_size.h; // set y position based on direction |
| 51 | + |
| 52 | + Uint32 current_time = SDL_GetTicks(); |
| 53 | + static Uint32 last_time = 0; |
| 54 | + Uint32 delta_time = current_time - last_time; |
| 55 | + if (delta_time >= 100) |
| 56 | + { // add a delay of 100 milliseconds between frames |
| 57 | + frame++; // increment frame counter |
| 58 | + if (frame >= 6) |
| 59 | + { // if we've reached the end of the sprite sheet, wrap around |
| 60 | + frame = 0; |
| 61 | + } |
| 62 | + last_time = current_time; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void moveEnemy(enemy *e) |
| 67 | +{ |
| 68 | + if (e->direction == 1) |
| 69 | + { // move right |
| 70 | + animateEnemy(e, 1); |
| 71 | + e->img_pos.x += e->speed; |
| 72 | + if (e->img_pos.x >= e->x + e->max_steps) |
| 73 | + { |
| 74 | + e->img_pos.x = e->x + e->max_steps; |
| 75 | + e->direction = 0; // change direction to idle |
| 76 | + e->idle_time = SDL_GetTicks(); // start idle time |
| 77 | + } |
| 78 | + } |
| 79 | + else if (e->direction == 2) |
| 80 | + { // move left |
| 81 | + animateEnemy(e, 2); |
| 82 | + e->img_pos.x -= e->speed; |
| 83 | + if (e->img_pos.x <= e->x) |
| 84 | + { |
| 85 | + e->img_pos.x = e->x; |
| 86 | + e->direction = 0; // change direction to idle |
| 87 | + e->idle_time = SDL_GetTicks(); // start idle time |
| 88 | + } |
| 89 | + } |
| 90 | + else if (e->direction == 0) |
| 91 | + { // idle |
| 92 | + animateEnemy(e, 0); |
| 93 | + Uint32 current_time = SDL_GetTicks(); |
| 94 | + if (current_time - e->idle_time >= 2000) |
| 95 | + { // check if idle time is over |
| 96 | + if (e->img_pos.x == e->x) |
| 97 | + { |
| 98 | + e->direction = 1; // change direction to right |
| 99 | + } |
| 100 | + else if (e->img_pos.x == e->x + e->max_steps) |
| 101 | + { |
| 102 | + e->direction = 2; // change direction to left |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +int collisionBB(SDL_Rect player, SDL_Rect enemy) |
| 109 | +{ |
| 110 | + int collision = 0; |
| 111 | + 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)) |
| 112 | + { |
| 113 | + collision = 0; // no collision |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + collision = 1; // collision |
| 118 | + } |
| 119 | + return collision; |
| 120 | +} |
| 121 | + |
| 122 | +//******************************* |
| 123 | + |
| 124 | +void initEnemytest(enemy *e) |
| 125 | +{ |
| 126 | + e->img = IMG_Load("../assets/img/enemyrun.png"); |
| 127 | + if (e->img == NULL) |
| 128 | + { |
| 129 | + printf("Error loading enemy image: %s\n", SDL_GetError()); |
| 130 | + } |
| 131 | + e->direction = 1; |
| 132 | + e->speed = 1; |
| 133 | + e->max_steps = 100; |
| 134 | + e->idle_time = 2000; |
| 135 | + e->x = (SCREEN_W / 2 - (e->img->w / 6) - 96 / 2) + 200; // screen width - (width of img / sprite size) - (width of sprite / 2) |
| 136 | + e->y = (SCREEN_H / 2 - (e->img->h / 3) - 96 / 2) + 100; // screen height - (height of img / sprite size) - (height of sprite / 2) |
| 137 | + |
| 138 | + e->img_size.x = 0; |
| 139 | + e->img_size.y = 0; |
| 140 | + e->img_size.w = (e->img->w) / 6; // (width of img / sprite frames) :576/6 =96px |
| 141 | + e->img_size.h = (e->img->h) / 3; // (height of img / sprite frames) := 288/3= 96px |
| 142 | + e->img_pos.x = e->x; |
| 143 | + e->img_pos.y = e->y; |
| 144 | +} |
| 145 | + |
| 146 | +void drawEnemytest(SDL_Surface *screen, enemy e) |
| 147 | +{ |
| 148 | + SDL_BlitSurface(e.img, &(e.img_size), screen, &(e.img_pos)); |
| 149 | +} |
| 150 | + |
| 151 | +void animateEnemytest(enemy *e, int direction) //! added new parameter dont forget |
| 152 | +{ |
| 153 | + static int frame = 0; // static variable to track current frame |
| 154 | + int row = 0; |
| 155 | + if (direction == 1) |
| 156 | + { |
| 157 | + row = 0; |
| 158 | + } |
| 159 | + else if (direction == 2) |
| 160 | + { |
| 161 | + row = 1; |
| 162 | + } |
| 163 | + else if (direction == 0) |
| 164 | + { |
| 165 | + row = 2; |
| 166 | + } |
| 167 | + e->img_size.x = frame * e->img_size.w; // set x position based on frame |
| 168 | + e->img_size.y = row * e->img_size.h; // set y position based on direction |
| 169 | + |
| 170 | + Uint32 current_time = SDL_GetTicks(); |
| 171 | + static Uint32 last_time = 0; |
| 172 | + Uint32 delta_time = current_time - last_time; |
| 173 | + if (delta_time >= 100) |
| 174 | + { // add a delay of 100 milliseconds between frames |
| 175 | + frame++; // increment frame counter |
| 176 | + if (frame >= 6) |
| 177 | + { // if we've reached the end of the sprite sheet, wrap around |
| 178 | + frame = 0; |
| 179 | + } |
| 180 | + last_time = current_time; |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +void moveEnemytest(enemy *e) |
| 185 | +{ |
| 186 | + if (e->direction == 2) |
| 187 | + { // move right |
| 188 | + animateEnemy(e, 1); |
| 189 | + e->img_pos.x += e->speed; |
| 190 | + if (e->img_pos.x >= e->x + e->max_steps) |
| 191 | + { |
| 192 | + e->img_pos.x = e->x + e->max_steps; |
| 193 | + e->direction = 0; // change direction to idle |
| 194 | + e->idle_time = SDL_GetTicks(); // start idle time |
| 195 | + } |
| 196 | + } |
| 197 | + else if (e->direction == 1) |
| 198 | + { // move left |
| 199 | + animateEnemy(e, 2); |
| 200 | + e->img_pos.x -= e->speed; |
| 201 | + if (e->img_pos.x <= e->x) |
| 202 | + { |
| 203 | + e->img_pos.x = e->x; |
| 204 | + e->direction = 0; // change direction to idle |
| 205 | + e->idle_time = SDL_GetTicks(); // start idle time |
| 206 | + } |
| 207 | + } |
| 208 | + else if (e->direction == 0) |
| 209 | + { // idle |
| 210 | + animateEnemy(e, 0); |
| 211 | + Uint32 current_time = SDL_GetTicks(); |
| 212 | + if (current_time - e->idle_time >= 2000) |
| 213 | + { // check if idle time is over |
| 214 | + if (e->img_pos.x == e->x) |
| 215 | + { |
| 216 | + e->direction = 1; // change direction to right |
| 217 | + } |
| 218 | + else if (e->img_pos.x == e->x + e->max_steps) |
| 219 | + { |
| 220 | + e->direction = 2; // change direction to left |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments