Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enemy animation #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
153 changes: 153 additions & 0 deletions Enemy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#include "Enemy.h"
#include <math.h>

//SDL_Rect cameraRect;

SpriteManager *enemySpriteManager = NULL;

bool isAlert = false;
bool isHit = false;
bool isDead = false;

int last_rand; // ensure same number doesn't happen twice in a row


// strength is the % chance to attempt to hit the player
Enemy::Enemy(int health, int block_chance, int strength){
this->health = health;
this->block_chance = block_chance;
this->strength = strength;

punching = false;
}

Enemy::~Enemy(){

}

void Enemy::initEnemy()
{
enemySpriteManager = new SpriteManager("assets/KK_ENEMY1_HIT2.png",500,0);
}

// Detects if a given game object is within the given range
bool objectDetected(int playerX, int range) {
bool detected = false;
int xDist = enemySpriteManager->getCharacterXpos() - playerX;
if (abs(xDist) <= range) detected = true;
return detected;
}

// checks if playerX is within hit range
bool withinHitRange(int playerX) {
int xDist = enemySpriteManager->getCharacterXpos() - playerX;
return (abs(xDist) <= 100); // temp
}

// determine if enemy will hit player
bool Enemy::hitPlayer(int &flag) {
srand(time(0));
int rand_num = rand() % 100 + 1;
if (rand_num != last_rand && rand_num < strength) {
srand(time(0));
flag = 5;
std::cout << rand_num << std::endl;
last_rand = rand_num;
return true;
}
return false;
}

bool Enemy::blocked(int &flag) {
// if player is punching, % chance to block
srand(time(0));
int rand_num = rand() % 100 + 1; // value btwn 1 and 100
//std::cout << rand_num << std::endl;
if (rand_num < block_chance) std::cout << "BLOCK" << std::endl;
return (rand_num < block_chance);
}

// isHit is given to determine if enemy should react to being hit
// flag table:
// 0 - calm
// 1 - moving
// 2 - injured
// 3 - dead
// 4 - block
// 5 - punch
void Enemy::updateEnemy(SDL_Rect& cameraRect, int playerX){
int flag = 0;
int step = 0;
if (!isDead) {
// if isHit == true, react to hit
if (isHit) {
if (!blocked(flag)) {
//std::cout << "NOT BLOCKED" << std::endl;
flag = 2;
step = 0;
health -= 5;
if (health <= 0) isDead = true;
//std::cout << "HEALTH: " << health << std::endl;
}
isHit = false;
} else {
// if MC within range, follow keeping 50 sprites away
isAlert = objectDetected(playerX, 250);
if (isAlert) {
//std::cout << "ALERT" << std::endl;
flag = 1;
// check if within hitting range
// if not, move to player
if (withinHitRange(playerX)) {
// enter fight loop
if (hitPlayer(flag)) {
std::cout << "THROW PUNCH" << std::endl;
punching = true;
} else punching = false;

} else {
// move to player
if (getEnemyXpos() - playerX < 0) step = 5;
else step = -5;
}
}
}
} else flag = 3;
enemySpriteManager-> updateEnemySprite(flag, step);

}



void Enemy::renderEnemy(SDL_Rect& cameraRect){
//std::cout << "ENEMY: ";
enemySpriteManager -> renderSprite(cameraRect);
}

int Enemy::getEnemyXpos() {
return enemySpriteManager->getCharacterXpos();
}

int Enemy::getEnemyYpos() {
return enemySpriteManager->getCharacterYpos();
}

void Enemy::setAlertFlag(bool flag) {
isAlert = flag;
}

void Enemy::setHitFlag(bool flag) {
isHit = flag;
}

bool Enemy::enemyIsDead() {
return isDead;
}

bool Enemy::enemyThrewPunch() {
return punching;
}




41 changes: 41 additions & 0 deletions Enemy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef _ENEMY_H_
#define _ENEMY_H_



#include "GameEngine.h"

extern SDL_Rect cameraRect;

class Enemy{
public:
Enemy(int health, int block_chance, int strength);
~Enemy();

void initEnemy();
void updateEnemy(SDL_Rect& cameraRect, int playerX);
void renderEnemy(SDL_Rect& cameraRect);

// ADDED FOR ENEMY COLLISION
int getEnemyXpos();
int getEnemyYpos();

void setAlertFlag(bool flag);
void setHitFlag(bool flag);

bool enemyIsDead();
bool blocked(int &flag);
bool enemyThrewPunch(); // tell if enemy has thrown a punch

private:
int health;
int block_chance;
int strength;

bool punching;

bool hitPlayer(int &flag); // try to hit player

};

#endif
Loading