From fea77281399e9ee3998c97b1524e9fa5e5bb6a6a Mon Sep 17 00:00:00 2001 From: Remco Date: Wed, 22 Jun 2016 00:26:43 +0200 Subject: [PATCH] enemy hits player --- Enemy.cpp | 19 ++++++++++++++++--- Enemy.h | 4 +++- Main.cpp | 5 +++-- World.cpp | 14 +++++++------- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Enemy.cpp b/Enemy.cpp index d46e4fb..d86a114 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -28,7 +28,9 @@ Enemy::Enemy(const std::string &fileName, hasTarget = false; hit_sound_id = CrystalPoint::GetSoundSystem().LoadSound(fileMusic.c_str(), false); music = CrystalPoint::GetSoundSystem().GetSound(hit_sound_id); - attack = false; + canAttack = false; + lastAttacked = 0; + } @@ -80,6 +82,17 @@ void Enemy::collide(const Entity * entity) position.z = difference.z + entity->position.z; } +bool Enemy::attack() +{ + float timeNow = glutGet(GLUT_ELAPSED_TIME)/1000.0f; + if (canAttack && timeNow - lastAttacked > 0.8) + { + lastAttacked = timeNow; + return true; + } + return false; +} + void Enemy::update(float delta) { music->SetPos(position, Vec3f()); @@ -99,7 +112,7 @@ void Enemy::update(float delta) length = sqrt(dx*dx + dz*dz); if (length > 1) { - attack = false; + canAttack = false; dx /= length; dz /= length; @@ -112,7 +125,7 @@ void Enemy::update(float delta) } else { - attack = true; + canAttack = true; } rotation.y = atan2f(dx, dz) * 180 / M_PI; } diff --git a/Enemy.h b/Enemy.h index eb503b4..10b53d8 100644 --- a/Enemy.h +++ b/Enemy.h @@ -19,7 +19,9 @@ public: float health; float damage; int xp; - bool attack; + bool canAttack; + float lastAttacked; + bool attack(); void update(float); void draw(); diff --git a/Main.cpp b/Main.cpp index 6a41a7a..786708f 100644 --- a/Main.cpp +++ b/Main.cpp @@ -79,8 +79,9 @@ void configureOpenGL() { //Init window and glut display mode glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); - glutInitWindowSize(1440, 900); - //glutInitWindowPosition(glutGet(GLUT_WINDOW_WIDTH) / 2 - 800/2, glutGet(GLUT_WINDOW_HEIGHT) / 2 - 600/2); + glutInitWindowSize(1440, 900); + //glutPositionWindow((glutGet(GLUT_SCREEN_WIDTH) / 2) - (glutGet(GLUT_WINDOW_WIDTH) / 2), (glutGet(GLUT_SCREEN_HEIGHT) / 2) - (glutGet(GLUT_WINDOW_HEIGHT) / 2)); + glutCreateWindow("Crystal Point"); //glutFullScreen(); diff --git a/World.cpp b/World.cpp index b2d282b..4caa231 100644 --- a/World.cpp +++ b/World.cpp @@ -313,7 +313,7 @@ void World::update(float elapsedTime) //Al deze code zou in enemy moeten staan enemy->inEyeSight(player->position); - + enemy->update(elapsedTime); if (enemy->hasTarget) @@ -326,12 +326,11 @@ void World::update(float elapsedTime) break; } } + } - if (enemy->attack) - { - remove = true; - continue; - } + if (enemy->attack()) + { + player->HpDown(enemy->damage); } enemy->position.y = getHeight(enemy->position.x, enemy->position.z) + 2.0f; @@ -342,7 +341,8 @@ void World::update(float elapsedTime) if (remove) { player->XpUp(enemies[count]->xp); - delete enemies[count]; + delete enemies[count]; + enemies.erase(enemies.begin() + count); player->HpUp(10); }