Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F131599
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
18 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/EnemyEntity.cpp b/src/EnemyEntity.cpp
index a700f0f..6159e69 100644
--- a/src/EnemyEntity.cpp
+++ b/src/EnemyEntity.cpp
@@ -1,417 +1,432 @@
#include "EnemyEntity.h"
#include "PlayerEntity.h"
#include "ExplosionEntity.h"
#include "sfml_game/SpriteEntity.h"
#include "sfml_game/ImageManager.h"
#include "sfml_game/SoundManager.h"
#include "Constants.h"
#include "WitchBlastGame.h"
+const float FACING_DELAY = 0.25f;
+
EnemyEntity::EnemyEntity(sf::Texture* image, float x, float y)
: BaseCreatureEntity (image, x, y, 64, 64)
{
type = ENTITY_ENEMY;
bloodColor = BloodRed;
z = y;
h = 0;
age = -0.001f * (rand()%800) - 0.4f;
deathFrame = -1;
dyingFrame = -1;
dyingSound = SOUND_ENNEMY_DYING;
agonizingSound = SOUND_NONE;
hurtingSound = SOUND_NONE;
isAgonising = false;
enemyType = NB_ENEMY;
meleeLevel = 0;
meleeType = ShotTypeStandard;
canExplode = true;
label_dy = 0;
+
+ nextFacingDirection = 0;
+ facingTimer = -1.0f;
}
enemyTypeEnum EnemyEntity::getEnemyType()
{
return enemyType;
}
+void EnemyEntity::checkNextFacing(float dt)
+{
+ if (facingTimer > 0.0f) facingTimer -= dt;
+ if (facingDirection != nextFacingDirection && facingTimer <= 0.0f)
+ {
+ facingDirection = nextFacingDirection;
+ facingTimer = FACING_DELAY;
+ }
+}
+
void EnemyEntity::setLabelDy(float label_dy)
{
this->label_dy = label_dy;
}
void EnemyEntity::animate(float delay)
{
if (isAgonising)
{
if (hpDisplay > hp) hpDisplay--;
if (h < -0.01f)
{
isAgonising = false;
isDying = true;
game().addCorpse(x, y, deathFrame);
if (dyingSound != SOUND_NONE) SoundManager::getInstance().playSound(dyingSound);
}
else
{
frame = dyingFrame;
hVelocity -= 700.0f * delay;
h += hVelocity * delay;
}
return;
}
if (canCollide()) testSpriteCollisions();
if (age > 0.0f)
BaseCreatureEntity::animate(delay);
else
age += delay;
// FIX enemy not on map
if (x < TILE_WIDTH / 2 || x > MAP_WIDTH * TILE_WIDTH || y < TILE_HEIGHT / 2 || y > MAP_HEIGHT * TILE_HEIGHT)
isDying = true;
}
void EnemyEntity::calculateBB()
{
boundingBox.left = (int)x - width / 2;
boundingBox.width = width;
boundingBox.top = (int)y - height / 2;
boundingBox.height = height;
}
void EnemyEntity::collideMapRight()
{
velocity.x = 0.0f;
}
void EnemyEntity::collideMapLeft()
{
velocity.x = 0.0f;
}
void EnemyEntity::collideMapTop()
{
velocity.y = 0.0f;
}
void EnemyEntity::collideMapBottom()
{
velocity.y = 0.0f;
}
void EnemyEntity::readCollidingEntity(CollidingSpriteEntity* entity)
{
if (!isDying && !isAgonising && collideWithEntity(entity))
{
if (entity->getType() == ENTITY_PLAYER || entity->getType() == ENTITY_BOLT )
{
PlayerEntity* playerEntity = dynamic_cast<PlayerEntity*>(entity);
BoltEntity* boltEntity = dynamic_cast<BoltEntity*>(entity);
if (playerEntity != NULL && !playerEntity->isDead())
{
if (playerEntity->hurt(getHurtParams(meleeDamages, meleeType, meleeLevel, false, SourceTypeBolt, enemyType, false)) > 0)
{
float xs = (x + playerEntity->getX()) / 2;
float ys = (y + playerEntity->getY()) / 2;
SpriteEntity* star = new SpriteEntity(ImageManager::getInstance().getImage(IMAGE_HURT_IMPACT), xs, ys);
star->setFading(true);
star->setZ(y+ 100);
star->setLifetime(0.7f);
star->setType(ENTITY_EFFECT);
star->setSpin(400.0f);
if (playerEntity->isEquiped(EQUIP_ROBE_ADVANCED))
{
giveRecoil(true, Vector2D(playerEntity->getX(), playerEntity->getY()).vectorTo(Vector2D(x, y), 800), 0.8f);
SoundManager::getInstance().playSound(SOUND_ELECTRIC_CHARGE);
star->setScale(1.5f, 1.5f);
star->setColor(sf::Color(220, 180, 255));
star->setLifetime(1.3f);
game().makeColorEffect(X_GAME_COLOR_VIOLET, 0.2f);
}
}
inflictsRecoilTo(playerEntity);
}
else if (boltEntity != NULL && !boltEntity->getDying() && boltEntity->getAge() > 0.05f)
{
collideWithBolt(boltEntity);
}
}
else // collision with other enemy ?
{
if (entity->getType() >= ENTITY_ENEMY && entity->getType() <= ENTITY_ENEMY_MAX)
{
if (this != entity)
{
EnemyEntity* enemyEntity = static_cast<EnemyEntity*>(entity);
if (enemyEntity->canCollide()) collideWithEnemy(enemyEntity);
}
}
}
}
}
void EnemyEntity::collideWithBolt(BoltEntity* boltEntity)
{
float xs = (x + boltEntity->getX()) / 2;
float ys = (y + boltEntity->getY()) / 2;
int maxDamages = hp;
int boltDamages = hurt(getHurtParams
(boltEntity->getDamages(),
boltEntity->getBoltType(),
boltEntity->getLevel(),
boltEntity->isCritical(),
SourceTypeBolt,
enemyType, boltEntity->getGoThrough()));
if (hp > 0)
{
boltEntity->loseDamages(boltEntity->getDamages());
}
else
{
boltEntity->loseDamages(maxDamages >= boltDamages ? boltDamages : maxDamages);
}
if (bloodColor > BloodNone) game().generateBlood(x, y, bloodColor);
SoundManager::getInstance().playSound(SOUND_IMPACT);
SpriteEntity* star = new SpriteEntity(ImageManager::getInstance().getImage(IMAGE_HURT_IMPACT), xs, ys);
star->setFading(true);
star->setZ(y+ 100);
star->setLifetime(0.7f);
star->setType(ENTITY_EFFECT);
star->setSpin(400.0f);
if (boltEntity->getBoltType() == ShotTypeStone)
{
float factor = (boltEntity->isFromPlayer() && game().getPlayer()->isEquiped(EQUIP_RAPID_SHOT)) ? 0.25f : 1.0f;
float recoilVelocity = factor * STONE_DECOIL_VELOCITY[boltEntity->getLevel()];
float recoilDelay = factor * STONE_DECOIL_DELAY[boltEntity->getLevel()];
Vector2D recoilVector = Vector2D(0, 0).vectorTo(boltEntity->getVelocity(),
recoilVelocity );
giveRecoil(true, recoilVector, recoilDelay);
}
boltEntity->collide();
}
int EnemyEntity::getCollisionDirection(BoltEntity* boltEntity)
{
int tol = 4;
float bx = boltEntity->getX();
float by = boltEntity->getY();
int alignX, alignY;
if (bx + tol < x) alignX = 4;
else if (bx - tol > x) alignX = 6;
else alignX = 5;
if (by + tol < y) alignY = 8;
else if (by - tol > y) alignY = 2;
else alignY = 5;
int collisionDir = 5;
if (alignX == 4)
{
if (alignY == 8) collisionDir = 7;
else if (alignY == 5) collisionDir = 4;
else if (alignY == 2) collisionDir = 1;
}
else if (alignX == 5)
{
if (alignY == 8) collisionDir = 8;
else if (alignY == 5) collisionDir = 5;
else if (alignY == 2) collisionDir = 2;
}
else if (alignX == 6)
{
if (alignY == 8) collisionDir = 9;
else if (alignY == 5) collisionDir = 6;
else if (alignY == 2) collisionDir = 3;
}
return collisionDir;
}
void EnemyEntity::collideWithEnemy(EnemyEntity* entity)
{
// To implement the behaviour when colliding with another ennemy
}
int EnemyEntity::hurt(StructHurt hurtParam)
{
int hurtedHp = BaseCreatureEntity::hurt(hurtParam);
if (hurtedHp > 0 && hurtingSound != SOUND_NONE && hp > 0)
SoundManager::getInstance().playSound(hurtingSound);
return hurtedHp;
}
void EnemyEntity::dying()
{
if (dyingFrame > -1)
{
isAgonising = true;
hVelocity = 200.0f;
if (agonizingSound != SOUND_NONE) SoundManager::getInstance().playSound(agonizingSound);
}
else // dyingFrame == -1
{
isDying = true;
game().addCorpse(x, y, deathFrame);
if (dyingSound != SOUND_NONE) SoundManager::getInstance().playSound(dyingSound);
}
if (bloodColor != BloodNone) for (int i = 0; i < 4; i++) game().generateBlood(x, y, bloodColor);
drop();
game().addKilledEnemy(enemyType, hurtingType);
}
void EnemyEntity::dropItem(enumItemType item)
{
ItemEntity* newItem = new ItemEntity(item, x, y);
newItem->setMap(map, TILE_WIDTH, TILE_HEIGHT, 0, 0);
newItem->setVelocity(Vector2D(50.0f + rand()% 140));
newItem->setViscosity(0.96f);
newItem->startsJumping();
}
void EnemyEntity::drop()
{
if (rand() % 40 == 0)
{
if (rand() % 2 == 0)
{
dropItem(ItemScrollRevelation);
}
else
{
dropItem((enumItemType)(ItemPotion01 + rand() % NUMBER_UNIDENTIFIED));
}
}
else
{
if (rand() % 5 == 0)
{
dropItem(ItemCopperCoin);
}
if (game().getPlayer()->isEquiped(EQUIP_LUCK) && rand() % 5 == 0)
{
dropItem(ItemCopperCoin);
}
if (rand() % 25 == 0)
{
dropItem(ItemHealthVerySmall);
}
}
}
bool EnemyEntity::canCollide()
{
return (!isAgonising);
}
void EnemyEntity::render(sf::RenderTarget* app)
{
if (isAgonising || (isDying && dyingFrame > -1))
{
if (shadowFrame > -1)
{
// shadow
int nx = shadowFrame;
int ny = 0;
if (imagesProLine > 0 && shadowFrame >= imagesProLine)
{
nx = shadowFrame % imagesProLine;
ny = shadowFrame / imagesProLine;
}
sprite.setPosition(x, y);
sprite.setTextureRect(sf::IntRect(nx * width, ny * height, width, height));
app->draw(sprite);
}
int nx = dyingFrame;
int ny = 0;
if (imagesProLine > 0)
{
nx = dyingFrame % imagesProLine;
ny = dyingFrame / imagesProLine;
}
sprite.setPosition(x, y - h);
if (isMirroring)
sprite.setTextureRect(sf::IntRect(nx * width + width, ny * height, -width, height));
else
sprite.setTextureRect(sf::IntRect(nx * width, ny * height, width, height));
app->draw(sprite);
}
else
BaseCreatureEntity::render(app);
}
void EnemyEntity::renderLifeBar(sf::RenderTarget* app, std::string label)
{
game().addLifeBarToDisplay(label, hpDisplay, hpMax);
}
bool EnemyEntity::testEntityInMap()
{
int collideCounter = 6;
while (isCollidingWithMap() && collideCounter > 0)
{
int movCounter = 100;
if (collideWithMap(DIRECTION_LEFT) && !collideWithMap(DIRECTION_RIGHT))
{
x = (float)((int)x);
while (collideWithMap(DIRECTION_LEFT) && movCounter > 0)
{
x--;
movCounter--;
}
}
else if (collideWithMap(DIRECTION_RIGHT) && !collideWithMap(DIRECTION_LEFT))
{
x = (float)((int)x);
while (collideWithMap(DIRECTION_RIGHT) && movCounter > 0)
{
x++;
movCounter--;
}
}
else if (collideWithMap(DIRECTION_BOTTOM) && !collideWithMap(DIRECTION_TOP))
{
y = (float)((int)y);
while (collideWithMap(DIRECTION_BOTTOM) && movCounter > 0)
{
y--;
movCounter--;
}
}
else if (collideWithMap(DIRECTION_TOP) && !collideWithMap(DIRECTION_BOTTOM))
{
y = (float)((int)y);
while (collideWithMap(DIRECTION_TOP) && movCounter > 0)
{
y++;
movCounter--;
}
}
collideCounter--;
}
return (collideCounter > 0);
}
diff --git a/src/EnemyEntity.h b/src/EnemyEntity.h
index 101b965..6b914c4 100644
--- a/src/EnemyEntity.h
+++ b/src/EnemyEntity.h
@@ -1,61 +1,65 @@
#ifndef ENNEMYPRITE_H
#define ENNEMYPRITE_H
#include "BaseCreatureEntity.h"
#include "BoltEntity.h"
#include "Items.h"
class EnemyEntity : public BaseCreatureEntity
{
public:
EnemyEntity(sf::Texture* image, float x, float y);
virtual void animate(float delay);
virtual void calculateBB();
virtual void render(sf::RenderTarget* app);
virtual int hurt(StructHurt hurtParam) override;
virtual bool canCollide();
enemyTypeEnum getEnemyType();
void setLabelDy(float label_dy);
+ void checkNextFacing(float dt);
+
protected:
virtual void collideMapRight();
virtual void collideMapLeft();
virtual void collideMapTop();
virtual void collideMapBottom();
void renderLifeBar(sf::RenderTarget* app, std::string label);
virtual void readCollidingEntity(CollidingSpriteEntity* entity);
virtual void dying();
virtual void drop();
void dropItem(enumItemType item);
virtual void collideWithEnemy(EnemyEntity* entity);
virtual void collideWithBolt(BoltEntity* boltEntity);
int getCollisionDirection(BoltEntity* boltEntity);
int meleeDamages;
int meleeLevel;
enumShotType meleeType;
float h; /*!< Vertical position */
float hVelocity; /*!< Vertical velocity */
+ int nextFacingDirection;
+ float facingTimer;
float label_dy; /*!< dy of the bar label for bosses */
int dyingFrame;
int deathFrame;
bool isAgonising;
sound_resources hurtingSound;
sound_resources dyingSound;
sound_resources agonizingSound;
enemyTypeEnum enemyType;
bool testEntityInMap();
private:
};
#endif // ENNEMYPRITE_H
diff --git a/src/RatEntity.cpp b/src/RatEntity.cpp
index 83af53f..28f0578 100644
--- a/src/RatEntity.cpp
+++ b/src/RatEntity.cpp
@@ -1,222 +1,226 @@
#include "RatEntity.h"
#include "BoltEntity.h"
#include "PlayerEntity.h"
#include "sfml_game/SpriteEntity.h"
#include "sfml_game/ImageManager.h"
#include "sfml_game/SoundManager.h"
#include "Constants.h"
#include "WitchBlastGame.h"
RatEntity::RatEntity(float x, float y, ratTypeEnum ratType, bool invocated)
: EnemyEntity (ImageManager::getInstance().getImage(IMAGE_RAT), x, y)
{
this->ratType = ratType;
imagesProLine = 10;
this->invocated = invocated;
doesAccelerate = true;
if (ratType == RatTypeNormal)
{
frame = 1;
dyingFrame = 9;
deathFrame = FRAME_CORPSE_RAT;
if (invocated) enemyType = EnemyTypeRat_invocated;
else enemyType = EnemyTypeRat;
hp = RAT_HP;
creatureSpeed = RAT_SPEED;
}
else //(ratType == RatTypeHelmet)
{
frame = 31;
dyingFrame = 39;
deathFrame = FRAME_CORPSE_RAT_HELMET;
if (invocated) enemyType = EnemyTypeRatHelmet_invocated;
else enemyType = EnemyTypeRatHelmet;
hp = RAT_HP_HELMET;
creatureSpeed = RAT_SPEED_HELMET;
}
direction = rand() % 4;
clockTurn = rand() % 2 == 0;
compute(false);
timer = 6 + rand() % 6;
meleeDamages = RAT_DAMAGES;
bloodColor = BloodRed;
shadowFrame = -1;
agonizingSound = SOUND_RAT_DYING;
sprite.setOrigin(32.0f, 38.0f);
+
+ nextFacingDirection = direction;
}
void RatEntity::animate(float delay)
{
if (age > 0.0f && !isAgonising)
{
timer -= delay;
if (timer < 0.0f)
{
timer = 6 + rand() % 6;
if (rand() % 3 == 0) clockTurn = !clockTurn;
compute(true);
}
+ checkNextFacing(delay);
+
frame = ((int)(age * 7.0f)) % 4;
if (frame == 3) frame = 1;
if (facingDirection == 4 || facingDirection == 6) frame += 3;
isMirroring = (facingDirection == 6 );
if (facingDirection == 8) frame += 6;
if (ratType == RatTypeHelmet) frame += 30;
}
EnemyEntity::animate(delay);
z = y + 17;
}
void RatEntity::compute(bool turn)
{
if (turn)
{
if (clockTurn)
{
direction++;
if (direction == 4) direction = 0;
}
else
{
direction--;
if (direction < 0) direction = 3;
}
}
velocity = Vector2D{0, 0};
float accelerationAbs = (enemyType == EnemyTypeRatHelmet || enemyType == EnemyTypeRatHelmet_invocated) ? (creatureSpeed / 10) : (creatureSpeed / 20);
doesAccelerate = true;
switch (direction)
{
case 0:
acceleration.x = 0;
acceleration.y = -accelerationAbs;
- facingDirection = 8;
+ nextFacingDirection = 8;
break;
case 1:
acceleration.x = accelerationAbs;
acceleration.y = 0;
- facingDirection = 6;
+ nextFacingDirection = 6;
break;
case 2:
acceleration.x = 0;
acceleration.y = accelerationAbs;
- facingDirection = 2;
+ nextFacingDirection = 2;
break;
case 3:
acceleration.x = -accelerationAbs;
acceleration.y = 0;
- facingDirection = 4;
+ nextFacingDirection = 4;
break;
}
}
void RatEntity::calculateBB()
{
boundingBox.left = (int)x - width / 2 + RAT_BB_LEFT;
boundingBox.width = width - RAT_BB_WIDTH_DIFF;
boundingBox.top = (int)y - 13;
boundingBox.height = 31;
}
void RatEntity::collideMapRight()
{
if (recoil.active) recoil.active = false;
compute(true);
}
void RatEntity::collideMapLeft()
{
if (recoil.active) recoil.active = false;
compute(true);
}
void RatEntity::collideMapTop()
{
if (recoil.active) recoil.active = false;
compute(true);
}
void RatEntity::collideMapBottom()
{
if (recoil.active) recoil.active = false;
compute(true);
}
void RatEntity::collideWithEnemy(EnemyEntity* entity)
{
if (entity->getMovingStyle() == movWalking)
{
Vector2D recoilVector = Vector2D(entity->getX(), entity->getY()).vectorTo(Vector2D(x, y), 50.0f);
giveRecoil(false, recoilVector, 0.2f);
compute(true);
}
}
void RatEntity::collideWithBolt(BoltEntity* boltEntity)
{
if (ratType == RatTypeHelmet && boltEntity->getBoltType() != ShotTypeIllusion)
{
int collisionDir = getCollisionDirection(boltEntity);
bool boltCollide = true;
switch (facingDirection)
{
case 4:
if (collisionDir == 7 || collisionDir == 4 || collisionDir == 1) boltCollide = false;
break;
case 2:
if (collisionDir == 1 || collisionDir == 2 || collisionDir == 3) boltCollide = false;
break;
case 6:
if (collisionDir == 9 || collisionDir == 6 || collisionDir == 3) boltCollide = false;
break;
case 8:
if (collisionDir == 7 || collisionDir == 8 || collisionDir == 9) boltCollide = false;
break;
}
if (boltCollide) EnemyEntity::collideWithBolt(boltEntity);
else
{
float xs = (x + boltEntity->getX()) / 2;
float ys = (y + boltEntity->getY()) / 2;
boltEntity->collide();
SpriteEntity* star = new SpriteEntity(ImageManager::getInstance().getImage(IMAGE_HURT_IMPACT), xs, ys);
star->setFading(true);
star->setZ(y+ 100);
star->setLifetime(0.7f);
star->setType(ENTITY_EFFECT);
star->setSpin(400.0f);
SoundManager::getInstance().playSound(SOUND_CLANG_00);
boltEntity->loseDamages(boltEntity->getDamages());
if (boltEntity->getBoltType() == ShotTypeStone)
{
float factor = (boltEntity->isFromPlayer() && game().getPlayer()->isEquiped(EQUIP_RAPID_SHOT)) ? 0.25f : 1.0f;
float recoilVelocity = factor * STONE_DECOIL_VELOCITY[boltEntity->getLevel()];
float recoilDelay = factor * STONE_DECOIL_DELAY[boltEntity->getLevel()];
Vector2D recoilVector = Vector2D(0, 0).vectorTo(boltEntity->getVelocity(),
recoilVelocity );
giveRecoil(true, recoilVector, recoilDelay);
}
}
}
else EnemyEntity::collideWithBolt(boltEntity);
}
void RatEntity::drop()
{
if (!invocated) EnemyEntity::drop();
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Jun 16, 12:17 AM (2 w, 2 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
71345
Default Alt Text
(18 KB)
Attached To
Mode
R78 witchblast
Attached
Detach File
Event Timeline