Lighting math formula updated. Moved rendering order so bullets are rendered last.
This commit is contained in:
parent
fd8cac38af
commit
65c5a742ac
Binary file not shown.
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 26 KiB |
@ -4,6 +4,7 @@
|
|||||||
#include <strstream>
|
#include <strstream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
using namespace olc;
|
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() {
|
void FaceBall::InitializeEnemyData() {
|
||||||
enemyData[EnemyID::NONE] = { "VOID",undefined,BLACK };
|
enemyData[EnemyID::NONE] = { "VOID",undefined,BLACK };
|
||||||
enemyData[EXIT] = { "EXIT",undefined,GREEN };
|
enemyData[EXIT] = { "EXIT",undefined,GREEN };
|
||||||
@ -33,6 +46,15 @@ void FaceBall::InitializeEnemyData() {
|
|||||||
enemyData[AREA_MAP] = { "Map",undefined,GREEN };
|
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)
|
void FaceBall::LoadLevel(int level)
|
||||||
{
|
{
|
||||||
this->level = 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 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 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);
|
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))));}
|
Pixel lightCol = b.col / 2 + Pixel{ 128, 128, 128 };
|
||||||
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 (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 (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))));}
|
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;
|
//triViewed.col = triTransformed.col;
|
||||||
|
|
||||||
@ -631,12 +654,12 @@ void FaceBall::RenderWorld() {
|
|||||||
for (auto& obj : objects) {
|
for (auto& obj : objects) {
|
||||||
RenderMesh(matView,vecTrianglesToRaster,obj);
|
RenderMesh(matView,vecTrianglesToRaster,obj);
|
||||||
}
|
}
|
||||||
for (auto& bullet : bullets) {
|
|
||||||
RenderMesh(matView, vecTrianglesToRaster, bullet);
|
|
||||||
}
|
|
||||||
for (auto& enemy : enemies) {
|
for (auto& enemy : enemies) {
|
||||||
RenderMesh(matView, vecTrianglesToRaster, enemy);
|
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;});
|
//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);
|
ClearBuffer(BLACK, true);
|
||||||
@ -726,7 +749,7 @@ void FaceBall::HandleKeys(float fElapsedTime) {
|
|||||||
else {
|
else {
|
||||||
pitch = 0;
|
pitch = 0;
|
||||||
if (GetMouse(0).bPressed) {
|
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) {
|
if (GetKey(W).bHeld) {
|
||||||
@ -828,14 +851,38 @@ bool FaceBall::OnUserCreate()
|
|||||||
mapFloor.texture = floor_tex;
|
mapFloor.texture = floor_tex;
|
||||||
|
|
||||||
InitializeEnemyData();
|
InitializeEnemyData();
|
||||||
|
InitializeBulletColors();
|
||||||
|
|
||||||
LoadLevel(1);
|
LoadLevel(1);
|
||||||
|
|
||||||
return true;
|
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) {
|
bool Bullet::Update(float fElapsedTime) {
|
||||||
vec3d adjustedSpd = { spd.x * fElapsedTime,0,spd.y * 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)) {
|
if (!FaceBall::CheckCollision(adjustedSpd, {pos.x,pos.z}, 0.05)) {
|
||||||
pos.x += adjustedSpd.x;
|
pos.x += adjustedSpd.x;
|
||||||
pos.z += adjustedSpd.z;
|
pos.z += adjustedSpd.z;
|
||||||
@ -846,6 +893,14 @@ bool Bullet::Update(float fElapsedTime) {
|
|||||||
return true;
|
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)
|
bool FaceBall::OnUserUpdate(float fElapsedTime)
|
||||||
{
|
{
|
||||||
for (std::vector<Bullet>::iterator it = bullets.begin(); it != bullets.end(); ++it) {
|
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) {
|
switch (mode) {
|
||||||
case GAME: {
|
case GAME: {
|
||||||
HandleKeys(fElapsedTime);
|
HandleKeys(fElapsedTime);
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include "pixelGameEngine.h"
|
#include "pixelGameEngine.h"
|
||||||
#include "Editor.h"
|
#include "Editor.h"
|
||||||
#include "geometry2d.h"
|
#include "geometry2d.h"
|
||||||
|
#include <optional>
|
||||||
using namespace olc;
|
using namespace olc;
|
||||||
|
|
||||||
const float PI = 3.14159f;
|
const float PI = 3.14159f;
|
||||||
@ -146,6 +147,8 @@ struct Object {
|
|||||||
|
|
||||||
struct Bullet : Object{
|
struct Bullet : Object{
|
||||||
vf2d spd = { 0,0 };
|
vf2d spd = { 0,0 };
|
||||||
|
Pixel col = GREEN;
|
||||||
|
bool friendly=true;
|
||||||
bool Update(float fElapsedTime);
|
bool Update(float fElapsedTime);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -174,6 +177,9 @@ struct Enemy : Object {
|
|||||||
float fireDelay=0;
|
float fireDelay=0;
|
||||||
public:
|
public:
|
||||||
Enemy(EnemyID id, vec3d pos, float rot, float radius);
|
Enemy(EnemyID id, vec3d pos, float rot, float radius);
|
||||||
|
EnemyID GetID();
|
||||||
|
void Hurt();
|
||||||
|
bool isDead();
|
||||||
};
|
};
|
||||||
|
|
||||||
class FaceBall : public PixelGameEngine
|
class FaceBall : public PixelGameEngine
|
||||||
@ -188,15 +194,15 @@ class FaceBall : public PixelGameEngine
|
|||||||
EnemyData GetData(EnemyID id);
|
EnemyData GetData(EnemyID id);
|
||||||
Decal* circle,*arrow,*YAZAWA;
|
Decal* circle,*arrow,*YAZAWA;
|
||||||
std::map<EnemyID, EnemyData>enemyData;
|
std::map<EnemyID, EnemyData>enemyData;
|
||||||
|
std::vector<Enemy>enemies;
|
||||||
private:
|
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;
|
Decal* dot, * enemy_ShootMe_tex,*bullet_tex,*wall_tex,*floor_tex;
|
||||||
vi2d MAP_SIZE;
|
vi2d MAP_SIZE;
|
||||||
std::vector<std::vector<MapSquare>>map;
|
std::vector<std::vector<MapSquare>>map;
|
||||||
std::vector<Object>objects;
|
std::vector<Object>objects;
|
||||||
std::vector<Bullet>bullets;
|
std::vector<Bullet>bullets;
|
||||||
std::vector<Enemy>enemies;
|
|
||||||
GAMEMODE mode=GAMEMODE::GAME;
|
GAMEMODE mode=GAMEMODE::GAME;
|
||||||
Editor editor;
|
Editor editor;
|
||||||
int level=1;
|
int level=1;
|
||||||
@ -246,8 +252,11 @@ class FaceBall : public PixelGameEngine
|
|||||||
bool OnUserUpdate(float fElapsedTime) override;
|
bool OnUserUpdate(float fElapsedTime) override;
|
||||||
void OnTextEntryComplete(const std::string& sText) override;
|
void OnTextEntryComplete(const std::string& sText) override;
|
||||||
void InitializeEnemyData();
|
void InitializeEnemyData();
|
||||||
|
void InitializeBulletColors();
|
||||||
void LoadLevel(int level);
|
void LoadLevel(int level);
|
||||||
void RenderMesh(mat4x4&matView, std::vector<Triangle>&vecTrianglesToRaster,Object&o);
|
void RenderMesh(mat4x4&matView, std::vector<Triangle>&vecTrianglesToRaster,Object&o);
|
||||||
|
void RunEnemyAI(Enemy& e,float fElapsedTime);
|
||||||
public:
|
public:
|
||||||
static bool CheckCollision(vec3d movementVector,vf2d pos,float radius);
|
static bool CheckCollision(vec3d movementVector,vf2d pos,float radius);
|
||||||
|
static int CheckEnemyCollision(vec3d movementVector, vf2d pos, float radius);
|
||||||
};
|
};
|
Loading…
x
Reference in New Issue
Block a user