enemy position and rotation

This commit is contained in:
h4570
2022-08-12 10:24:33 +02:00
parent cbe346900d
commit d65561487c
6 changed files with 33 additions and 29 deletions
-1
View File
@@ -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
-1
View File
@@ -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();
}; };
+15 -9
View File
@@ -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;
}
nextPos = *pos + direction; if (diff.length() > 120.0F) {
diff.normalize();
const float speed = 1.5F;
auto nextPos = *enemyPosition - diff * speed;
nextPos.y = heightmap.getHeightOffset(nextPos) - 150.0F; nextPos.y = heightmap.getHeightOffset(nextPos) - 150.0F;
mesh->translation.identity(); mesh->translation.identity();
mesh->translation.translate(nextPos); mesh->translation.translate(nextPos);
}
const float naturalRotation = 3.1F;
mesh->rotation.identity();
ang += naturalRotation;
mesh->rotation.rotateByAngle(ang, Vec4(0.0F, 1.0F, 0.0F, 0.0F));
mesh->animate(); mesh->animate();
} }
+1 -1
View File
@@ -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();
+12 -12
View File
@@ -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) {
+3 -3
View File
@@ -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);