enemy position and rotation
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
------------ Tyra's v2.0 roadmap to publish on GitHub ------------
|
------------ Tyra's v2.0 roadmap to publish on GitHub ------------
|
||||||
|
|
||||||
- [Demo] Enemy running and rotating to player
|
|
||||||
- [Demo] Crosshair
|
- [Demo] Crosshair
|
||||||
- [Demo] 2D Health bar
|
- [Demo] 2D Health bar
|
||||||
- [Demo] Change enemy to monster
|
- [Demo] Change enemy to monster
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ class Enemy {
|
|||||||
void update(const Heightmap& heightmap, const Vec4& playerPosition);
|
void update(const Heightmap& heightmap, const Vec4& playerPosition);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vec4 direction;
|
|
||||||
void allocateOptions();
|
void allocateOptions();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include <file/file_utils.hpp>
|
#include <file/file_utils.hpp>
|
||||||
|
|
||||||
using Tyra::FileUtils;
|
using Tyra::FileUtils;
|
||||||
|
using Tyra::Math;
|
||||||
using Tyra::ObjLoader;
|
using Tyra::ObjLoader;
|
||||||
using Tyra::ObjLoaderOptions;
|
using Tyra::ObjLoaderOptions;
|
||||||
|
|
||||||
@@ -43,8 +44,6 @@ Enemy::Enemy(TextureRepository* repo) {
|
|||||||
allocateOptions();
|
allocateOptions();
|
||||||
|
|
||||||
pair = new RendererDynamicPair{mesh, options};
|
pair = new RendererDynamicPair{mesh, options};
|
||||||
|
|
||||||
direction = Vec4(1.0F, 0.0F, 1.0F, 0.0F);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Enemy::~Enemy() {
|
Enemy::~Enemy() {
|
||||||
@@ -54,18 +53,25 @@ Enemy::~Enemy() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Enemy::update(const Heightmap& heightmap, const Vec4& playerPosition) {
|
void Enemy::update(const Heightmap& heightmap, const Vec4& playerPosition) {
|
||||||
auto* pos = mesh->getPosition();
|
auto* enemyPosition = mesh->getPosition();
|
||||||
auto nextPos = *pos + direction;
|
|
||||||
|
|
||||||
if (heightmap.isOutside(nextPos)) {
|
auto diff = *enemyPosition - playerPosition;
|
||||||
direction.x = -direction.x;
|
auto ang = Math::atan2(diff.x, diff.z);
|
||||||
direction.z = -direction.z;
|
|
||||||
|
if (diff.length() > 120.0F) {
|
||||||
|
diff.normalize();
|
||||||
|
const float speed = 1.5F;
|
||||||
|
auto nextPos = *enemyPosition - diff * speed;
|
||||||
|
nextPos.y = heightmap.getHeightOffset(nextPos) - 150.0F;
|
||||||
|
|
||||||
|
mesh->translation.identity();
|
||||||
|
mesh->translation.translate(nextPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
nextPos = *pos + direction;
|
const float naturalRotation = 3.1F;
|
||||||
nextPos.y = heightmap.getHeightOffset(nextPos) - 150.0F;
|
mesh->rotation.identity();
|
||||||
mesh->translation.identity();
|
ang += naturalRotation;
|
||||||
mesh->translation.translate(nextPos);
|
mesh->rotation.rotateByAngle(ang, Vec4(0.0F, 1.0F, 0.0F, 0.0F));
|
||||||
|
|
||||||
mesh->animate();
|
mesh->animate();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ void GameState::update() {
|
|||||||
renderer.add(skybox->pair);
|
renderer.add(skybox->pair);
|
||||||
renderer.add(player->pair);
|
renderer.add(player->pair);
|
||||||
renderer.add(enemy->pair);
|
renderer.add(enemy->pair);
|
||||||
renderer.add(terrain->pair);
|
// renderer.add(terrain->pair);
|
||||||
renderer.add(dbgObj->pair);
|
renderer.add(dbgObj->pair);
|
||||||
}
|
}
|
||||||
renderer.render();
|
renderer.render();
|
||||||
|
|||||||
@@ -23,24 +23,24 @@ Terrain::Terrain(TextureRepository* repo)
|
|||||||
Vec4(-750.0F, 0.0F, -1400.0F, 1.0F), // Left up
|
Vec4(-750.0F, 0.0F, -1400.0F, 1.0F), // Left up
|
||||||
Vec4(920.0F, 0.0F, 1520.0F, 1.0F) // Right down
|
Vec4(920.0F, 0.0F, 1520.0F, 1.0F) // Right down
|
||||||
) {
|
) {
|
||||||
ObjLoader loader;
|
// ObjLoader loader;
|
||||||
|
|
||||||
ObjLoaderOptions objOptions;
|
// ObjLoaderOptions objOptions;
|
||||||
objOptions.flipUVs = true;
|
// objOptions.flipUVs = true;
|
||||||
objOptions.scale = 100.0F;
|
// objOptions.scale = 100.0F;
|
||||||
|
|
||||||
auto* data = loader.load(
|
// auto* data = loader.load(
|
||||||
FileUtils::fromCwd("game/models/terrain/terrain.obj"), objOptions);
|
// FileUtils::fromCwd("game/models/terrain/terrain.obj"), objOptions);
|
||||||
data->normalsEnabled = false;
|
// data->normalsEnabled = false;
|
||||||
mesh = new StaticMesh(*data);
|
// mesh = new StaticMesh(*data);
|
||||||
|
|
||||||
delete data;
|
// delete data;
|
||||||
|
|
||||||
repo->addByMesh(mesh, FileUtils::fromCwd("game/models/terrain/"), "png");
|
// repo->addByMesh(mesh, FileUtils::fromCwd("game/models/terrain/"), "png");
|
||||||
|
|
||||||
allocateOptions();
|
// allocateOptions();
|
||||||
|
|
||||||
pair = new RendererStaticPair{mesh, options};
|
// pair = new RendererStaticPair{mesh, options};
|
||||||
}
|
}
|
||||||
|
|
||||||
float Terrain::getHeightOffset(const Vec4& playerPosition) {
|
float Terrain::getHeightOffset(const Vec4& playerPosition) {
|
||||||
|
|||||||
@@ -338,9 +338,9 @@ void M4x4::rotationZ(const float& v) {
|
|||||||
void M4x4::rotationByAngle(const float& angle, const Vec4& axis) {
|
void M4x4::rotationByAngle(const float& angle, const Vec4& axis) {
|
||||||
Vec4 localAxis = Vec4(axis);
|
Vec4 localAxis = Vec4(axis);
|
||||||
localAxis.normalize();
|
localAxis.normalize();
|
||||||
float x = localAxis.x;
|
const float& x = localAxis.x;
|
||||||
float y = localAxis.y;
|
const float& y = localAxis.y;
|
||||||
float z = localAxis.z;
|
const float& z = localAxis.z;
|
||||||
|
|
||||||
float c = Math::cos(angle);
|
float c = Math::cos(angle);
|
||||||
float s = Math::sin(angle);
|
float s = Math::sin(angle);
|
||||||
|
|||||||
Reference in New Issue
Block a user