demo map and weapon
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
# ______ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licenced under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#include "states/game/debug_object.hpp"
|
||||
#include <loaders/3d/tyrobj/tyrobj_loader.hpp>
|
||||
#include <file/file_utils.hpp>
|
||||
|
||||
using Tyra::Color;
|
||||
using Tyra::FileUtils;
|
||||
using Tyra::TyrobjLoader;
|
||||
|
||||
namespace Demo {
|
||||
|
||||
DebugObject::DebugObject(TextureRepository* repo) {
|
||||
TyrobjLoader loader;
|
||||
auto* data =
|
||||
loader.load(FileUtils::fromCwd("game/models/debug.obj"), 1, .5F, true);
|
||||
data->normalsEnabled = false;
|
||||
data->textureCoordsEnabled = false;
|
||||
mesh = new StaticMesh(*data);
|
||||
delete data;
|
||||
mesh->getMaterial(0)->color = Color(192.0F, 32.0F, 32.0F, 128.0F);
|
||||
rotator = 0.0F;
|
||||
allocateOptions();
|
||||
|
||||
pair = new RendererStaticPair{mesh, options};
|
||||
}
|
||||
|
||||
void DebugObject::setPosition(const Vec4& v) {
|
||||
mesh->translation.identity();
|
||||
mesh->rotation.identity();
|
||||
mesh->translation.translate(v);
|
||||
mesh->rotation.rotateX(rotator);
|
||||
mesh->rotation.rotateY(rotator);
|
||||
mesh->rotation.rotateZ(rotator);
|
||||
|
||||
rotator += 0.05F;
|
||||
if (rotator >= 360.0F) rotator = 0.0F;
|
||||
}
|
||||
|
||||
DebugObject::~DebugObject() {
|
||||
delete mesh;
|
||||
delete options;
|
||||
delete pair;
|
||||
}
|
||||
|
||||
void DebugObject::allocateOptions() {
|
||||
options = new StaPipOptions();
|
||||
options->shadingType = Tyra::TyraShadingGouraud;
|
||||
options->blendingEnabled = false;
|
||||
options->antiAliasingEnabled = false;
|
||||
}
|
||||
|
||||
} // namespace Demo
|
||||
@@ -9,19 +9,20 @@
|
||||
*/
|
||||
|
||||
#include "states/game/game_state.hpp"
|
||||
#include "file/file_utils.hpp"
|
||||
#include "thread/threading.hpp"
|
||||
#include "debug/debug.hpp"
|
||||
|
||||
using Tyra::FileUtils;
|
||||
using Tyra::Threading;
|
||||
|
||||
namespace Demo {
|
||||
|
||||
GameState::GameState(Engine* t_engine) : State(t_engine) {
|
||||
GameState::GameState(Engine* t_engine)
|
||||
: State(t_engine),
|
||||
renderer(&t_engine->renderer),
|
||||
player(t_engine),
|
||||
terrain(&t_engine->renderer.core.texture.repository),
|
||||
dbgObj(&t_engine->renderer.core.texture.repository) {
|
||||
state = STATE_GAME;
|
||||
_wantFinish = false;
|
||||
initialized = false;
|
||||
fpsChecker = 0;
|
||||
}
|
||||
|
||||
GameState::~GameState() {}
|
||||
@@ -42,8 +43,26 @@ GlobalStateType GameState::onFinish() {
|
||||
}
|
||||
|
||||
void GameState::update() {
|
||||
engine->renderer.beginFrame();
|
||||
Threading::switchThread();
|
||||
if (fpsChecker++ > 100) {
|
||||
TYRA_LOG("FPS: ", engine->info.getFps());
|
||||
fpsChecker = 0;
|
||||
}
|
||||
|
||||
auto cameraInfo = player.getCameraInfo();
|
||||
engine->renderer.beginFrame(cameraInfo);
|
||||
|
||||
player.update();
|
||||
dbgObj.setPosition(*cameraInfo.looksAt);
|
||||
|
||||
renderer.clear();
|
||||
{
|
||||
renderer.add(player.staticPairs);
|
||||
renderer.add(player.dynamicPairs);
|
||||
renderer.add(terrain.pair);
|
||||
renderer.add(dbgObj.pair);
|
||||
}
|
||||
renderer.render();
|
||||
|
||||
engine->renderer.endFrame();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
# ______ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licenced under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#include "states/game/player/camera.hpp"
|
||||
#include "debug/debug.hpp"
|
||||
|
||||
namespace Demo {
|
||||
|
||||
Camera::Camera(Pad* t_pad) : lookAt(0.0F), position(0.0F), pad(t_pad) {
|
||||
lengthFromOrigin = 30.0F;
|
||||
circleRotation = -3.5F;
|
||||
height = 1.5F;
|
||||
}
|
||||
|
||||
Camera::~Camera() {}
|
||||
|
||||
void Camera::update(const Vec4& playerPosition) {
|
||||
const float rotationOffset = 0.03F;
|
||||
const float heightOffset = 0.5F;
|
||||
|
||||
const auto& rightJoy = pad->getRightJoyPad();
|
||||
|
||||
if (rightJoy.h <= 100) {
|
||||
circleRotation -= rotationOffset;
|
||||
} else if (rightJoy.h >= 200) {
|
||||
circleRotation += rotationOffset;
|
||||
}
|
||||
|
||||
if (rightJoy.v <= 100) {
|
||||
height -= heightOffset;
|
||||
} else if (rightJoy.v >= 200) {
|
||||
height += heightOffset;
|
||||
}
|
||||
|
||||
unitCircle.x = (sin(circleRotation) * lengthFromOrigin);
|
||||
unitCircle.y = height + playerPosition.y;
|
||||
unitCircle.z = (cos(circleRotation) * lengthFromOrigin);
|
||||
|
||||
lookAt = unitCircle + playerPosition;
|
||||
}
|
||||
|
||||
} // namespace Demo
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
# ______ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licenced under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#include "states/game/player/player.hpp"
|
||||
|
||||
namespace Demo {
|
||||
|
||||
Player::Player(Engine* engine)
|
||||
: position(0.0F),
|
||||
weapon(&engine->renderer.core.texture.repository),
|
||||
camera(&engine->pad) {
|
||||
staticPairs.push_back(new RendererStaticPair{weapon.mesh, weapon.options});
|
||||
pad = &engine->pad;
|
||||
position = Vec4(3.0F, 3.0F, 0.0F);
|
||||
camera.lookAt = Vec4(0.0F, 0.0F, -30.0F);
|
||||
speed = 0.5F;
|
||||
}
|
||||
|
||||
Player::~Player() {
|
||||
for (auto pair : staticPairs) {
|
||||
delete pair;
|
||||
}
|
||||
for (auto pair : dynamicPairs) {
|
||||
delete pair;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::update() {
|
||||
handlePlayerPosition();
|
||||
camera.update(position);
|
||||
weapon.update();
|
||||
}
|
||||
|
||||
void Player::handlePlayerPosition() {
|
||||
const auto& leftJoy = pad->getLeftJoyPad();
|
||||
|
||||
auto normalizedCamera = Vec4(camera.unitCircle);
|
||||
normalizedCamera.normalize();
|
||||
normalizedCamera *= speed;
|
||||
|
||||
Vec4 nextPosition(position);
|
||||
|
||||
if (leftJoy.v <= 100) {
|
||||
nextPosition.x += normalizedCamera.x;
|
||||
nextPosition.z += normalizedCamera.z;
|
||||
} else if (leftJoy.v >= 200) {
|
||||
nextPosition.x += -normalizedCamera.x;
|
||||
nextPosition.z += -normalizedCamera.z;
|
||||
}
|
||||
if (leftJoy.h <= 100) {
|
||||
nextPosition.x += normalizedCamera.z;
|
||||
nextPosition.z += -normalizedCamera.x;
|
||||
} else if (leftJoy.h >= 200) {
|
||||
nextPosition.x += -normalizedCamera.z;
|
||||
nextPosition.z += normalizedCamera.x;
|
||||
}
|
||||
|
||||
position = nextPosition;
|
||||
|
||||
camera.position = position;
|
||||
}
|
||||
|
||||
} // namespace Demo
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
# ______ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licenced under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#include "states/game/player/weapon.hpp"
|
||||
#include <loaders/3d/tyrobj/tyrobj_loader.hpp>
|
||||
#include <file/file_utils.hpp>
|
||||
|
||||
using Tyra::FileUtils;
|
||||
using Tyra::TyrobjLoader;
|
||||
|
||||
namespace Demo {
|
||||
|
||||
Weapon::Weapon(TextureRepository* repo) {
|
||||
TyrobjLoader loader;
|
||||
auto* data = loader.load(FileUtils::fromCwd("game/models/ak47/ak47.obj"), 1,
|
||||
1.0F, true);
|
||||
data->normalsEnabled = false;
|
||||
mesh = new StaticMesh(*data);
|
||||
delete data;
|
||||
|
||||
mesh->translation.translateZ(-50.0F);
|
||||
|
||||
repo->addByMesh(mesh, FileUtils::fromCwd("game/models/ak47/"), "png");
|
||||
|
||||
allocateOptions();
|
||||
}
|
||||
|
||||
// float testRotation = 0.0F;
|
||||
|
||||
void Weapon::update() {
|
||||
// testRotation += 0.001F;
|
||||
mesh->rotation.identity();
|
||||
mesh->rotation.rotateZ(0.1F);
|
||||
mesh->rotation.rotateY(-4.3F);
|
||||
// TYRA_LOG(testRotation);
|
||||
}
|
||||
|
||||
Weapon::~Weapon() {
|
||||
delete mesh;
|
||||
delete options;
|
||||
}
|
||||
|
||||
void Weapon::allocateOptions() {
|
||||
options = new StaPipOptions();
|
||||
options->shadingType = Tyra::TyraShadingGouraud;
|
||||
options->blendingEnabled = false;
|
||||
options->antiAliasingEnabled = false;
|
||||
}
|
||||
|
||||
} // namespace Demo
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
# ______ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licenced under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#include "states/game/renderer/game_renderer.hpp"
|
||||
#include "thread/threading.hpp"
|
||||
|
||||
using Tyra::Threading;
|
||||
|
||||
namespace Demo {
|
||||
|
||||
GameRenderer::GameRenderer(Renderer* t_renderer) {
|
||||
renderer = t_renderer;
|
||||
|
||||
stpip.setRenderer(&renderer->core);
|
||||
dypip.setRenderer(&renderer->core);
|
||||
}
|
||||
|
||||
GameRenderer::~GameRenderer() {}
|
||||
|
||||
void GameRenderer::add(std::vector<RendererStaticPair*> t_staticPairs) {
|
||||
staticPairs.insert(staticPairs.end(), t_staticPairs.begin(),
|
||||
t_staticPairs.end());
|
||||
}
|
||||
|
||||
void GameRenderer::add(std::vector<RendererDynamicPair*> t_dynamicPairs) {
|
||||
dynamicPairs.insert(dynamicPairs.end(), t_dynamicPairs.begin(),
|
||||
t_dynamicPairs.end());
|
||||
}
|
||||
|
||||
void GameRenderer::add(RendererStaticPair* staticPair) {
|
||||
staticPairs.push_back(staticPair);
|
||||
}
|
||||
|
||||
void GameRenderer::add(RendererDynamicPair* dynamicPair) {
|
||||
dynamicPairs.push_back(dynamicPair);
|
||||
}
|
||||
|
||||
void GameRenderer::clear() {
|
||||
staticPairs.clear();
|
||||
dynamicPairs.clear();
|
||||
}
|
||||
|
||||
void GameRenderer::render() {
|
||||
if (staticPairs.size()) {
|
||||
renderer->renderer3D.usePipeline(&stpip);
|
||||
|
||||
for (auto& pair : staticPairs) {
|
||||
stpip.render(pair->mesh, pair->options);
|
||||
}
|
||||
}
|
||||
|
||||
Threading::switchThread();
|
||||
|
||||
if (dynamicPairs.size()) {
|
||||
renderer->renderer3D.usePipeline(&dypip);
|
||||
|
||||
for (auto& pair : dynamicPairs) {
|
||||
dypip.render(pair->mesh, pair->options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Demo
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
# ______ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licenced under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#include "states/game/terrain/terrain.hpp"
|
||||
#include <loaders/3d/tyrobj/tyrobj_loader.hpp>
|
||||
#include <file/file_utils.hpp>
|
||||
|
||||
using Tyra::FileUtils;
|
||||
using Tyra::TyrobjLoader;
|
||||
|
||||
namespace Demo {
|
||||
|
||||
Terrain::Terrain(TextureRepository* repo) {
|
||||
TyrobjLoader loader;
|
||||
auto* data = loader.load(
|
||||
FileUtils::fromCwd("game/models/terrain/terrain.obj"), 1, 5.0F, true);
|
||||
data->normalsEnabled = false;
|
||||
mesh = new StaticMesh(*data);
|
||||
mesh->getMaterial(0)->color.r = 96.0F;
|
||||
mesh->getMaterial(0)->color.g = 96.0F;
|
||||
mesh->getMaterial(0)->color.b = 96.0F;
|
||||
delete data;
|
||||
|
||||
repo->addByMesh(mesh, FileUtils::fromCwd("game/models/terrain/"), "png");
|
||||
|
||||
allocateOptions();
|
||||
|
||||
pair = new RendererStaticPair{mesh, options};
|
||||
}
|
||||
|
||||
// float testRotation = 0.0F;
|
||||
|
||||
void Terrain::update() {
|
||||
// testRotation += 0.001F;
|
||||
mesh->rotation.identity();
|
||||
mesh->rotation.rotateZ(0.1F);
|
||||
mesh->rotation.rotateY(-4.3F);
|
||||
// TYRA_LOG(testRotation);
|
||||
}
|
||||
|
||||
Terrain::~Terrain() {
|
||||
delete mesh;
|
||||
delete options;
|
||||
delete pair;
|
||||
}
|
||||
|
||||
void Terrain::allocateOptions() {
|
||||
options = new StaPipOptions();
|
||||
options->shadingType = Tyra::TyraShadingGouraud;
|
||||
options->blendingEnabled = true;
|
||||
options->antiAliasingEnabled = false;
|
||||
}
|
||||
|
||||
} // namespace Demo
|
||||
Reference in New Issue
Block a user