Skip to content

Commit

Permalink
start on implementing AI (and zombie stuff)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kade-github committed May 11, 2024
1 parent cbb9f04 commit a64d9fc
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 34 deletions.
48 changes: 24 additions & 24 deletions Assets/Models/zombie_frim.obj
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ f 6/13/4 2/14/4 4/15/4 8/16/4
f 2/17/5 1/18/5 3/19/5 4/20/5
f 6/21/6 5/22/6 1/23/6 2/24/6
o Head
v -0.321279 0.886969 0.396589
v -0.321279 1.637983 0.396589
v -0.321279 0.886969 -0.396589
v -0.321279 1.637983 -0.396589
v 0.321279 0.886969 0.396589
v 0.321279 1.637983 0.396589
v 0.321279 0.886969 -0.396589
v 0.321279 1.637983 -0.396589
v -0.352997 0.905410 0.347135
v -0.352997 1.562773 0.347135
v -0.352997 0.905410 -0.347135
v -0.352997 1.562773 -0.347135
v 0.352997 0.905410 0.347135
v 0.352997 1.562773 0.347135
v 0.352997 0.905410 -0.347135
v 0.352997 1.562773 -0.347135
vn -1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 -1.0000
vn 1.0000 -0.0000 -0.0000
Expand Down Expand Up @@ -95,14 +95,14 @@ f 13/37/10 14/38/10 10/39/10 9/40/10
f 11/41/11 15/42/11 13/43/11 9/44/11
f 16/45/12 12/46/12 10/47/12 14/48/12
o RightArm
v -0.211492 -0.316166 -0.340364
v -0.211492 -0.316166 -0.630798
v 0.205802 -0.316166 -0.340363
v 0.205802 -0.316166 -0.630798
v -0.211492 0.879596 -0.340364
v -0.211492 0.879596 -0.630798
v 0.205802 0.879596 -0.340363
v 0.205802 0.879596 -0.630798
v -0.162334 -0.316166 -0.340363
v -0.162334 -0.316166 -0.630798
v 0.156643 -0.316166 -0.340363
v 0.156643 -0.316166 -0.630798
v -0.162334 0.879596 -0.340363
v -0.162334 0.879596 -0.630798
v 0.156643 0.879596 -0.340363
v 0.156643 0.879596 -0.630798
vn -0.0000 -1.0000 -0.0000
vn 1.0000 -0.0000 -0.0000
vn -0.0000 1.0000 -0.0000
Expand Down Expand Up @@ -141,14 +141,14 @@ f 21/61/16 22/62/16 18/63/16 17/64/16
f 19/65/17 23/66/17 21/67/17 17/68/17
f 24/69/18 20/70/18 18/71/18 22/72/18
o LeftArm
v -0.211492 -0.316166 0.630227
v -0.211492 -0.316166 0.339793
v 0.205802 -0.316166 0.630227
v 0.205802 -0.316166 0.339793
v -0.211492 0.879596 0.630227
v -0.211492 0.879596 0.339793
v 0.205802 0.879596 0.630227
v 0.205802 0.879596 0.339793
v -0.162334 -0.316166 0.630227
v -0.162334 -0.316166 0.339793
v 0.156643 -0.316166 0.630227
v 0.156643 -0.316166 0.339793
v -0.162334 0.879596 0.630227
v -0.162334 0.879596 0.339793
v 0.156643 0.879596 0.630227
v 0.156643 0.879596 0.339793
vn -0.0000 -1.0000 -0.0000
vn 1.0000 -0.0000 -0.0000
vn -0.0000 1.0000 -0.0000
Expand Down
9 changes: 9 additions & 0 deletions Assets/Shaders/frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ uniform sampler2D ourTexture;
uniform vec3 CameraPos;
uniform float FogFar;
uniform vec3 FogColor;
uniform float lightLevel = 10;
float FogMin = 0.0;

float getFogFactor(float d)
Expand All @@ -30,7 +31,15 @@ void main()
float fogFactor = getFogFactor(d);

if (FogFar < 1000)
{
FragColor = mix(color, vec4(FogColor, 1.0), fogFactor);

// darken based on the light level

float light = max(0.1, lightLevel / 10.0);

FragColor = vec4(FragColor.rgb * light, FragColor.a);
}
else
FragColor = color;
}
Binary file modified Assets/Textures/zombie_frim.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ glad.c
"Game/Objects/Base/FallingBlock.h" "Game/Objects/Base/FallingBlock.cpp" "Game/Objects/Base/Blocks/Sand.cpp" "Game/Objects/Base/Blocks/NullBlock.h" "Game/Objects/Base/Blocks/Torch.h" "Game/Objects/Base/Blocks/Torch.cpp" "Game/Objects/Base/Blocks/Furnace.h" "Game/Objects/Base/Blocks/Furnace.cpp"

"Game/Objects/Base/Blocks/RuinedCobblestone.h"
"Game/Objects/Base/Blocks/RuinedDebris.h" "Game/Objects/Base/Blocks/ReinforcedIronBlock.h")
"Game/Objects/Base/Blocks/RuinedDebris.h" "Game/Objects/Base/Blocks/ReinforcedIronBlock.h" "Game/Objects/Base/Zombie.h" "Game/Objects/Base/AI.h" "Game/Objects/Base/AI.cpp")

link_directories(bassLibrary)

add_executable(TheFrim ${SOURCE_FILES})
add_executable(TheFrim ${SOURCE_FILES} "Game/Objects/Base/Zombie.cpp")

set_target_properties(TheFrim PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
Expand Down
20 changes: 19 additions & 1 deletion src/Engine/OpenGL/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ void Model::loadModel(std::string path)
directory = path.substr(0, path.find_last_of('/'));

processNode(scene->mRootNode, scene);

// get names from the file

std::fstream file;

file.open(path);

std::string line;
int i = 0;
while (std::getline(file, line))
{
if (line.find("o ") != std::string::npos)
{
std::string name = line.substr(2, line.size() - 2);
meshes[i].name = name;
i++;
}
}
}

void Model::processNode(aiNode* node, const aiScene* scene)
Expand Down Expand Up @@ -103,7 +121,7 @@ void Model::Draw()
glm::mat4 model = glm::mat4(1.0f);

model = glm::translate(model, position + mesh.position);
model = glm::rotate(model, glm::radians(angle + mesh.angle), rotateAxis);
model = glm::rotate(model, glm::radians(angle + mesh.angle), mesh.axis);
model = glm::scale(model, scale * mesh.scale);

Game::instance->shader->SetUniformMat4f("model", &model[0][0]);
Expand Down
65 changes: 65 additions & 0 deletions src/Game/Objects/Base/AI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "AI.h"
#include "../../WorldManager.h"

glm::vec3 AI::GenerateRandomPosition(float distance)
{
// get a random position within a distance from the current position

// get a random angle

float angle = (rand() % 360) * 3.14159 / 180;

// get a random distance

float dist = (rand() % (int)distance) + 1;

// calculate the new position

glm::vec3 newPos = glm::vec3(position.x + cos(angle) * dist, position.y, position.z + sin(angle) * dist);

Chunk* c = WorldManager::instance->GetChunk(newPos.x, newPos.z);

if (c == nullptr)
return glm::vec3();

// get the lowest block from our position

for (int y = (int)newPos.y; y > -1; y--)
{
if (c->GetBlock(newPos.x, y, newPos.z) > 0)
{
newPos.y = y + 1;
break;
}
}

return newPos;
}

void AI::MoveTo(glm::vec3 pos)
{
target = pos;

// get the path to the target


}

void AI::MoveToRandom()
{
glm::vec3 r = GenerateRandomPosition(10);
if (std::abs(r.y - position.y) > 4)
return;

MoveTo(r);
}

void AI::Draw()
{
// move to target if we have one

float distance = glm::distance(position, target);


Entity::Draw(); // physics
}
23 changes: 23 additions & 0 deletions src/Game/Objects/Base/AI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _AI_H
#define _AI_H

#include "Entity.h"

class AI : public Entity
{
public:
AI(glm::vec3 pos) : Entity(pos) {}

std::vector<glm::vec3> path;

glm::vec3 target;
glm::vec3 GenerateRandomPosition(float distance);

void MoveTo(glm::vec3 pos);

void MoveToRandom();

void Draw() override;
};

#endif
5 changes: 4 additions & 1 deletion src/Game/Objects/Base/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,7 @@ void Chunk::DrawRegular()
glBindVertexArray(VAO); // regular faces
txp->Bind();
s->Bind();
s->SetUniform1f("lightLevel", 10.0f);

glm::mat4 model = glm::mat4(1.0f);

Expand Down Expand Up @@ -1146,6 +1147,7 @@ void Chunk::DrawTransparent()
glBindVertexArray(TRANSPARENTVAO); // transparent faces
txp->Bind();
s->Bind();
s->SetUniform1f("lightLevel", 10.0f);

glm::mat4 model = glm::mat4(1.0f);

Expand All @@ -1169,13 +1171,14 @@ void Chunk::DrawShadows()
return;

Shader* s = Game::instance->shader;

glEnable(GL_DEPTH_CLAMP);
glEnable(GL_CULL_FACE);

glBindVertexArray(SHADOWVAO); // shadow faces
txp->Bind();
s->Bind();
s->SetUniform1f("lightLevel", 10.0f);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, SHADOWEBO);

Expand Down
10 changes: 10 additions & 0 deletions src/Game/Objects/Base/DroppedItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ void DroppedItem::Draw()

Entity::Draw();

Game::instance->shader->Bind();

Game::instance->shader->SetUniform1f("lightLevel", (float)lightLevel);

sprite->Draw();

Game::instance->shader->Bind();

Game::instance->shader->SetUniform1f("lightLevel", 10.0f);

Game::instance->shader->Unbind();

}
8 changes: 8 additions & 0 deletions src/Game/Objects/Base/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../../Scenes/Gameplay.h"
#include "../../WorldManager.h"
#include "../../MusicManager.h"
#include "../../LightingManager.h"

Entity::Entity(glm::vec3 pos) : GameObject(pos)
{
Expand Down Expand Up @@ -366,6 +367,13 @@ void Entity::Draw()
else
inWater = topWater;

if (glfwGetTime() - lightUpdate > 0.1)
{
lightUpdate = glfwGetTime();

lightLevel = LightingManager::GetInstance()->GetLightLevel(position);
}

if (!Hud::GamePaused)
{

Expand Down
3 changes: 3 additions & 0 deletions src/Game/Objects/Base/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

class Entity : public GameObject
{
int lightUpdate = 0;
public:
Sprite3D* shadow;

glm::vec3 front = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);

int lightLevel = 10;

bool isCreature = true;

bool topWater = false;
Expand Down
51 changes: 51 additions & 0 deletions src/Game/Objects/Base/Zombie.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "Zombie.h"
#include "../../LightingManager.h"
#include <Game.h>

Zombie::Zombie(glm::vec3 pos) : AI(pos)
{
m = Model("Assets/Models/zombie_frim.obj");
m.LoadUVMap("zombie_frim");

m.scale = glm::vec3(0.7, 0.7, 0.7);


// raise arms

Mesh& leftArm = m.GetMesh("LeftArm");
Mesh& rightArm = m.GetMesh("RightArm");

leftArm.axis = glm::vec3(0, 0, 1);
rightArm.axis = glm::vec3(0, 0, 1);

leftArm.angle = 90;
rightArm.angle = 90;

// center them

leftArm.position += glm::vec3(0.5, 0.5, 0);
rightArm.position += glm::vec3(0.5, 0.5, 0);

}

void Zombie::Draw()
{
m.position = position - glm::vec3(0,0.9f,0);
m.angle = angle;

// bind shader

Game::instance->shader->Bind();

Game::instance->shader->SetUniform1f("lightLevel", (float)lightLevel);

m.Draw();

Game::instance->shader->Bind();

Game::instance->shader->SetUniform1f("lightLevel", 10.0f);

Game::instance->shader->Unbind();

AI::Draw(); // physics and AI pathing
}
17 changes: 17 additions & 0 deletions src/Game/Objects/Base/Zombie.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _ZOMBIE_H
#define _ZOMBIE_H

#include "AI.h"
#include <OpenGL/Model.h>
class Zombie : public AI
{
Model m;

public:

Zombie(glm::vec3 pos);

void Draw() override;
};

#endif
Loading

0 comments on commit a64d9fc

Please sign in to comment.