From 4249e3565313430656033fcc83ec6acf6dfaef6a Mon Sep 17 00:00:00 2001 From: jancoow Date: Tue, 21 Jun 2016 17:00:36 +0200 Subject: [PATCH] Check if weapon collision point is inside enemy and do weapon damage --- CrystalPoint.cpp | 3 +++ Enemy.cpp | 20 +++++++++++++++++++- Enemy.h | 3 +++ Player.cpp | 4 ++-- Weapon.cpp | 5 +++-- Weapon.h | 4 ++-- World.cpp | 1 + 7 files changed, 33 insertions(+), 7 deletions(-) diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp index 8e9c54b..a37683f 100644 --- a/CrystalPoint.cpp +++ b/CrystalPoint.cpp @@ -151,6 +151,9 @@ void CrystalPoint::update() } else { + if (keyboardState.keys[27]){ + glutExit(); + } menu->update(); cursor->update(cursor->mousePosition + mouseOffset); } diff --git a/Enemy.cpp b/Enemy.cpp index 42e5019..0368ba9 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -2,6 +2,7 @@ #include #include "Enemy.h" #include "Model.h" +#include "Player.h" #include Enemy::Enemy(const std::string &fileName, @@ -75,6 +76,10 @@ void Enemy::collide(const Entity * entity) position.z = difference.z + entity->position.z; } +void Enemy::hit(int damage){ + +} + void Enemy::update(float delta) { music->SetPos(position, Vec3f()); @@ -117,5 +122,18 @@ void Enemy::update(float delta) rotation.y = atan2f(dx, dz) * 180 / M_PI; } - + Player *player = Player::getInstance(); + if(inObject(player->leftWeapon->collisionPoint)){ + if(!isHit){ + isHit = true; + hit(player->leftWeapon->damage); + } + }else if(inObject(player->rightWeapon->collisionPoint)){ + if(!isHit){ + isHit = true; + hit(player->rightWeapon->damage); + } + }else{ + isHit = false; + } } \ No newline at end of file diff --git a/Enemy.h b/Enemy.h index 61ab4f4..cf9d301 100644 --- a/Enemy.h +++ b/Enemy.h @@ -19,11 +19,14 @@ public: int xp; bool attack; + bool isHit; + void update(float); void draw(); void inEyeSight(Vec3f &); void collide(const Entity *entity); + void hit(int damage); private: int hit_sound_id; }; diff --git a/Player.cpp b/Player.cpp index 303a9d4..9948c14 100644 --- a/Player.cpp +++ b/Player.cpp @@ -15,10 +15,10 @@ Player::Player() level = 1; crystals = 0; - leftWeapon = new Weapon("models/weapons/ZwaardMetTextures/TextureZwaard.obj", 1, position, rotation, Vec3f(4.5, -8, -1), Vec3f(-2.0f, 6.0f, -2.1f), Vec2f(170, 70), Vec2f(20, -80)); + leftWeapon = new Weapon("models/weapons/ZwaardMetTextures/TextureZwaard.obj", 1, position, rotation, Vec3f(4.5, -8, -1), Vec3f(-2.0f, 6.0f, -2.1f), Vec3f(-2.0f, 0.5f, -2.1f), Vec2f(170, 70), Vec2f(20, -80)); leftWeapon->rotateWeapon(Vec3f(150, 0, 60)); - rightWeapon = new Weapon("models/weapons/ZwaardMetTextures/TextureZwaard.obj", 1, position, rotation, Vec3f(0.5, -8, -1), Vec3f(-2.0f, 6.0f, -2.1f), Vec2f(170, 70), Vec2f(20, -80)); + rightWeapon = new Weapon("models/weapons/ZwaardMetTextures/TextureZwaard.obj", 1, position, rotation, Vec3f(0.5, -8, -1), Vec3f(-2.0f, 6.0f, -2.1f), Vec3f(-2.0f, 0.5f, -2.1f), Vec2f(170, 70), Vec2f(20, -80)); rightWeapon->rotateWeapon(Vec3f(150, 0, 60)); } diff --git a/Weapon.cpp b/Weapon.cpp index 7a26355..55e1514 100644 --- a/Weapon.cpp +++ b/Weapon.cpp @@ -10,7 +10,7 @@ Weapon::Weapon(std::string modelFilename, float scale, Vec3f location, Vec2f rotation, - Vec3f offsetPlayer, Vec3f ankerPoint, + Vec3f offsetPlayer, Vec3f ankerPoint, Vec3f collisionPoint, Vec2f maxRotation, Vec2f minRotation){ weaponmodel = Model::load(modelFilename); rotate(rotation); @@ -21,6 +21,7 @@ Weapon::Weapon(std::string modelFilename, float scale, Vec3f location, Vec2f rot this->ankerPoint = ankerPoint; this->maxRotation = maxRotation; this->minRotation = minRotation; + this->collisionPoint = collisionPoint; }; Weapon::~Weapon(){ @@ -72,7 +73,7 @@ void Weapon::draw(){ //Test code for finding anker point glColor3ub(255, 255, 0); - glTranslatef(ankerPoint.x, ankerPoint.y, ankerPoint.z); + glTranslatef(collisionPoint.x, collisionPoint.y, collisionPoint.z); glBegin(GL_LINES); glVertex2f(0, 4); glVertex2f(0, -4); diff --git a/Weapon.h b/Weapon.h index 8d5e672..b31f5e9 100644 --- a/Weapon.h +++ b/Weapon.h @@ -12,7 +12,7 @@ class Weapon { public: Weapon(std::string modelFilename, float scale, Vec3f location, Vec2f rotation, - Vec3f offsetPlayer, Vec3f ankerPoint, + Vec3f offsetPlayer, Vec3f ankerPoint, Vec3f collisionPoint, Vec2f maxRotation, Vec2f minXRotation); ~Weapon(); @@ -26,7 +26,7 @@ public: float scale; Vec3f position, rotation, rotationWeapon; - Vec3f offsetPlayer, ankerPoint; + Vec3f offsetPlayer, ankerPoint, collisionPoint; Vec2f maxRotation, minRotation; }; diff --git a/World.cpp b/World.cpp index ebf2d52..adbd7f2 100644 --- a/World.cpp +++ b/World.cpp @@ -351,3 +351,4 @@ bool World::isPlayerPositionValid() } return true; } +