Implement movement and collision for the player.

This commit is contained in:
sigonasr2 2023-04-10 12:01:18 -05:00
parent a081174ad1
commit afeadd3e6c
9 changed files with 1364 additions and 178 deletions

View File

@ -1,16 +1,26 @@
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
#include "main.h" #include "main.h"
using namespace olc; using namespace olc;
extern FaceBall* game; extern FaceBall* game;
Editor::Editor() {
map = LoadLevel(this->level);
MAP_SIZE = { (int)map[0].size(),(int)map.size() };
}
void Editor::Update(float fElapsedTime){ void Editor::Update(float fElapsedTime){
MouseWheelEnemySelection(); MouseWheelEnemySelection();
RenderLevel(); RenderLevel();
WaveLayerSelection(); WaveLayerSelection();
game->DrawStringDecal({ 2,16 }, "Selected Enemy ("+std::to_string(selectedEnemy) + "): " + game->GetData(selectedEnemy).name, WHITE, {3,3}); game->DrawStringDecal({ 2,16 }, "Selected Enemy ("+std::to_string(selectedEnemy) + "): " + game->GetData(selectedEnemy).name, WHITE, {3,3});
game->DrawStringDecal({ 2,(float)game->ScreenHeight() - 50 }, "Layer " + std::to_string(waveLayer), WHITE, { 5,5 }); game->DrawStringDecal({ 2,(float)game->ScreenHeight() - 50 }, "Layer " + std::to_string(waveLayer), WHITE, { 5,5 });
if (!game->IsTextEntryEnabled()) {
if (game->GetKey(F3).bPressed) {
SaveLevel();
std::cout << "Level " << level << " Saved!" << std::endl;
}
}
LoadLevelHandling(); LoadLevelHandling();
} }
@ -88,6 +98,7 @@ void Editor::RenderLevel()
vf2d squarePos = vf2d{ (float)x,(float)y }*GRID_SIZE + center; vf2d squarePos = vf2d{ (float)x,(float)y }*GRID_SIZE + center;
if (game->GetMouseX() >= squarePos.x && game->GetMouseX() <= squarePos.x + GRID_SIZE.x && if (game->GetMouseX() >= squarePos.x && game->GetMouseX() <= squarePos.x + GRID_SIZE.x &&
game->GetMouseY() >= squarePos.y && game->GetMouseY() <= squarePos.y + GRID_SIZE.y) { game->GetMouseY() >= squarePos.y && game->GetMouseY() <= squarePos.y + GRID_SIZE.y) {
if (!game->IsTextEntryEnabled()) {
if (game->GetKey(W).bPressed) { if (game->GetKey(W).bPressed) {
t.wallN = !t.wallN; t.wallN = !t.wallN;
if (y > 0) { if (y > 0) {
@ -161,6 +172,7 @@ void Editor::RenderLevel()
if (game->GetKey(LEFT).bHeld) { if (game->GetKey(LEFT).bHeld) {
t.facingDir = FacingDirection::WEST; t.facingDir = FacingDirection::WEST;
} }
}
game->FillRectDecal(squarePos, GRID_SIZE, { 0,0,255,64 }); game->FillRectDecal(squarePos, GRID_SIZE, { 0,0,255,64 });
} }
//game->DrawRectDecal(squarePos,GRID_SIZE,{64,64,64}); //game->DrawRectDecal(squarePos,GRID_SIZE,{64,64,64});
@ -201,6 +213,7 @@ void Editor::RenderLevel()
void Editor::WaveLayerSelection() void Editor::WaveLayerSelection()
{ {
if (!game->IsTextEntryEnabled()) {
if (game->GetKey(K1).bPressed) { if (game->GetKey(K1).bPressed) {
waveLayer = 0; waveLayer = 0;
} }
@ -210,6 +223,33 @@ void Editor::WaveLayerSelection()
if (game->GetKey(K3).bPressed) { if (game->GetKey(K3).bPressed) {
waveLayer = 2; waveLayer = 2;
} }
}
}
void Editor::SaveLevel()
{
std::ofstream file("assets/map/map" + std::to_string(level) + ".map");
file << MAP_SIZE.x << std::endl;
file << MAP_SIZE.y << std::endl;
for (int y = 0; y < MAP_SIZE.y; y++) {
for (int x = 0; x < MAP_SIZE.x; x++) {
int databits=0;
Tile& t = map[y][x];
databits =
t.blink << 15 |
int(t.facingDir) << 13 |
int(t.enemyId) << 7 |
t.wave3 << 6 |
t.wave2 << 5 |
t.wave1 << 4 |
t.wallN << 3 |
t.wallE << 2 |
t.wallS << 1 |
t.wallW;
file << databits << std::endl;
}
}
file.close();
} }
void Editor::OnTextEntryComplete(const std::string& sText) { void Editor::OnTextEntryComplete(const std::string& sText) {
@ -222,7 +262,8 @@ void Editor::OnTextEntryComplete(const std::string& sText) {
game->TextEntryEnable(false); game->TextEntryEnable(false);
promptState = PromptState::NONE; promptState = PromptState::NONE;
this->level = input; this->level = input;
LoadLevel(); map = LoadLevel(this->level);
MAP_SIZE = { (int)map[0].size(),(int)map.size() };
} }
else { else {
reEnableTextEntry = true; reEnableTextEntry = true;
@ -294,7 +335,8 @@ void Editor::OnTextEntryComplete(const std::string& sText) {
} }
void Editor::LoadLevel() { std::vector<std::vector<Tile>> Editor::LoadLevel(int level) {
std::vector<std::vector<Tile>> map;
std::ifstream file("assets/map/map"+std::to_string(level)+".map"); std::ifstream file("assets/map/map"+std::to_string(level)+".map");
if (file.good()) { if (file.good()) {
while (file.good()) { while (file.good()) {
@ -303,7 +345,7 @@ void Editor::LoadLevel() {
int width = std::atoi(line.c_str()); int width = std::atoi(line.c_str());
std::getline(file, line); std::getline(file, line);
int height = std::atoi(line.c_str()); int height = std::atoi(line.c_str());
MAP_SIZE = { width,height }; vi2d MAP_SIZE = { width,height };
map.clear(); map.clear();
for (int y = 0; y < MAP_SIZE.y; y++) { for (int y = 0; y < MAP_SIZE.y; y++) {
std::vector<Tile>row; std::vector<Tile>row;
@ -325,11 +367,14 @@ void Editor::LoadLevel() {
} }
map.push_back(row); map.push_back(row);
} }
goto loopDone;
} }
loopDone:
int a;
} }
else { else {
std::cout << "Map Level " << level << " does not exist. Creating default map."<<std::endl; std::cout << "Map Level " << level << " does not exist. Creating default map."<<std::endl;
MAP_SIZE = { 5,5 }; vi2d MAP_SIZE = { 5,5 };
map.clear(); map.clear();
for (int y = 0; y < MAP_SIZE.y; y++) { for (int y = 0; y < MAP_SIZE.y; y++) {
std::vector<Tile>row; std::vector<Tile>row;
@ -339,4 +384,5 @@ void Editor::LoadLevel() {
map.push_back(row); map.push_back(row);
} }
} }
return map;
} }

View File

@ -58,8 +58,8 @@ struct Tile {
}; };
class Editor { class Editor {
int level; int level=1;
vi2d MAP_SIZE; vi2d MAP_SIZE = { 1,1 };
bool reEnableTextEntry; //We must set this to true to cause Text Entry to reappear due to how the callback works. bool reEnableTextEntry; //We must set this to true to cause Text Entry to reappear due to how the callback works.
std::vector<std::vector<Tile>> map; std::vector<std::vector<Tile>> map;
const vi2d GRID_SIZE = { 32,32 }; const vi2d GRID_SIZE = { 32,32 };
@ -67,12 +67,14 @@ class Editor {
PromptState promptState = PromptState::NONE; PromptState promptState = PromptState::NONE;
int waveLayer = 0; int waveLayer = 0;
public: public:
Editor() {} Editor();
void Update(float fElapsedTime); void Update(float fElapsedTime);
std::vector<std::vector<Tile>> LoadLevel(int level);
void OnTextEntryComplete(const std::string& sText); void OnTextEntryComplete(const std::string& sText);
void LoadLevel(); private:
void LoadLevelHandling(); void LoadLevelHandling();
void MouseWheelEnemySelection(); void MouseWheelEnemySelection();
void RenderLevel(); void RenderLevel();
void WaveLayerSelection(); void WaveLayerSelection();
void SaveLevel();
}; };

View File

@ -104,7 +104,7 @@
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp14</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
@ -133,6 +133,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Editor.h" /> <ClInclude Include="Editor.h" />
<ClInclude Include="geometry2d.h" />
<ClInclude Include="main.h" /> <ClInclude Include="main.h" />
<ClInclude Include="pixelGameEngine.h" /> <ClInclude Include="pixelGameEngine.h" />
</ItemGroup> </ItemGroup>

View File

@ -32,5 +32,8 @@
<ClInclude Include="main.h"> <ClInclude Include="main.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="geometry2d.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 565 B

View File

@ -1,6 +1,34 @@
2 8
2 4
41126 33195
41126 32778
41126 8200
41126 8204
8203
8202
8200
8204
40969
32776
0
8194
8618
8202
8198
8197
8193
8192
8198
8617
8200
8202
8202
8198
8195
16662
8207
8195
8194
8202
8202
8206

1050
Faceball2030/geometry2d.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,6 @@
#define OLC_PGE_APPLICATION #define OLC_PGE_APPLICATION
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
#include "geometry2d.h"
#include <strstream> #include <strstream>
#include <algorithm> #include <algorithm>
#include "main.h" #include "main.h"
@ -23,6 +24,113 @@ void FaceBall::InitializeEnemyData() {
enemyData[AREA_MAP] = { "Map",GREEN }; enemyData[AREA_MAP] = { "Map",GREEN };
} }
void FaceBall::LoadLevel(int level)
{
this->level = level;
std::vector<std::vector<Tile>>mapData = editor.LoadLevel(level);
MAP_SIZE = { (int)mapData[0].size(),(int)mapData.size() };
map.clear();
mapMesh.tris.clear();
objects.clear();
for (int y = 0; y < MAP_SIZE.y; y++) {
std::vector<MapSquare>row;
for (int x = 0; x < MAP_SIZE.x; x++) {
row.push_back({});
mapMesh.tris.push_back({ {{(float)x,0,(float)y},{(float)x,0,(float)y + 1},{(float)x + 1,0,(float)y}},{{0,0},{1,0},{0,1}},BLUE });
mapMesh.tris.push_back({ {{(float)x + 1,0,(float)y},{(float)x,0,(float)y + 1},{(float)x + 1,0,(float)y + 1}},{{0,0},{1,0},{0,1}},BLUE });
}
map.push_back(row);
}
for (int y = 0; y < MAP_SIZE.y; y++) {
for (int x = 0; x < MAP_SIZE.x; x++) {
int wallData = 0;
if (mapData[y][x].wallN) {
wallData |= NORTH;
}
if (mapData[y][x].wallE) {
wallData |= EAST;
}
if (mapData[y][x].wallS) {
wallData |= SOUTH;
}
if (mapData[y][x].wallW) {
wallData |= WEST;
}
AddWall(wallData, { x,y });
if (map[y][x].wallN != NULL) {
mapMesh.tris.push_back({ {{(float)x,1,(float)y},{(float)x,0,(float)y},{(float)x + 1,1,(float)y}},{{0,0},{1,0},{0,1}},YELLOW });
mapMesh.tris.push_back({ {{(float)x,0,(float)y},{(float)x + 1,0,(float)y},{(float)x + 1,1,(float)y}},{{0,0},{1,0},{0,1}},YELLOW });
}
if (map[y][x].wallS != NULL) {
mapMesh.tris.push_back({ {{(float)x + 1,1,(float)y + 1},{(float)x,0,(float)y + 1},{(float)x,1,(float)y + 1}},{{0,0},{1,0},{0,1}},DARK_RED });
mapMesh.tris.push_back({ {{(float)x + 1,1,(float)y + 1},{(float)x + 1,0,(float)y + 1},{(float)x,0,(float)y + 1}},{{0,0},{1,0},{0,1}},DARK_RED });
}
if (map[y][x].wallW != NULL) {
mapMesh.tris.push_back({ {{(float)x,1,(float)y},{(float)x,1,(float)y + 1}, {(float)x,0,(float)y + 1}},{{0,0},{1,0},{0,1}},MAGENTA });
mapMesh.tris.push_back({ {{(float)x,0,(float)y},{(float)x,1,(float)y}, {(float)x,0,(float)y + 1}}, {{0,0},{1,0},{0,1}},MAGENTA });
}
if (map[y][x].wallE != NULL) {
mapMesh.tris.push_back({ {{(float)x + 1,0,(float)y + 1},{(float)x + 1,1,(float)y + 1},{(float)x + 1,1,(float)y}},{{0,0},{1,0},{0,1}},CYAN });
mapMesh.tris.push_back({ {{(float)x + 1,0,(float)y + 1} ,{(float)x + 1,1,(float)y},{(float)x + 1,0,(float)y}},{{0,0},{1,0},{0,1}},CYAN });
}
}
}
objects.push_back({ mapMesh });
}
bool FaceBall::CheckCollision(vec3d movementVector){
utils::geom2d::circle<float>newPos{ {player.GetPos().x + movementVector.x,player.GetPos().z + movementVector.z},player.GetRadius() };
bool collisionOccured = false;
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
int offsetX = (int)player.GetPos().x + x;
int offsetY = (int)player.GetPos().z + y;
if (offsetX >= 0 && offsetX < MAP_SIZE.x && offsetY >= 0 && offsetY < MAP_SIZE.y) {
MapSquare tile = map[offsetY][offsetX];
if (tile.wallN != NULL) {
utils::geom2d::line<float>wall{ {(float)offsetX,(float)offsetY},{(float)offsetX+1,(float)offsetY} };
if (((int)(newPos.pos.x-player.GetRadius())==offsetX||
(int)(newPos.pos.x + player.GetRadius()) == offsetX)
&&
newPos.pos.y > wall.start.y && wall.start.y > newPos.pos.y - player.GetRadius()) {
collisionOccured = true;
goto breakOutOfCollisionLoop;
}
}
if (tile.wallE != NULL) {
utils::geom2d::line<float>wall{ {(float)offsetX+1,(float)offsetY},{(float)offsetX + 1,(float)offsetY+1} };
if (((int)(newPos.pos.y - player.GetRadius()) == offsetY ||
(int)(newPos.pos.y + player.GetRadius()) == offsetY)
&& newPos.pos.x<wall.start.x && wall.start.x < newPos.pos.x + player.GetRadius()) {
collisionOccured = true;
goto breakOutOfCollisionLoop;
}
}
if (tile.wallS != NULL) {
utils::geom2d::line<float>wall{ {(float)offsetX,(float)offsetY+1},{(float)offsetX + 1,(float)offsetY+1} };
if (((int)(newPos.pos.x - player.GetRadius()) == offsetX ||
(int)(newPos.pos.x + player.GetRadius()) == offsetX)
&& newPos.pos.y < wall.start.y && wall.start.y < newPos.pos.y + player.GetRadius()) {
collisionOccured = true;
goto breakOutOfCollisionLoop;
}
}
if (tile.wallW != NULL) {
utils::geom2d::line<float>wall{ {(float)offsetX,(float)offsetY},{(float)offsetX,(float)offsetY + 1} };
if (((int)(newPos.pos.y - player.GetRadius()) == offsetY ||
(int)(newPos.pos.y + player.GetRadius()) == offsetY)
&& newPos.pos.x > wall.start.x && wall.start.x > newPos.pos.x - player.GetRadius()) {
collisionOccured = true;
goto breakOutOfCollisionLoop;
}
}
}
}
}
breakOutOfCollisionLoop:
return collisionOccured;
}
EnemyData FaceBall::GetData(EnemyID id) EnemyData FaceBall::GetData(EnemyID id)
{ {
return enemyData[id]; return enemyData[id];
@ -364,8 +472,8 @@ void FaceBall::RenderWorld() {
vec3d vTarget = { 0,sinf(pitch),cosf(pitch) }; vec3d vTarget = { 0,sinf(pitch),cosf(pitch) };
mat4x4 matCameraRot = Matrix_MakeRotationY(fYaw); mat4x4 matCameraRot = Matrix_MakeRotationY(fYaw);
vLookDir = Matrix_MultiplyVector(matCameraRot, vTarget); vLookDir = Matrix_MultiplyVector(matCameraRot, vTarget);
vTarget = Vector_Add(vCamera, vLookDir); vTarget = Vector_Add(player.GetPos(), vLookDir);
mat4x4 matCamera = Matrix_PointAt(vCamera, vTarget, vUp); mat4x4 matCamera = Matrix_PointAt(player.GetPos(), vTarget, vUp);
mat4x4 matView = Matrix_QuickInverse(matCamera); mat4x4 matView = Matrix_QuickInverse(matCamera);
std::vector<Triangle>vecTrianglesToRaster; std::vector<Triangle>vecTrianglesToRaster;
@ -397,7 +505,7 @@ void FaceBall::RenderWorld() {
normal = Vector_CrossProduct(line1, line2); normal = Vector_CrossProduct(line1, line2);
normal = Vector_Normalise(normal); normal = Vector_Normalise(normal);
vec3d vCameraRay = Vector_Sub(triTransformed.p[0], vCamera); vec3d vCameraRay = Vector_Sub(triTransformed.p[0], player.GetPos());
if (Vector_DotProduct(normal, vCameraRay) < 0) { if (Vector_DotProduct(normal, vCameraRay) < 0) {
vec3d light_dir = Vector_Mul(vLookDir, -1); vec3d light_dir = Vector_Mul(vLookDir, -1);
@ -545,23 +653,38 @@ void FaceBall::RenderWorld() {
} }
void FaceBall::HandleKeys(float fElapsedTime) { void FaceBall::HandleKeys(float fElapsedTime) {
vec3d vForward = Vector_Mul(vLookDir, 2 * fElapsedTime); vec3d vForward = Vector_Mul(vLookDir, std::min(player.GetRadius()-0.00001f,2*fElapsedTime));
if (freeRoam) { if (freeRoam) {
if (GetKey(olc::DOWN).bHeld) { if (GetKey(DOWN).bHeld) {
pitch -= 1 * fElapsedTime; pitch -= 1 * fElapsedTime;
} }
if (GetKey(olc::UP).bHeld) { if (GetKey(UP).bHeld) {
pitch += 1 * fElapsedTime; pitch += 1 * fElapsedTime;
} }
} }
else { else {
pitch = 0; pitch = 0;
} }
if (GetKey(olc::W).bHeld) { if (GetKey(W).bHeld) {
vCamera = Vector_Add(vCamera, vForward); if (!CheckCollision({ vForward.x,0,0 })) {
vec3d xMovement{ vForward.x,0,0 };
player.UpdatePos(Vector_Add(player.GetPos(), xMovement));
}
if (!CheckCollision({0,0,vForward.z})) {
vec3d zMovement{ 0,0,vForward.z };
player.UpdatePos(Vector_Add(player.GetPos(), zMovement));
}
}
if (GetKey(S).bHeld) {
vec3d vReverse = { -vForward.x,-vForward.y,-vForward.z };
if (!CheckCollision({ vReverse.x,0,0 })) {
vec3d xMovement{ vReverse.x,0,0 };
player.UpdatePos(Vector_Add(player.GetPos(), xMovement));
}
if (!CheckCollision({ 0,0,vReverse.z })) {
vec3d zMovement{ 0,0,vReverse.z };
player.UpdatePos(Vector_Add(player.GetPos(), zMovement));
} }
if (GetKey(olc::S).bHeld) {
vCamera = Vector_Sub(vCamera, vForward);
} }
if (GetKey(olc::A).bHeld) { if (GetKey(olc::A).bHeld) {
fYaw -= 2 * fElapsedTime; fYaw -= 2 * fElapsedTime;
@ -574,7 +697,7 @@ void FaceBall::HandleKeys(float fElapsedTime) {
} }
} }
void FaceBall::AddWall(Direction dir, vi2d gridSquare) { void FaceBall::AddWall(int dir, vi2d gridSquare) {
if (dir & NORTH) { if (dir & NORTH) {
map[gridSquare.y][gridSquare.x].wallN = dot; map[gridSquare.y][gridSquare.x].wallN = dot;
if (gridSquare.y > 0) { if (gridSquare.y > 0) {
@ -615,99 +738,8 @@ bool FaceBall::OnUserCreate()
Mesh testEnemy("assets/enemies/ShootMe.obj", enemy_ShootMe_tex); Mesh testEnemy("assets/enemies/ShootMe.obj", enemy_ShootMe_tex);
mapMesh.texture = dot; mapMesh.texture = dot;
for (int i = 0; i < 75; i++) {
Object newEnemy({ testEnemy,{ float(rand() % 20),float(rand() % 20) }, (rand() % 1000) / 1000.f * 2 * PI });
objects.push_back(newEnemy);
}
for (int y = 0; y < MAP_SIZE.y; y++) { LoadLevel(1);
std::vector<MapSquare>row;
for (int x = 0; x < MAP_SIZE.x; x++) {
row.push_back({});
mapMesh.tris.push_back({ {{(float)x,0,(float)y},{(float)x,0,(float)y + 1},{(float)x + 1,0,(float)y}},{{0,0},{1,0},{0,1}},BLUE });
mapMesh.tris.push_back({ {{(float)x + 1,0,(float)y},{(float)x,0,(float)y + 1},{(float)x + 1,0,(float)y + 1}},{{0,0},{1,0},{0,1}},BLUE });
}
map.push_back(row);
}
for (int y = 0; y < MAP_SIZE.y; y++) {
for (int x = 0; x < MAP_SIZE.x; x++) {
switch (rand() % 16) {
case 0: {//No Wall.
}break;
case 1: {//North Wall.
AddWall(NORTH, { x,y });
}break;
case 2: {//East Wall.
AddWall(EAST, { x,y });
}break;
case 3: {//South Wall.
AddWall(SOUTH, { x,y });
}break;
case 4: {//West Wall.
AddWall(WEST, { x,y });
}break;
case 5: {//NE Wall.
AddWall(Direction(NORTH | EAST), { x,y });
}break;
case 6: {
AddWall(Direction(NORTH | WEST), { x,y });
}break;
case 7: {
AddWall(Direction(SOUTH | EAST), { x,y });
}break;
case 8: {
AddWall(Direction(SOUTH | WEST), { x,y });
}break;
case 9: {
AddWall(Direction(NORTH | SOUTH), { x,y });
}break;
case 10: {
AddWall(Direction(EAST | WEST), { x,y });
}break;
case 11: {
AddWall(Direction(NORTH | WEST | EAST), { x,y });
}break;
case 12: {
AddWall(Direction(NORTH | WEST | SOUTH), { x,y });
}break;
case 13: {
AddWall(Direction(WEST | SOUTH | EAST), { x,y });
}break;
case 14: {
AddWall(Direction(SOUTH | EAST | NORTH), { x,y });
}break;
case 15: {
AddWall(Direction(SOUTH | EAST | NORTH | WEST), { x,y });
}break;
}
}
}
AddWall(Direction(SOUTH | EAST | NORTH | WEST), { 0,0 });
for (int y = 0; y < MAP_SIZE.y; y++) {
for (int x = 0; x < MAP_SIZE.x; x++) {
if (map[y][x].wallN != NULL) {
mapMesh.tris.push_back({ {{(float)x,1,(float)y},{(float)x,0,(float)y},{(float)x + 1,1,(float)y}},{{0,0},{1,0},{0,1}},YELLOW });
mapMesh.tris.push_back({ {{(float)x,0,(float)y},{(float)x + 1,0,(float)y},{(float)x + 1,1,(float)y}},{{0,0},{1,0},{0,1}},YELLOW });
}
if (map[y][x].wallS != NULL) {
mapMesh.tris.push_back({ {{(float)x + 1,1,(float)y + 1},{(float)x,0,(float)y + 1},{(float)x,1,(float)y + 1}},{{0,0},{1,0},{0,1}},DARK_RED });
mapMesh.tris.push_back({ {{(float)x + 1,1,(float)y + 1},{(float)x + 1,0,(float)y + 1},{(float)x,0,(float)y + 1}},{{0,0},{1,0},{0,1}},DARK_RED });
}
if (map[y][x].wallW != NULL) {
mapMesh.tris.push_back({ {{(float)x,1,(float)y},{(float)x,1,(float)y + 1}, {(float)x,0,(float)y + 1}},{{0,0},{1,0},{0,1}},MAGENTA });
mapMesh.tris.push_back({ {{(float)x,0,(float)y},{(float)x,1,(float)y}, {(float)x,0,(float)y + 1}}, {{0,0},{1,0},{0,1}},MAGENTA });
}
if (map[y][x].wallE != NULL) {
mapMesh.tris.push_back({ {{(float)x + 1,0,(float)y + 1},{(float)x + 1,1,(float)y + 1},{(float)x + 1,1,(float)y}},{{0,0},{1,0},{0,1}},CYAN });
mapMesh.tris.push_back({ {{(float)x + 1,0,(float)y + 1} ,{(float)x + 1,1,(float)y},{(float)x + 1,0,(float)y}},{{0,0},{1,0},{0,1}},CYAN });
}
}
}
//objects.push_back(mapMesh);
return true; return true;
} }
@ -716,9 +748,6 @@ bool FaceBall::OnUserUpdate(float fElapsedTime)
{ {
switch (mode) { switch (mode) {
case GAME: { case GAME: {
for (Object& o : objects) {
o.rot += PI / 8 * fElapsedTime;
}
HandleKeys(fElapsedTime); HandleKeys(fElapsedTime);
RenderWorld(); RenderWorld();
}break; }break;
@ -728,6 +757,9 @@ bool FaceBall::OnUserUpdate(float fElapsedTime)
} }
if (GetKey(olc::F5).bPressed) { if (GetKey(olc::F5).bPressed) {
mode = (GAMEMODE)!(mode&1); mode = (GAMEMODE)!(mode&1);
if (mode == GAMEMODE::GAME) {
LoadLevel(level);
}
} }
return true; return true;
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
#include "Editor.h" #include "Editor.h"
#include "geometry2d.h"
using namespace olc; using namespace olc;
const float PI = 3.14159f; const float PI = 3.14159f;
@ -31,6 +32,25 @@ struct vec3d
float y = 0; float y = 0;
float z = 0; float z = 0;
float w = 1; float w = 1;
friend std::ostream& operator << (std::ostream& os, const vec3d& rhs) { os << rhs.x << "," << rhs.y << "," << rhs.z; return os; }
};
struct Player {
private:
vec3d pos;
olc::utils::geom2d::circle<float> playerR;
public:
Player(vec3d pos,olc::utils::geom2d::circle<float>r):pos(pos), playerR(r) {}
void UpdatePos(vec3d pos) {
this->pos = pos;
playerR.pos = { pos.x,pos.z };
}
float GetRadius() {
return playerR.radius;
}
vec3d&GetPos() {
return pos;
}
}; };
struct Triangle struct Triangle
@ -160,10 +180,10 @@ class FaceBall : public PixelGameEngine
std::vector<Object>objects; std::vector<Object>objects;
GAMEMODE mode=GAMEMODE::GAME; GAMEMODE mode=GAMEMODE::GAME;
Editor editor; Editor editor;
int level=1;
mat4x4 matProj; mat4x4 matProj;
vec3d vCamera = { 5,0.5,5 };
vec3d vLookDir; vec3d vLookDir;
float zOffset = 2; float zOffset = 2;
@ -172,6 +192,8 @@ class FaceBall : public PixelGameEngine
float fYaw = 0; float fYaw = 0;
float pitch = -PI / 6; float pitch = -PI / 6;
Player player = { {3.7,0.3,0.7}, {{0.5,0.5},0.2} };
vec3d Matrix_MultiplyVector(mat4x4& m, vec3d& i); vec3d Matrix_MultiplyVector(mat4x4& m, vec3d& i);
mat4x4 Matrix_MakeIdentity(); mat4x4 Matrix_MakeIdentity();
mat4x4 Matrix_MakeRotationX(float fAngleRad); mat4x4 Matrix_MakeRotationX(float fAngleRad);
@ -194,9 +216,11 @@ class FaceBall : public PixelGameEngine
int Triangle_ClipAgainstPlane(vec3d plane_p, vec3d plane_n, Triangle& in_tri, Triangle& out_tri1, Triangle& out_tri2); int Triangle_ClipAgainstPlane(vec3d plane_p, vec3d plane_n, Triangle& in_tri, Triangle& out_tri1, Triangle& out_tri2);
void RenderWorld(); void RenderWorld();
void HandleKeys(float fElapsedTime); void HandleKeys(float fElapsedTime);
void AddWall(Direction dir, vi2d gridSquare); void AddWall(int dir, vi2d gridSquare);
bool OnUserCreate() override; bool OnUserCreate() override;
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 LoadLevel(int level);
bool CheckCollision(vec3d movementVector);
}; };