enemy comes to you when in his radius

This commit is contained in:
Remco
2016-05-23 08:48:23 +02:00
parent a8415db4ac
commit d01efecb0e
22 changed files with 7274 additions and 21 deletions
+3 -2
View File
@@ -40,7 +40,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);
@@ -48,7 +48,7 @@ void CrystalJohan::draw()
glVertex2f(mousePosition.x+15, mousePosition.y+15);
glVertex2f(mousePosition.x+5, mousePosition.y+20);
glEnd();
glEnd();*/
@@ -87,6 +87,7 @@ void CrystalJohan::update()
if (!world->isPlayerPositionValid())
world->player.position = oldPosition;
world->update(deltaTime);
mousePosition = mousePosition + mouseOffset;
+3
View File
@@ -178,6 +178,9 @@
<ClInclude Include="vector.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
@@ -87,4 +87,7 @@
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="worlds\world1.json" />
</ItemGroup>
</Project>
+69 -2
View File
@@ -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 ;
}
+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
@@ -30,7 +30,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
@@ -289,6 +289,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;
}
}
+35
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,17 @@ 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);
}
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;
@@ -34,6 +47,22 @@ 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 +90,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);
}
+5
View File
@@ -14,9 +14,13 @@ public:
Vec3f();
Vec3f(Vec3f &other);
Vec3f(float x, float y, float z);
float Length();
float Distance(const Vec3f &);
float& operator [](int);
Vec3f operator + (const Vec3f &other);
Vec3f operator / (float value);
bool operator ==(const Vec3f &other);
bool operator !=(const Vec3f &other);
};
class Vec2f
@@ -35,5 +39,6 @@ public:
Vec2f(Vec2f &other);
float& operator [](int);
Vec2f operator + (const Vec2f &other);
float length();
};
+75 -7
View File
@@ -4,14 +4,20 @@
#include "LevelObject.h"
#include "json.h"
#include <fstream>
#include <string>
#include <iostream>
World::World() : player(Player::getInstance())
{
json::Value v = json::readJson(std::ifstream("worlds/world1.json"));
player.position.x = v["player"]["startposition"][0];
player.position.y = v["player"]["startposition"][1];
player.position.z = v["player"]["startposition"][2];
if (!v["player"]["startposition"].isNull())
{
player.position.x = v["player"]["startposition"][0];
player.position.y = v["player"]["startposition"][1];
player.position.z = v["player"]["startposition"][2];
}
for (auto object : v["objects"])
{
@@ -30,6 +36,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"];
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));
}
}
}
}
@@ -55,15 +104,34 @@ void World::draw()
glVertex3f(50, 0, -50);
glEnd();
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;
enemy->update(elapsedTime);
}
}
bool World::isPlayerPositionValid()
+2 -1
View File
@@ -2,6 +2,7 @@
#include <vector>
#include "Player.h"
#include "Enemy.h"
class Entity;
@@ -14,7 +15,7 @@ public:
Player& player;
std::vector<Entity*> entities;
std::vector<Enemy*> enemies;
void draw();
void update(float elapsedTime);
bool isPlayerPositionValid();
+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

+22 -4
View File
@@ -5,12 +5,30 @@
"objects": [
{
"file": "models/boom/boom.obj",
"pos": [ 0, 0, -4 ],
"rot": [ 0, 20, 0 ]
"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
},
{
"id": 0,
"pos": [ 4, 2, -15 ],
"scale": 0.01
}
]
}