Lighting math formula updated. Moved rendering order so bullets are rendered last.

linux_template
sigonasr2 2 years ago
parent fd8cac38af
commit 65c5a742ac
  1. BIN
      Faceball2030/assets/enemies/bullet.png
  2. 73
      Faceball2030/main.cpp
  3. 13
      Faceball2030/main.h

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

@ -4,6 +4,7 @@
#include <strstream>
#include <algorithm>
#include "main.h"
#include <optional>
using namespace olc;
@ -18,6 +19,18 @@ Enemy::Enemy(EnemyID id,vec3d pos,float rot,float radius)
}
}
EnemyID Enemy::GetID() {
return id;
}
void Enemy::Hurt() {
health--;
}
bool Enemy::isDead() {
return health <= 0;
}
void FaceBall::InitializeEnemyData() {
enemyData[EnemyID::NONE] = { "VOID",undefined,BLACK };
enemyData[EXIT] = { "EXIT",undefined,GREEN };
@ -33,6 +46,15 @@ void FaceBall::InitializeEnemyData() {
enemyData[AREA_MAP] = { "Map",undefined,GREEN };
}
void FaceBall::InitializeBulletColors() {
green_bullet = bullet;
for (Triangle& t : green_bullet.tris) {
t.col[0] = GREEN;
t.col[1] = GREEN;
t.col[2] = GREEN;
}
}
void FaceBall::LoadLevel(int level)
{
this->level = level;
@ -540,9 +562,10 @@ void FaceBall::RenderMesh(mat4x4&matView,std::vector<Triangle>&vecTrianglesToRas
float colorMult = (dist < 2 ? std::sinf(0.75 * dist + PI / 2) * 4 :1);
float colorMult2 = (dist2 < 2 ? std::sinf(0.75 * dist2 + PI / 2) * 4 : 1);
float colorMult3 = (dist3 < 2 ? std::sinf(0.75 * dist3 + PI / 2) * 4 : 1);
if (dist < 2) {triViewed.col[0] = Pixel(std::min(255, std::max((int)originalCol[0].r, (int)(originalCol[0].r * colorMult / 2))), std::min(255, std::max((int)originalCol[0].g, (int)(originalCol[0].g * colorMult))), std::min(255, std::max((int)originalCol[0].b, (int)(originalCol[0].b * colorMult / 2))));}
if (dist2 < 2) {triViewed.col[1] = Pixel(std::min(255, std::max((int)originalCol[1].r, (int)(originalCol[1].r * colorMult2 / 2))), std::min(255, std::max((int)originalCol[1].g, (int)(originalCol[1].g * colorMult2))), std::min(255, std::max((int)originalCol[1].b, (int)(originalCol[1].b * colorMult2 / 2))));}
if (dist3 < 2) {triViewed.col[2] = Pixel(std::min(255, std::max((int)originalCol[2].r, (int)(originalCol[2].r * colorMult3 / 2))), std::min(255, std::max((int)originalCol[2].g, (int)(originalCol[2].g * colorMult3))), std::min(255, std::max((int)originalCol[2].b, (int)(originalCol[2].b * colorMult3 / 2))));}
Pixel lightCol = b.col / 2 + Pixel{ 128, 128, 128 };
if (dist < 2) {triViewed.col[0] = Pixel(std::min(255, std::max((int)originalCol[0].r, (int)(originalCol[0].r * colorMult / float(255.f/lightCol.r)))), std::min(255, std::max((int)originalCol[0].g, (int)(originalCol[0].g * colorMult / float(255.f / lightCol.g)))), std::min(255, std::max((int)originalCol[0].b, (int)(originalCol[0].b * colorMult / float(255.f / lightCol.b)))));}
if (dist2 < 2) {triViewed.col[1] = Pixel(std::min(255, std::max((int)originalCol[1].r, (int)(originalCol[1].r * colorMult2 / float(255.f / lightCol.r)))), std::min(255, std::max((int)originalCol[1].g, (int)(originalCol[1].g * colorMult2 / float(255.f / lightCol.g)))), std::min(255, std::max((int)originalCol[1].b, (int)(originalCol[1].b * colorMult2 / float(255.f / lightCol.b)))));}
if (dist3 < 2) {triViewed.col[2] = Pixel(std::min(255, std::max((int)originalCol[2].r, (int)(originalCol[2].r * colorMult3 / float(255.f / lightCol.r)))), std::min(255, std::max((int)originalCol[2].g, (int)(originalCol[2].g * colorMult3 / float(255.f / lightCol.g)))), std::min(255, std::max((int)originalCol[2].b, (int)(originalCol[2].b * colorMult3 / float(255.f / lightCol.b)))));}
}
//triViewed.col = triTransformed.col;
@ -631,12 +654,12 @@ void FaceBall::RenderWorld() {
for (auto& obj : objects) {
RenderMesh(matView,vecTrianglesToRaster,obj);
}
for (auto& bullet : bullets) {
RenderMesh(matView, vecTrianglesToRaster, bullet);
}
for (auto& enemy : enemies) {
RenderMesh(matView, vecTrianglesToRaster, enemy);
}
for (auto& bullet : bullets) {
RenderMesh(matView, vecTrianglesToRaster, bullet);
}
//std::sort(vecTrianglesToRaster.begin(),vecTrianglesToRaster.end(),[](triangle&t1,triangle&t2){return (t1.p[0].z+t1.p[1].z+t1.p[2].z)/3.0f>(t2.p[0].z+t2.p[1].z+t2.p[2].z)/3.0f;});
ClearBuffer(BLACK, true);
@ -726,7 +749,7 @@ void FaceBall::HandleKeys(float fElapsedTime) {
else {
pitch = 0;
if (GetMouse(0).bPressed) {
bullets.push_back({ bullet,{player.GetPos().x,player.GetPos().y - 0.15f, player.GetPos().z},fYaw,0.125f,{shotSpd * std::cosf(fYaw),shotSpd * std::sinf(fYaw)} });
bullets.push_back({ green_bullet,{player.GetPos().x,player.GetPos().y - 0.15f, player.GetPos().z},fYaw,0.125f,{shotSpd * std::cosf(fYaw),shotSpd * std::sinf(fYaw)},GREEN,true });
}
}
if (GetKey(W).bHeld) {
@ -828,14 +851,38 @@ bool FaceBall::OnUserCreate()
mapFloor.texture = floor_tex;
InitializeEnemyData();
InitializeBulletColors();
LoadLevel(1);
return true;
}
int FaceBall::CheckEnemyCollision(vec3d movementVector, vf2d pos, float radius) {
vf2d newpos = { pos.x + movementVector.x,pos.y + movementVector.z };
for (int i = 0; i < game->enemies.size();i++) {
Enemy& e = game->enemies[i];
float dist = std::sqrtf(std::powf(newpos.x - e.pos.x, 2) + std::powf(newpos.y - e.pos.z, 2));
if (dist < radius + e.radius) {
return i;
}
}
return -1;
}
bool Bullet::Update(float fElapsedTime) {
vec3d adjustedSpd = { spd.x * fElapsedTime,0,spd.y * fElapsedTime };
if (friendly) {
int collided_enemy = FaceBall::CheckEnemyCollision(adjustedSpd, { pos.x,pos.z }, 0.1);
if (collided_enemy!=-1) {
Enemy& enemy = game->enemies[collided_enemy];
enemy.Hurt();
if (enemy.isDead()) {
game->enemies.erase(game->enemies.begin() + collided_enemy);
}
return false;
}
}
if (!FaceBall::CheckCollision(adjustedSpd, {pos.x,pos.z}, 0.05)) {
pos.x += adjustedSpd.x;
pos.z += adjustedSpd.z;
@ -846,6 +893,14 @@ bool Bullet::Update(float fElapsedTime) {
return true;
}
void FaceBall::RunEnemyAI(Enemy& e,float fElapsedTime) {
switch (e.GetID()) {
case SHOOTME: {
e.rot += 0.5 * fElapsedTime;
}break;
}
}
bool FaceBall::OnUserUpdate(float fElapsedTime)
{
for (std::vector<Bullet>::iterator it = bullets.begin(); it != bullets.end(); ++it) {
@ -857,6 +912,10 @@ bool FaceBall::OnUserUpdate(float fElapsedTime)
}
}
}
for (std::vector<Enemy>::iterator it = enemies.begin(); it != enemies.end(); ++it) {
Enemy& e = *it;
RunEnemyAI(e,fElapsedTime);
}
switch (mode) {
case GAME: {
HandleKeys(fElapsedTime);

@ -2,6 +2,7 @@
#include "pixelGameEngine.h"
#include "Editor.h"
#include "geometry2d.h"
#include <optional>
using namespace olc;
const float PI = 3.14159f;
@ -146,6 +147,8 @@ struct Object {
struct Bullet : Object{
vf2d spd = { 0,0 };
Pixel col = GREEN;
bool friendly=true;
bool Update(float fElapsedTime);
};
@ -174,6 +177,9 @@ struct Enemy : Object {
float fireDelay=0;
public:
Enemy(EnemyID id, vec3d pos, float rot, float radius);
EnemyID GetID();
void Hurt();
bool isDead();
};
class FaceBall : public PixelGameEngine
@ -188,15 +194,15 @@ class FaceBall : public PixelGameEngine
EnemyData GetData(EnemyID id);
Decal* circle,*arrow,*YAZAWA;
std::map<EnemyID, EnemyData>enemyData;
std::vector<Enemy>enemies;
private:
Mesh mapWalls,mapFloor,enemy_ShootMe,bullet,undefined;
Mesh mapWalls,mapFloor,enemy_ShootMe,bullet,green_bullet,undefined;
Decal* dot, * enemy_ShootMe_tex,*bullet_tex,*wall_tex,*floor_tex;
vi2d MAP_SIZE;
std::vector<std::vector<MapSquare>>map;
std::vector<Object>objects;
std::vector<Bullet>bullets;
std::vector<Enemy>enemies;
GAMEMODE mode=GAMEMODE::GAME;
Editor editor;
int level=1;
@ -246,8 +252,11 @@ class FaceBall : public PixelGameEngine
bool OnUserUpdate(float fElapsedTime) override;
void OnTextEntryComplete(const std::string& sText) override;
void InitializeEnemyData();
void InitializeBulletColors();
void LoadLevel(int level);
void RenderMesh(mat4x4&matView, std::vector<Triangle>&vecTrianglesToRaster,Object&o);
void RunEnemyAI(Enemy& e,float fElapsedTime);
public:
static bool CheckCollision(vec3d movementVector,vf2d pos,float radius);
static int CheckEnemyCollision(vec3d movementVector, vf2d pos, float radius);
};
Loading…
Cancel
Save