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 ------------
- [Demo] Enemy running and rotating to player
- [Demo] Crosshair
- [Demo] 2D Health bar
- [Demo] Change enemy to monster
-1
View File
@@ -36,7 +36,6 @@ class Enemy {
void update(const Heightmap& heightmap, const Vec4& playerPosition);
private:
Vec4 direction;
void allocateOptions();
};
+17 -11
View File
@@ -13,6 +13,7 @@
#include <file/file_utils.hpp>
using Tyra::FileUtils;
using Tyra::Math;
using Tyra::ObjLoader;
using Tyra::ObjLoaderOptions;
@@ -43,8 +44,6 @@ Enemy::Enemy(TextureRepository* repo) {
allocateOptions();
pair = new RendererDynamicPair{mesh, options};
direction = Vec4(1.0F, 0.0F, 1.0F, 0.0F);
}
Enemy::~Enemy() {
@@ -54,18 +53,25 @@ Enemy::~Enemy() {
}
void Enemy::update(const Heightmap& heightmap, const Vec4& playerPosition) {
auto* pos = mesh->getPosition();
auto nextPos = *pos + direction;
auto* enemyPosition = mesh->getPosition();
if (heightmap.isOutside(nextPos)) {
direction.x = -direction.x;
direction.z = -direction.z;
auto diff = *enemyPosition - playerPosition;
auto ang = Math::atan2(diff.x, diff.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;
nextPos.y = heightmap.getHeightOffset(nextPos) - 150.0F;
mesh->translation.identity();
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();
}
+1 -1
View File
@@ -78,7 +78,7 @@ void GameState::update() {
renderer.add(skybox->pair);
renderer.add(player->pair);
renderer.add(enemy->pair);
renderer.add(terrain->pair);
// renderer.add(terrain->pair);
renderer.add(dbgObj->pair);
}
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(920.0F, 0.0F, 1520.0F, 1.0F) // Right down
) {
ObjLoader loader;
// ObjLoader loader;
ObjLoaderOptions objOptions;
objOptions.flipUVs = true;
objOptions.scale = 100.0F;
// ObjLoaderOptions objOptions;
// objOptions.flipUVs = true;
// objOptions.scale = 100.0F;
auto* data = loader.load(
FileUtils::fromCwd("game/models/terrain/terrain.obj"), objOptions);
data->normalsEnabled = false;
mesh = new StaticMesh(*data);
// auto* data = loader.load(
// FileUtils::fromCwd("game/models/terrain/terrain.obj"), objOptions);
// data->normalsEnabled = false;
// 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) {
+3 -3
View File
@@ -338,9 +338,9 @@ void M4x4::rotationZ(const float& v) {
void M4x4::rotationByAngle(const float& angle, const Vec4& axis) {
Vec4 localAxis = Vec4(axis);
localAxis.normalize();
float x = localAxis.x;
float y = localAxis.y;
float z = localAxis.z;
const float& x = localAxis.x;
const float& y = localAxis.y;
const float& z = localAxis.z;
float c = Math::cos(angle);
float s = Math::sin(angle);