From c0ab97585c5b5ed4c1607ea93faa9052fa1f39d3 Mon Sep 17 00:00:00 2001 From: Foxar Date: Sat, 5 Dec 2020 22:04:38 +0100 Subject: [PATCH] Added: collectibles class. Collectibles are strewn around the level and can be 'picked up' (deactivated, won't be rendered), when the player gets close and jumps. --- src/samples/dolphin/Makefile | 1 + src/samples/dolphin/dolphin.cpp | 41 +++++++++++++++++++++ src/samples/dolphin/dolphin.hpp | 3 +- src/samples/dolphin/objects/collectible.cpp | 39 ++++++++++++++++++++ src/samples/dolphin/objects/collectible.hpp | 33 +++++++++++++++++ src/samples/dolphin/objects/player.cpp | 5 +++ src/samples/dolphin/objects/player.hpp | 1 + 7 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/samples/dolphin/objects/collectible.cpp create mode 100644 src/samples/dolphin/objects/collectible.hpp diff --git a/src/samples/dolphin/Makefile b/src/samples/dolphin/Makefile index c4e5f83..5c92574 100755 --- a/src/samples/dolphin/Makefile +++ b/src/samples/dolphin/Makefile @@ -5,6 +5,7 @@ TYRA_DIR = ./../../engine EE_OBJS = \ camera.o \ objects/player.o \ + objects/collectible.o \ dolphin.o \ main.o diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 323d9f6..19f3df3 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -14,8 +14,11 @@ const u8 WATER_TILE_SCALE = 5.0F; const u8 WATER_SIZE = 100.0F; +const u8 OYSTERS_COUNT = 15.0F; + Dolphin::Dolphin(Engine *t_engine) : engine(t_engine), camera(&engine->screen) { + oysters = new Collectible[OYSTERS_COUNT]; } Dolphin::~Dolphin() {} @@ -58,6 +61,26 @@ void Dolphin::onInit() waterbox.rotation.x = Math::PI; waterbox.position.y = -100.F; + printf("Loading oyster dff\n"); + //oysters[0].loadDff("oyster/", "oyster", 1.F, false); + oysters[0].mesh.loadObj("oyster/", "oyster", 10.F, false); + printf("Loaded."); + oysters[0].mesh.shouldBeBackfaceCulled = false; + oysters[0].mesh.shouldBeFrustumCulled = false; + oysters[0].mesh.position.set(15, 0, 15); + texRepo->addByMesh("oyster/", oysters[0].mesh, BMP); + printf("Adding oyster texture.\n"); + for (int i = 1; i < OYSTERS_COUNT; i++) + { + printf("Processing oyster %d\n", i); + oysters[i].mesh.loadFrom(oysters[0].mesh); + oysters[i].mesh.shouldBeBackfaceCulled = false; + oysters[i].mesh.shouldBeFrustumCulled = false; + texRepo->getByMesh(oysters[0].mesh.getId(), oysters[0].mesh.getMaterial(0).getId()) + ->addLink(oysters[i].mesh.getId(), oysters[i].mesh.getMaterial(0).getId()); + oysters[i].mesh.position.set(i * -100, 5.0F, i * -100); + } + texRepo->addByMesh("dolphin/", player.mesh, BMP); texRepo->addByMesh("sunnyisl/", island, BMP); texRepo->addByMesh("skybox/", skybox, BMP); @@ -86,6 +109,17 @@ void Dolphin::onUpdate() water.position.z = (player.mesh.position.z / WATER_SIZE) * WATER_SIZE; } + for (int i = 0; i < OYSTERS_COUNT; i++) + { + Vector3 vecDist = oysters[i].mesh.position - player.mesh.position; + float dist = Math::sqrt(vecDist.x + vecDist.y + vecDist.z); + if (dist < 2 && player.getIsJumping() && oysters[i].isActive()) + { + printf("Pickup %d Dist %d\n", i, dist); + oysters[i].setActive(false); + } + } + if (player.mesh.position.y > WATER_LEVEL - 5) player.mesh.position.y = WATER_LEVEL - 5; @@ -97,6 +131,13 @@ void Dolphin::onUpdate() engine->renderer->draw(water); engine->renderer->draw(skybox); engine->renderer->draw(waterbox); + for (u8 i = 0; i < OYSTERS_COUNT; i++) + { + if (oysters[i].isActive()) + { + engine->renderer->draw(oysters[i].mesh); + } + } engine->renderer->draw(player.mesh); if (camera.position.y < WATER_LEVEL) engine->renderer->draw(waterOverlay); diff --git a/src/samples/dolphin/dolphin.hpp b/src/samples/dolphin/dolphin.hpp index 6c00556..9630f5e 100755 --- a/src/samples/dolphin/dolphin.hpp +++ b/src/samples/dolphin/dolphin.hpp @@ -11,7 +11,6 @@ #ifndef _DRIVER_ #define _DRIVER_ - #include #include #include @@ -21,6 +20,7 @@ #include #include "./camera.hpp" #include "./objects/player.hpp" +#include "./objects/collectible.hpp" class Dolphin : public Game { @@ -39,6 +39,7 @@ private: Player player; Camera camera; Mesh island, skybox, water, waterbox; + Collectible *oysters; TextureRepository *texRepo; Sprite waterOverlay; }; diff --git a/src/samples/dolphin/objects/collectible.cpp b/src/samples/dolphin/objects/collectible.cpp new file mode 100644 index 0000000..27ffb5c --- /dev/null +++ b/src/samples/dolphin/objects/collectible.cpp @@ -0,0 +1,39 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#include "collectible.hpp" + +#include +#include +#include +#include +#include + +Collectible::Collectible() +{ + mesh.loadObj("oyster/", "oyster", 10.F, false); + mesh.shouldBeFrustumCulled = false; + mesh.position.set(0.0F, 0.0F, 0.0f); + bActive = true; +} + +Collectible::~Collectible() +{ +} + +void Collectible::setActive(u8 b) +{ + bActive = b; +} + +u8 Collectible::isActive() +{ + return bActive; +} \ No newline at end of file diff --git a/src/samples/dolphin/objects/collectible.hpp b/src/samples/dolphin/objects/collectible.hpp new file mode 100644 index 0000000..a7adbd1 --- /dev/null +++ b/src/samples/dolphin/objects/collectible.hpp @@ -0,0 +1,33 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#ifndef _COLLECTIBLE_ +#define _COLLECTIBLE_ + +#include "../camera.hpp" +#include + +class Collectible +{ + +public: + float rotation; + Mesh mesh; + Collectible(); + ~Collectible(); + void update(); + void setActive(u8 b); + u8 isActive(); + +private: + u8 bActive; +}; + +#endif diff --git a/src/samples/dolphin/objects/player.cpp b/src/samples/dolphin/objects/player.cpp index 09e49b6..87c4953 100755 --- a/src/samples/dolphin/objects/player.cpp +++ b/src/samples/dolphin/objects/player.cpp @@ -86,4 +86,9 @@ void Player::update(Pad &t_pad) mesh.position.x += Math::sin(mesh.rotation.z) * velocity * (isJumping + 1); mesh.position.y += lift; +} + +u8 Player::getIsJumping() +{ + return isJumping; } \ No newline at end of file diff --git a/src/samples/dolphin/objects/player.hpp b/src/samples/dolphin/objects/player.hpp index bd990be..fecbd51 100755 --- a/src/samples/dolphin/objects/player.hpp +++ b/src/samples/dolphin/objects/player.hpp @@ -26,6 +26,7 @@ public: Player(); ~Player(); void update(Pad &t_pad); + u8 getIsJumping(); private: u8 isJumping;