cleanup & changed water logic

This commit is contained in:
h4570
2020-12-27 19:49:36 +01:00
parent 55f476e9ed
commit ea0d47e09c
10 changed files with 91 additions and 84 deletions
+2 -18
View File
@@ -12,7 +12,7 @@
#include <utils/math.hpp>
const float CAMERA_Y = 15.0F;
const float CAMERA_Y = 25.0F;
Camera::Camera(ScreenSettings *t_screen) : CameraBase(t_screen, &position)
{
@@ -40,12 +40,7 @@ void Camera::rotate(Pad &t_pad)
horizontalLevel -= 0.08;
else if (t_pad.rJoyH >= 200)
horizontalLevel += 0.08;
/*
if (t_pad.rJoyV <= 50 && verticalLevel > 25.0F)
verticalLevel -= 0.9F;
else if (t_pad.rJoyV >= 200 && verticalLevel < 80.0F)
verticalLevel += 0.9F;
*/
unitCirclePosition.x = (Math::sin(horizontalLevel) * verticalLevel);
unitCirclePosition.y = CAMERA_Y;
unitCirclePosition.z = (Math::cos(horizontalLevel) * verticalLevel);
@@ -56,15 +51,4 @@ void Camera::followBy(Mesh &t_mesh)
position.x = unitCirclePosition.x + t_mesh.position.x;
position.y = unitCirclePosition.y + t_mesh.position.y;
position.z = unitCirclePosition.z + t_mesh.position.z;
if (position.x > (-0.04F * verticalLevel) &&
position.x < 0.0F &&
position.z > (0.998F * verticalLevel))
{
// TODO
// position.x = 0.0F;
// position.y = 0.0F;
// position.z = 0.0F;
// horizontalLevel = 0.0F;
}
}
+76 -35
View File
@@ -11,14 +11,12 @@
#include "dolphin.hpp"
#include "utils/math.hpp"
const u8 WATER_TILE_SCALE = 5;
const u8 WATER_SIZE = 100;
float Dolphin::engineFPS = 60.0F;
const float WATER_TILE_SCALE = 6.0F;
const u8 WATER_SIZE = 4;
const u8 MINES_COUNT = 15;
const u8 OYSTERS_COUNT = 5;
float Dolphin::engineFPS = 60.F;
Dolphin::Dolphin(Engine *t_engine) : engine(t_engine), camera(&engine->screen)
{
oysters = new Collectible[OYSTERS_COUNT];
@@ -29,6 +27,7 @@ Dolphin::~Dolphin()
{
delete[] oysters;
delete[] mines;
delete[] waterDrawList;
}
void Dolphin::onInit()
@@ -73,11 +72,26 @@ void Dolphin::onInit()
printf("Loaded.\n");
printf("Loading water...\n");
water.loadObj("water/", "water", WATER_TILE_SCALE, false);
water.position.set(0, WATER_LEVEL, 0);
texRepo->addByMesh("water/", water, BMP);
water.shouldBeFrustumCulled = false;
water.shouldBeBackfaceCulled = false;
u32 spiralOffset = (u32)Math::sqrt(WATER_TILES_COUNT);
waterDrawList = new Mesh *[WATER_TILES_COUNT];
calcSpiral(spiralOffset, spiralOffset);
water[0].loadObj("water/", "water", WATER_TILE_SCALE, false);
water[0].position.set(0.0F, WATER_LEVEL, 0.0F);
texRepo->addByMesh("water/", water[0], BMP);
water[0].shouldBeFrustumCulled = true;
water[0].shouldBeBackfaceCulled = false;
waterDrawList[0] = &water[0];
for (size_t i = 1; i < WATER_TILES_COUNT; i++)
{
water[i].loadFrom(water[0]);
water[i].shouldBeFrustumCulled = true;
water[i].shouldBeBackfaceCulled = false;
water[i].position.set(WATER_TILE_SCALE * 2.0F * spirals[i].x, WATER_LEVEL, WATER_TILE_SCALE * 2.0F * spirals[i].y);
texRepo->getByMesh(water[0].getId(), water[0].getMaterial(0).getId())
->addLink(water[i].getId(), water[i].getMaterial(0).getId());
waterDrawList[i] = &water[i];
}
printf("Loading seabed...\n");
seabed.loadObj("seabed/", "seabed", 10.0F, false);
@@ -96,7 +110,6 @@ void Dolphin::onInit()
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;
@@ -117,7 +130,6 @@ void Dolphin::onInit()
printf("Loading mine .obj\n");
mines[0].mesh.loadObj("mine/", "mine", 10.F, false);
printf("Loaded.");
mines[0].mesh.shouldBeBackfaceCulled = false;
mines[0].mesh.shouldBeFrustumCulled = false;
mines[0].mesh.position.set(5, -15, 32);
@@ -135,7 +147,8 @@ void Dolphin::onInit()
}
Texture *pLifeTex = texRepo->add("2d/", "life", PNG);
//pLifeTex->addLink(lifeSprites[0].getId());
// pLifeTex->addLink(lifeSprites[0].getId());
for (int i = 0; i < 3; i++)
{
lifeSprites[i].size.set(64.0F, 64.0F);
@@ -164,12 +177,13 @@ void Dolphin::onUpdate()
engine->renderer->draw(gameOver);
return;
}
Dolphin::engineFPS = engine->fps;
if (engine->pad.isCrossClicked)
printf("Delta multiplier: %f\n", 60.0F / engine->fps);
float xDist = player.mesh.position.x - water.position.x;
float zDist = player.mesh.position.z - water.position.z;
Dolphin::engineFPS = engine->fps;
// if (engine->pad.isCrossClicked)
// printf("Delta multiplier: %f\n", 60.0F / engine->fps);
float xDist = player.mesh.position.x - water[0].position.x;
float zDist = player.mesh.position.z - water[0].position.z;
if (xDist < 0)
xDist *= -1;
if (zDist < 0)
@@ -177,8 +191,11 @@ void Dolphin::onUpdate()
if (xDist > WATER_SIZE * WATER_TILE_SCALE / 4 ||
zDist > WATER_SIZE * WATER_TILE_SCALE / 4)
{
water.position.x = (player.mesh.position.x / WATER_SIZE) * WATER_SIZE;
water.position.z = (player.mesh.position.z / WATER_SIZE) * WATER_SIZE;
for (size_t i = 0; i < WATER_TILES_COUNT; i++)
{
water[i].position.x = player.mesh.position.x + (WATER_TILE_SCALE * 2.0F * spirals[i].x);
water[i].position.z = player.mesh.position.z + (WATER_TILE_SCALE * 2.0F * spirals[i].y);
}
}
for (int i = 0; i < OYSTERS_COUNT; i++)
@@ -188,7 +205,7 @@ void Dolphin::onUpdate()
float dist = vecDist.length();
if (dist < 20.F && player.isJumping() && oysters[i].isActive())
{
printf("Pickup %d Dist %d\n", i, dist);
printf("Pickup:%d Dist:%f\n", i, dist);
oysters[i].disappear();
player.setLife(player.getLifes() + 1);
engine->audio.setADPCMVolume(30, 3);
@@ -204,11 +221,11 @@ void Dolphin::onUpdate()
if (dist < 20.F && !mines[i].getExplosionTicks())
{
printf("Vecdist %f,%f,%f\n", vecDist.x, vecDist.y, vecDist.z);
printf("Explode %d Dist %d\n", i, dist);
printf("Vecdist: %f,%f,%f\n", vecDist.x, vecDist.y, vecDist.z);
printf("Explode:%d Dist:%f\n", i, dist);
mines[i].explode();
player.setLife(player.getLifes() - 1);
//Buffer switching to prevent playing two sounds on one buffer
// Buffer switching to prevent playing two sounds on one buffer
engine->audio.setADPCMVolume(65, Mine::lastSoundBuffer);
engine->audio.playADPCM(boomSound, Mine::lastSoundBuffer);
Mine::lastSoundBuffer = Mine::lastSoundBuffer == 4 ? 5 : 4;
@@ -220,35 +237,34 @@ void Dolphin::onUpdate()
player.update(engine->pad);
camera.update(engine->pad, player.mesh);
skybox.position.set(player.mesh.position.x, +200.0F, player.mesh.position.z);
waterbox.position.set(player.mesh.position.x, -260.0F, player.mesh.position.z);
engine->renderer->draw(island);
engine->renderer->draw(water);
engine->renderer->draw(waterDrawList, WATER_TILES_COUNT);
engine->renderer->draw(skybox);
engine->renderer->draw(waterbox);
engine->renderer->draw(seabed);
for (u8 i = 0; i < OYSTERS_COUNT; i++)
{
if (oysters[i].isActive())
{
engine->renderer->draw(oysters[i].mesh);
}
}
for (u8 i = 0; i < MINES_COUNT; i++)
{
if (mines[i].getExplosionTicks() < MINE_EXPLOSION_TICKS)
engine->renderer->draw(mines[i].mesh);
}
for (u8 i = 0; i < player.getLifes(); i++)
{
engine->renderer->draw(lifeSprites[i]);
}
engine->renderer->draw(player.mesh);
if (camera.position.y < WATER_LEVEL)
{
engine->renderer->draw(waterOverlay);
engine->audio.setADPCMVolume(0, 0); //Surface ambient
engine->audio.setADPCMVolume(65, 1); //Underwater ambient
engine->audio.setADPCMVolume(0, 0); // Surface ambient
engine->audio.setADPCMVolume(65, 1); // Underwater ambient
}
else
{
@@ -256,3 +272,28 @@ void Dolphin::onUpdate()
engine->audio.setADPCMVolume(0, 1); //Underwater ambient
}
}
void Dolphin::calcSpiral(int X, int Y)
{
int x, y, dx;
x = y = dx = 0;
int dy = -1;
int t = X > Y ? X : Y;
int maxI = t * t;
for (int i = 0; i < maxI; i++)
{
if ((-X / 2 <= x) && (x <= X / 2) && (-Y / 2 <= y) && (y <= Y / 2))
{
spirals[i].x = x;
spirals[i].y = y;
}
if ((x == y) || ((x < 0) && (x == -y)) || ((x > 0) && (x == 1 - y)))
{
t = dx;
dx = -dy;
dy = t;
}
x += dx;
y += dy;
}
}
+10 -3
View File
@@ -37,8 +37,15 @@ public:
static float engineFPS;
private:
/*DO NOT TOUCH ORDER OF DECLARATION!
GCC BUG CAUSES RENDERING TO FAIL IN ANY OTHER ORDER OF DECLARATION!*/
void calcSpiral(int X, int Y);
static const u16 WATER_TILES_COUNT = 1024;
Point spirals[WATER_TILES_COUNT];
Mesh **waterDrawList;
/**
* DO NOT TOUCH ORDER OF DECLARATION!
* GCC BUG CAUSES RENDERING TO FAIL IN ANY OTHER ORDER OF DECLARATION!
*/
Mesh seabed;
Mesh island;
Mesh skybox;
@@ -49,7 +56,7 @@ private:
Collectible *oysters;
TextureRepository *texRepo;
Sprite waterOverlay;
Mesh water;
Mesh water[WATER_TILES_COUNT];
Mesh waterbox;
Sprite lifeSprites[3];
Sprite gameOver;
@@ -25,10 +25,6 @@ Collectible::Collectible()
bDisappearing = false;
}
Collectible::~Collectible()
{
}
void Collectible::disappear()
{
bDisappearing = true;
@@ -21,7 +21,6 @@ public:
float rotation;
Mesh mesh;
Collectible();
~Collectible();
void update();
void disappear();
void setActive(const u8 &b) { bActive = b; }
-4
View File
@@ -20,10 +20,6 @@ Mine::Mine()
explosionTicks = 0;
}
Mine::~Mine()
{
}
void Mine::update()
{
if (bExploding && explosionTicks < MINE_EXPLOSION_TICKS)
-1
View File
@@ -24,7 +24,6 @@ public:
void explode() { bExploding = true; }
void update();
Mine();
~Mine();
const u8 &getExplosionTicks() { return explosionTicks; }
private:
+2 -16
View File
@@ -28,10 +28,6 @@ Player::Player()
lifes = 3;
}
Player::~Player()
{
}
void Player::init(Engine *t_engine)
{
pEngine = t_engine;
@@ -63,8 +59,7 @@ void Player::update(Pad &t_pad)
if (t_pad.isCrossClicked && mesh.getCurrentAnimationFrame() <= 14 && WATER_LEVEL - 15 < mesh.position.y)
{
printf("Cross\n");
mesh.setAnimSpeed(0.8F);
mesh.setAnimSpeed(1.0F);
mesh.playAnimation(14, 58, 0);
}
@@ -72,19 +67,10 @@ void Player::update(Pad &t_pad)
bIsJumping = true;
else
bIsJumping = false;
/*
if (lift < 0)
mesh.rotation.y = 1;
else if (lift > 0)
mesh.rotation.y = -1;
else
mesh.rotation.y = 0;
*/
if (velocity > 0 && mesh.getCurrentAnimationFrame() == 0)
{
mesh.setAnimSpeed(0.05F);
printf("Swim animation\n");
mesh.playAnimation(1, 14, 0);
}
if (mesh.getCurrentAnimationFrame() == 42)
@@ -101,7 +87,7 @@ void Player::update(Pad &t_pad)
bIsImpactingWater = false;
}
velocity *= 60.0F / Dolphin::engineFPS; //Moving by deltatime of last frame.
velocity *= 60.0F / Dolphin::engineFPS; // Moving by deltatime of last frame.
mesh.position.z +=
Math::cos(mesh.rotation.z) * velocity * (bIsJumping + 1);
mesh.position.x +=
-1
View File
@@ -25,7 +25,6 @@ public:
float gravity, velocity, lift;
Mesh mesh;
Player();
~Player();
void init(Engine *t_engine);
void update(Pad &t_pad);
const u8 &isJumping() { return bIsJumping; }