enemy comes to you when in his radius
This commit is contained in:
@@ -1,12 +1,79 @@
|
||||
#define _USE_MATH_DEFINES
|
||||
#include <cmath>
|
||||
|
||||
#include "Enemy.h"
|
||||
#include "Model.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
|
||||
Enemy::Enemy()
|
||||
Enemy::Enemy(const std::string &fileName,
|
||||
const Vec3f &position,
|
||||
Vec3f &rotation,
|
||||
const float &scale,
|
||||
const bool &hasCollision)
|
||||
{
|
||||
model = Model::load(fileName);
|
||||
this->position = position;
|
||||
this->rotation = rotation;
|
||||
this->scale = scale;
|
||||
this->canCollide = hasCollision;
|
||||
target = position;
|
||||
speed = 1;
|
||||
radius = 10;
|
||||
hasTarget = false;
|
||||
}
|
||||
|
||||
|
||||
Enemy::~Enemy()
|
||||
{
|
||||
if (model)
|
||||
Model::unload(model);
|
||||
}
|
||||
|
||||
void Enemy::draw()
|
||||
{
|
||||
Entity::draw();
|
||||
glPushMatrix();
|
||||
|
||||
glTranslatef(position.x, position.y, position.z);
|
||||
|
||||
glBegin(GL_LINE_LOOP);
|
||||
for (int i = 0; i < 360; i++)
|
||||
{
|
||||
//convert degrees into radians
|
||||
float degInRad = i*(M_PI / 180.0);
|
||||
glVertex3f(cos(degInRad)*radius, 1*scale,sin(degInRad)*radius);
|
||||
}
|
||||
glEnd();
|
||||
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
void Enemy::update(float delta)
|
||||
{
|
||||
if (hasTarget)
|
||||
{
|
||||
//just 2d walking
|
||||
float dx, dz, length;
|
||||
|
||||
dx = target.x - position.x;
|
||||
dz = target.z - position.z;
|
||||
|
||||
length = sqrt(dx*dx + dz*dz);
|
||||
dx /= length;
|
||||
dz /= length;
|
||||
|
||||
dx *= speed*delta;
|
||||
dz *= speed*delta;
|
||||
|
||||
position.x += dx;
|
||||
position.z += dz;
|
||||
}
|
||||
|
||||
/*rotation.y += 10.0f;
|
||||
if (rotation.y >= 360.0f)
|
||||
rotation.y = 0.0f;*/
|
||||
//std::cout << atan2f(target.z - position.z, target.x - position.x) * 180 / M_PI << std::endl;
|
||||
//rotation.y = atan2f(target.z - position.z, target.x - position.x) * 180 / M_PI ;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user