Merge pull request #7 from CrystalPointA4/enemy

Enemy collision and tracking
This commit is contained in:
2016-05-25 13:44:26 +02:00
22 changed files with 7292 additions and 16 deletions
+3 -2
View File
@@ -42,7 +42,7 @@ void CrystalJohan::draw()
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_LIGHTING);
/*glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glColor4f(1, cos(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), sin(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), 1);
glBegin(GL_TRIANGLES);
@@ -50,7 +50,7 @@ void CrystalJohan::draw()
glVertex2f(mousePosition.x+15, mousePosition.y+15);
glVertex2f(mousePosition.x+5, mousePosition.y+20);
glEnd();
glEnd();*/
glutSwapBuffers();
}
@@ -86,6 +86,7 @@ void CrystalJohan::update()
if (!world->isPlayerPositionValid())
world->player.position = oldPosition;
world->update(deltaTime);
mousePosition = mousePosition + mouseOffset;
+3
View File
@@ -182,6 +182,9 @@
<ClInclude Include="Vertex.h" />
<ClInclude Include="World.h" />
</ItemGroup>
<ItemGroup>
<None Include="worlds\world1.json" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
+3
View File
@@ -99,4 +99,7 @@
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="worlds\world1.json" />
</ItemGroup>
</Project>
+66 -2
View File
@@ -1,12 +1,76 @@
#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);
if (length > 0.03)
{
dx /= length;
dz /= length;
dx *= speed*delta;
dz *= speed*delta;
position.x += dx;
position.z += dz;
}
rotation.y = atan2f(dx, dz) * 180 / M_PI;
}
}
+14 -2
View File
@@ -1,8 +1,20 @@
#pragma once
class Enemy
#include "Entity.h"
#include <string>
#include "Vector.h"
class Enemy : public Entity
{
public:
Enemy();
Enemy(const std::string &fileName,const Vec3f &position,Vec3f &rotation,const float &scale,const bool &hasCollision);
~Enemy();
bool hasTarget;
Vec3f target;
float speed,radius;
void update(float);
void draw();
};
+6
View File
@@ -29,7 +29,13 @@ void Entity::draw()
glRotatef(rotation.z, 0, 0, 1);
glScalef(scale, scale, scale);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
model->draw();
glCullFace(GL_FRONT);
model->draw();
glDisable(GL_CULL_FACE);
glPopMatrix();
}
+5 -1
View File
@@ -7,7 +7,11 @@
class LevelObject : public Entity
{
public:
LevelObject(const std::string &fileName, const Vec3f &position, const Vec3f &rotation, const float &scale, const bool &hasCollision);
LevelObject(const std::string &fileName,
const Vec3f &position,
const Vec3f &rotation,
const float &scale,
const bool &hasCollision);
~LevelObject();
};
+3
View File
@@ -306,6 +306,9 @@ Model::Texture::Texture(const std::string & fileName)
imgData); //data
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
stbi_image_free(imgData);
}
+3 -2
View File
@@ -18,11 +18,12 @@ void Player::setCamera()
void Player::setPosition(float angle, float fac, bool height)
{
fac *= speed;
if (height)
position.y += angle*fac;
else
{
position.x -= (float)cos((rotation.y + angle) / 180 * M_PI) * fac*speed;
position.z -= (float)sin((rotation.y + angle) / 180 * M_PI) * fac*speed;
position.x -= (float)cos((rotation.y + angle) / 180 * M_PI) * fac;
position.z -= (float)sin((rotation.y + angle) / 180 * M_PI) * fac;
}
}
+52
View File
@@ -1,3 +1,5 @@
#define _USE_MATH_DEFINES
#include <cmath>
#include "Vector.h"
Vec3f::Vec3f(float x, float y, float z)
@@ -6,6 +8,29 @@ Vec3f::Vec3f(float x, float y, float z)
this->y = y;
this->z = z;
}
/*The length of the vector*/
float Vec3f::Length()
{
return (float)sqrt(x*x+y*y+z*z);
}
void Vec3f::Normalize()
{
float length = this->Length();
if (length != 0)
{
x = x / length;
y = y / length;
z = z / length;
}
}
float Vec3f::Distance(const Vec3f &other)
{
return (float)sqrt(pow(other.x - x, 2)+pow(other.y - y, 2)+pow(other.z - z, 2));
}
Vec3f::Vec3f()
{
this->x = 0;
@@ -29,11 +54,32 @@ Vec3f Vec3f::operator+(const Vec3f & other)
return Vec3f(x + other.x, y + other.y, z + other.z);
}
Vec3f Vec3f::operator-(const Vec3f & other)
{
return Vec3f(x - other.x, y - other.y, z - other.z);
}
Vec3f Vec3f::operator/(float value)
{
return Vec3f(x / value, y / value, z / value);
}
bool Vec3f::operator==(const Vec3f & other)
{
/*bool xb, yb,zb;
xb = x == other.x;
yb = y == other.y;
zb = z == other.z;
if (xb & yb & zb)
return true;*/
return x == other.x & y == other.y & z == other.z;
}
bool Vec3f::operator!=(const Vec3f & other)
{
return x != other.x & y != other.y & z != other.z;
}
Vec2f::Vec2f(float x, float y)
@@ -61,3 +107,9 @@ Vec2f Vec2f::operator+(const Vec2f & other)
{
return Vec2f(x + other.x, y+other.y);
}
float Vec2f::length()
{
return (float)sqrt(x*x + y*y);
}
+7
View File
@@ -14,9 +14,15 @@ public:
Vec3f();
Vec3f(const Vec3f &other);
Vec3f(float x, float y, float z);
float Length();
void Normalize();
float Distance(const Vec3f &);
float& operator [](int);
Vec3f operator + (const Vec3f &other);
Vec3f operator -(const Vec3f &other);
Vec3f operator / (float value);
bool operator ==(const Vec3f &other);
bool operator !=(const Vec3f &other);
};
class Vec2f
@@ -35,5 +41,6 @@ public:
Vec2f(const Vec2f &other);
float& operator [](int);
Vec2f operator + (const Vec2f &other);
float length();
};
+80 -4
View File
@@ -8,6 +8,7 @@
World::World() : player(Player::getInstance())
{
std::ifstream file("worlds/world1.json");
if(!file.is_open())
std::cout<<"Uhoh, can't open file\n";
@@ -22,6 +23,7 @@ World::World() : player(Player::getInstance())
player.position.y = v["player"]["startposition"][1];
player.position.z = v["player"]["startposition"][2];
for (auto object : v["objects"])
{
bool hasCollision = true;
@@ -39,6 +41,49 @@ World::World() : player(Player::getInstance())
Vec3f position(object["pos"][0], object["pos"][1], object["pos"][2]);
entities.push_back(new LevelObject(object["file"], position, rotation, scale, hasCollision));
}
//look up table for the enemies
std::vector<std::pair<int, std::string>>enemy_models;
for (auto enemy_model : v["enemy_models"])
{
int id = -1;
if (!enemy_model["id"].isNull())
id = enemy_model["id"].asInt();
std::string fileName = "";
if (!enemy_model["file"].isNull())
fileName = enemy_model["file"].asString();
enemy_models.push_back(std::pair<int, std::string>(id,fileName));
}
for (auto enemy : v["enemy_data"])
{
int id = -1;
if (!enemy["id"].isNull())
id = enemy["id"];
for (auto enemy_model : enemy_models)
{
if (id == enemy_model.first)
{
Vec3f position(0, 0, 0);
if (!enemy["pos"].isNull())
position = Vec3f(enemy["pos"][0], enemy["pos"][1], enemy["pos"][2]);
Vec3f rotation(0, 0, 0);
if (!enemy["rot"].isNull())
rotation = Vec3f(enemy["rot"][0], enemy["rot"][1], enemy["rot"][2]);
float scale = 1.0f;
if (!enemy["scale"].isNull())
scale = enemy["scale"].asFloat();
enemies.push_back(new Enemy(enemy_model.second,position,rotation,scale,true));
}
}
}
}
@@ -58,15 +103,46 @@ void World::draw()
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
heightmap->Draw();
for (auto e : entities)
e->draw();
for (auto &enemy : enemies)
enemy->draw();
for (auto &entity : entities)
entity->draw();
}
void World::update(float elapsedTime)
{
for (auto e : entities)
e->update(elapsedTime);
for (auto &entity : entities)
entity->update(elapsedTime);
for (auto &enemy : enemies)
{
if (enemy->position.Distance(player.position) <= enemy->radius)
{
enemy->hasTarget = true;
enemy->target.x = player.position.x;
enemy->target.z = player.position.z;
}
else
enemy->hasTarget = false;
Vec3f oldpos = enemy->position;
enemy->update(elapsedTime);
if (enemy->hasTarget)
{
for (auto e : entities)
{
if (e->canCollide && e->inObject(enemy->position))
{
enemy->position = oldpos;
break;
}
}
}
}
}
bool World::isPlayerPositionValid()
+4
View File
@@ -3,6 +3,7 @@
#include <vector>
#include "HeightMap.h"
#include "Player.h"
#include "Enemy.h"
class Entity;
@@ -15,6 +16,9 @@ public:
Player& player;
std::vector<Entity*> entities;
std::vector<Enemy*> enemies;
HeightMap* heightmap;
void draw();
+19
View File
@@ -0,0 +1,19 @@
newmtl CylinderSG
illum 4
Kd 0.00 0.00 0.00
Ka 0.00 0.00 0.00
Tf 1.00 1.00 1.00
map_Kd ^D70CEF585F0EAF59BA1D4FC14B4011137E15ED44D07FA7B93F^pimgpsh_fullsize_distr.png
Ni 1.00
newmtl Oil_TankSG
illum 4
Kd 0.00 0.00 0.80
Ka 0.00 0.00 0.00
Tf 1.00 1.00 1.00
Ni 1.00
newmtl Oil_TankSG1
illum 4
Kd 0.43 0.63 0.80
Ka 0.00 0.00 0.00
Tf 1.00 1.00 1.00
Ni 1.00
File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB

Binary file not shown.
+9
View File
@@ -0,0 +1,9 @@
# Created by Every File Explorer
newmtl GessoMat00
Ka 1 1 1
Kd 1 1 1
Ks 0.05098039 0.05098039 0.05098039
d 1
map_Kd enemy_xx01_Gesso_dif.png
File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 606 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 345 B

+17 -3
View File
@@ -9,11 +9,25 @@
"objects": [
{
"file": "models/boom/Boom.obj",
"pos": [ 0, 0, -4 ]
"pos": [ 4, 0, -4 ]
},
{
"file": "models/boom/Boom.obj",
"pos": [ 4, 0, -4 ]
"file": "models/Teleporter/Teleporter.obj",
"pos": [ 0, 0, -4 ],
"rot": [ 0, 0, 0 ]
}
],
"enemy_models": [
{
"id": 0,
"file": "models/squid/Blooper.obj"
}
],
"enemy_data": [
{
"id": 0,
"pos": [ 1, 2, -10 ],
"scale": 0.01
}
]
}