Skip to content

Commit

Permalink
Fixed IA's
Browse files Browse the repository at this point in the history
  • Loading branch information
xserra93 committed Nov 2, 2014
2 parents 74ff8c5 + 14443f9 commit 9a0818e
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 79 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ set(SOURCE_FILES
src/graphic/effect/timer.cpp
src/graphic/effect/menu.cpp
src/graphic/effect/dialog.cpp
src/graphic/effect/blink.cpp

# Entities
src/entity.cpp
Expand Down
144 changes: 72 additions & 72 deletions res/level/overworld.tmx

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ int Entity::health() const {
}

void Entity::Damage(Entity* from, int damage) {
Sound::Play(hurt_sound_);
health_ -= damage;
if(is_vulnerable_) {
Sound::Play(hurt_sound_);
health_ -= damage;
}
}

bool Entity::IsMob() const {
Expand Down
8 changes: 6 additions & 2 deletions src/entity/mob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void Mob::MeleeAttack(Hitbox* hitbox) {
level_->DynamicCollidablesFor(hitbox, candidates);

for(Rectangle* candidate : candidates) {
if(candidate->CanCollideWith(this) && hitbox->CollidesWith(candidate)) {
if(candidate->CanCollideWith(this) and candidate->CanReceiveDamageFrom(this) and hitbox->CollidesWith(candidate)) {
if(candidate->IsEntity() && ((Entity*) candidate)->IsVulnerable()) {
Collision c = hitbox->CollisionType(candidate);

Expand All @@ -133,7 +133,7 @@ void Mob::MeleeAttack(Hitbox* hitbox) {
void Mob::Damage(Entity* from, int damage) {
super::Damage(from, damage);

if(!current_action_->IsTemporary()) {
if(is_vulnerable_ and !current_action_->IsTemporary()) {
vec2f dir = center() - from->center();
dir.normalize();

Expand Down Expand Up @@ -308,3 +308,7 @@ sf::SoundBuffer* Mob::attack_sound() const {
float Mob::speed() const {
return speed_;
}

bool Mob::CanCollideWith(Rectangle* rectangle) const {
return not rectangle->IsEntity() or ((Entity*)rectangle)->type() != type_;
}
1 change: 1 addition & 0 deletions src/entity/mob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Mob : public Entity {
Entity* SeekEnemy() const ;
Path* FindPath(Entity* to);
bool FollowPath(Path* path, double delta);
bool CanCollideWith(Rectangle* rectangle) const;
float speed() const;

sf::SoundBuffer* attack_sound() const;
Expand Down
20 changes: 17 additions & 3 deletions src/entity/mob/link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "action/move.hpp"
#include "action/attack.hpp"
#include "../../audio/sound.hpp"
#include "../../graphic/effect/blink.hpp"

SpriteSheet* Link::MOVE_SPRITE_SHEET;
std::vector<SpriteSet*> Link::MOVE_ANIMATIONS;
Expand Down Expand Up @@ -73,9 +74,11 @@ int Link::boss_keys() const {

bool Link::CollidesWith(Rectangle const * rectangle) const {
return super::CollidesWith(rectangle) and (
not rectangle->IsEntity() or
((Entity*)rectangle)->type() != BOSS or
rectangle->CollidesWith(this)
(not rectangle->IsEntity()) or
is_vulnerable_ and (
((Entity*)rectangle)->type() != BOSS or
rectangle->CollidesWith(this)
)
);
}

Expand Down Expand Up @@ -122,3 +125,14 @@ bool Link::CanCollideWith(Rectangle *rectangle) const{
}
return true;
}

void Link::Damage(Entity* from, int amount) {
super::Damage(from, amount);

if(is_vulnerable_) {
is_vulnerable_ = false;
ChangeEffect(new Blink(3, 0.1, [this] {
is_vulnerable_ = true;
}));
}
}
2 changes: 2 additions & 0 deletions src/entity/mob/link.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Link : public Mob {
void UpdateSmallKeys(int keys);
void UpdateBossKeys(int keys);

void Damage(Entity* from, int amount);

bool CollidesWith(Rectangle const * rectangle) const;
bool HandleCollisionWith(Mob* mob);

Expand Down
4 changes: 4 additions & 0 deletions src/entity/object/plant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ Plant::Plant(Sprite* sprite, float x, float y) :
type_ = PLANT;
die_sound_ = CUT_SOUND;
}

bool Plant::CanReceiveDamageFrom(Rectangle const* rectangle) const {
return rectangle->IsEntity() and ((Entity*) rectangle)->type() == PLAYER;
}
2 changes: 2 additions & 0 deletions src/entity/object/plant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ class Plant : public Object {
static void Load();

Plant(Sprite* sprite, float x, float y);

bool CanReceiveDamageFrom(Rectangle const * rectangle) const;
};
26 changes: 26 additions & 0 deletions src/graphic/effect/blink.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "blink.hpp"
#include "../drawable.hpp"

Blink::Blink(float duration, float interval, const CallbackEffect::Callback& callback) :
super(duration, callback),
interval_(interval),
show_(false)
{}


void Blink::Render() const {
if(show_)
drawable_->Draw();
}

void Blink::Tick(double delta) {
super::Tick(delta);
drawable_->Update(delta);

if(current_ >= interval_) {
current_ = 0;
show_ = !show_;
}

current_ += delta;
}
16 changes: 16 additions & 0 deletions src/graphic/effect/blink.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "timer.hpp"

class Blink : public Timer {
public:
typedef Timer super;

Blink(float duration, float interval, const CallbackEffect::Callback& callback);

void Tick(double delta);
void Render() const;

private:
float interval_;
float current_;
bool show_;
};
4 changes: 4 additions & 0 deletions src/math/rectangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,7 @@ void Rectangle::Update(double delta) {
float Rectangle::Distance(const Rectangle* rectangle) const {
return center().dist(rectangle->center());
}

bool Rectangle::CanReceiveDamageFrom(Rectangle const* rectangle) const {
return true;
}
1 change: 1 addition & 0 deletions src/math/rectangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Rectangle : public Drawable {

virtual bool CanCollideWith(Rectangle* rectangle) const;
virtual bool CollidesWith(Rectangle const * rectangle) const;
virtual bool CanReceiveDamageFrom(Rectangle const * rectangle) const;
virtual bool HandleCollisionWith(Mob* mob);
virtual Collision CollisionType(Rectangle* rectangle) const;

Expand Down

0 comments on commit 9a0818e

Please sign in to comment.