Implement movement and collision for the player.

linux_template
sigonasr2 2 years ago
parent a081174ad1
commit afeadd3e6c
  1. 164
      Faceball2030/Editor.cpp
  2. 10
      Faceball2030/Editor.h
  3. 3
      Faceball2030/Faceball2030.vcxproj
  4. 3
      Faceball2030/Faceball2030.vcxproj.filters
  5. BIN
      Faceball2030/assets/dot.png
  6. 40
      Faceball2030/assets/map/map1.map
  7. 1050
      Faceball2030/geometry2d.h
  8. 244
      Faceball2030/main.cpp
  9. 28
      Faceball2030/main.h

@ -1,16 +1,26 @@
#include "pixelGameEngine.h"
#include "main.h"
using namespace olc;
extern FaceBall* game;
Editor::Editor() {
map = LoadLevel(this->level);
MAP_SIZE = { (int)map[0].size(),(int)map.size() };
}
void Editor::Update(float fElapsedTime){
MouseWheelEnemySelection();
RenderLevel();
WaveLayerSelection();
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 });
if (!game->IsTextEntryEnabled()) {
if (game->GetKey(F3).bPressed) {
SaveLevel();
std::cout << "Level " << level << " Saved!" << std::endl;
}
}
LoadLevelHandling();
}
@ -88,37 +98,38 @@ void Editor::RenderLevel()
vf2d squarePos = vf2d{ (float)x,(float)y }*GRID_SIZE + center;
if (game->GetMouseX() >= squarePos.x && game->GetMouseX() <= squarePos.x + GRID_SIZE.x &&
game->GetMouseY() >= squarePos.y && game->GetMouseY() <= squarePos.y + GRID_SIZE.y) {
if (game->GetKey(W).bPressed) {
t.wallN = !t.wallN;
if (y > 0) {
Tile& neighbor = map[y - 1][x];
neighbor.wallS = t.wallN;
if (!game->IsTextEntryEnabled()) {
if (game->GetKey(W).bPressed) {
t.wallN = !t.wallN;
if (y > 0) {
Tile& neighbor = map[y - 1][x];
neighbor.wallS = t.wallN;
}
}
}
if (game->GetKey(D).bPressed) {
t.wallE = !t.wallE;
if (x < MAP_SIZE.x - 1) {
Tile& neighbor = map[y][x + 1];
neighbor.wallW = t.wallE;
if (game->GetKey(D).bPressed) {
t.wallE = !t.wallE;
if (x < MAP_SIZE.x - 1) {
Tile& neighbor = map[y][x + 1];
neighbor.wallW = t.wallE;
}
}
}
if (game->GetKey(S).bPressed) {
t.wallS = !t.wallS;
if (y < MAP_SIZE.y - 1) {
Tile& neighbor = map[y + 1][x];
neighbor.wallN = t.wallS;
if (game->GetKey(S).bPressed) {
t.wallS = !t.wallS;
if (y < MAP_SIZE.y - 1) {
Tile& neighbor = map[y + 1][x];
neighbor.wallN = t.wallS;
}
}
}
if (game->GetKey(A).bPressed) {
t.wallW = !t.wallW;
if (x > 0) {
Tile& neighbor = map[y][x - 1];
neighbor.wallE = t.wallW;
if (game->GetKey(A).bPressed) {
t.wallW = !t.wallW;
if (x > 0) {
Tile& neighbor = map[y][x - 1];
neighbor.wallE = t.wallW;
}
}
}
if (game->GetMouse(0).bPressed) {
t.enemyId = selectedEnemy;
switch (waveLayer) {
if (game->GetMouse(0).bPressed) {
t.enemyId = selectedEnemy;
switch (waveLayer) {
case 0: {
t.wave1 = true;
}break;
@ -128,13 +139,13 @@ void Editor::RenderLevel()
case 2: {
t.wave3 = true;
}break;
}
if (t.enemyId == EnemyID::NONE) {
t.wave1 = t.wave2 = t.wave3 = false;
}
}
if (t.enemyId == EnemyID::NONE) {
t.wave1 = t.wave2 = t.wave3 = false;
}
}
if (game->GetMouse(1).bPressed) {
switch (waveLayer) {
if (game->GetMouse(1).bPressed) {
switch (waveLayer) {
case 0: {
t.wave1 = false;
}break;
@ -144,22 +155,23 @@ void Editor::RenderLevel()
case 2: {
t.wave3 = false;
}break;
}
if (!(t.wave1 || t.wave2 || t.wave3)) {
t.enemyId = EnemyID::NONE;
}
}
if (!(t.wave1 || t.wave2 || t.wave3)) {
t.enemyId = EnemyID::NONE;
if (game->GetKey(DOWN).bHeld) {
t.facingDir = FacingDirection::SOUTH;
}
if (game->GetKey(RIGHT).bHeld) {
t.facingDir = FacingDirection::EAST;
}
if (game->GetKey(UP).bHeld) {
t.facingDir = FacingDirection::NORTH;
}
if (game->GetKey(LEFT).bHeld) {
t.facingDir = FacingDirection::WEST;
}
}
if (game->GetKey(DOWN).bHeld) {
t.facingDir = FacingDirection::SOUTH;
}
if (game->GetKey(RIGHT).bHeld) {
t.facingDir = FacingDirection::EAST;
}
if (game->GetKey(UP).bHeld) {
t.facingDir = FacingDirection::NORTH;
}
if (game->GetKey(LEFT).bHeld) {
t.facingDir = FacingDirection::WEST;
}
game->FillRectDecal(squarePos, GRID_SIZE, { 0,0,255,64 });
}
@ -201,15 +213,43 @@ void Editor::RenderLevel()
void Editor::WaveLayerSelection()
{
if (game->GetKey(K1).bPressed) {
waveLayer = 0;
}
if (game->GetKey(K2).bPressed) {
waveLayer = 1;
if (!game->IsTextEntryEnabled()) {
if (game->GetKey(K1).bPressed) {
waveLayer = 0;
}
if (game->GetKey(K2).bPressed) {
waveLayer = 1;
}
if (game->GetKey(K3).bPressed) {
waveLayer = 2;
}
}
if (game->GetKey(K3).bPressed) {
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) {
@ -222,7 +262,8 @@ void Editor::OnTextEntryComplete(const std::string& sText) {
game->TextEntryEnable(false);
promptState = PromptState::NONE;
this->level = input;
LoadLevel();
map = LoadLevel(this->level);
MAP_SIZE = { (int)map[0].size(),(int)map.size() };
}
else {
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");
if (file.good()) {
while (file.good()) {
@ -303,7 +345,7 @@ void Editor::LoadLevel() {
int width = std::atoi(line.c_str());
std::getline(file, line);
int height = std::atoi(line.c_str());
MAP_SIZE = { width,height };
vi2d MAP_SIZE = { width,height };
map.clear();
for (int y = 0; y < MAP_SIZE.y; y++) {
std::vector<Tile>row;
@ -325,11 +367,14 @@ void Editor::LoadLevel() {
}
map.push_back(row);
}
goto loopDone;
}
loopDone:
int a;
}
else {
std::cout << "Map Level " << level << " does not exist. Creating default map."<<std::endl;
MAP_SIZE = { 5,5 };
vi2d MAP_SIZE = { 5,5 };
map.clear();
for (int y = 0; y < MAP_SIZE.y; y++) {
std::vector<Tile>row;
@ -339,4 +384,5 @@ void Editor::LoadLevel() {
map.push_back(row);
}
}
return map;
}

@ -58,8 +58,8 @@ struct Tile {
};
class Editor {
int level;
vi2d MAP_SIZE;
int level=1;
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.
std::vector<std::vector<Tile>> map;
const vi2d GRID_SIZE = { 32,32 };
@ -67,12 +67,14 @@ class Editor {
PromptState promptState = PromptState::NONE;
int waveLayer = 0;
public:
Editor() {}
Editor();
void Update(float fElapsedTime);
std::vector<std::vector<Tile>> LoadLevel(int level);
void OnTextEntryComplete(const std::string& sText);
void LoadLevel();
private:
void LoadLevelHandling();
void MouseWheelEnemySelection();
void RenderLevel();
void WaveLayerSelection();
void SaveLevel();
};

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

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 565 B

@ -1,6 +1,34 @@
2
2
41126
41126
41126
41126
8
4
33195
32778
8200
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

File diff suppressed because it is too large Load Diff

@ -1,5 +1,6 @@
#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#include "geometry2d.h"
#include <strstream>
#include <algorithm>
#include "main.h"
@ -23,6 +24,113 @@ void FaceBall::InitializeEnemyData() {
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)
{
return enemyData[id];
@ -364,8 +472,8 @@ void FaceBall::RenderWorld() {
vec3d vTarget = { 0,sinf(pitch),cosf(pitch) };
mat4x4 matCameraRot = Matrix_MakeRotationY(fYaw);
vLookDir = Matrix_MultiplyVector(matCameraRot, vTarget);
vTarget = Vector_Add(vCamera, vLookDir);
mat4x4 matCamera = Matrix_PointAt(vCamera, vTarget, vUp);
vTarget = Vector_Add(player.GetPos(), vLookDir);
mat4x4 matCamera = Matrix_PointAt(player.GetPos(), vTarget, vUp);
mat4x4 matView = Matrix_QuickInverse(matCamera);
std::vector<Triangle>vecTrianglesToRaster;
@ -397,7 +505,7 @@ void FaceBall::RenderWorld() {
normal = Vector_CrossProduct(line1, line2);
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) {
vec3d light_dir = Vector_Mul(vLookDir, -1);
@ -545,23 +653,38 @@ void FaceBall::RenderWorld() {
}
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 (GetKey(olc::DOWN).bHeld) {
if (GetKey(DOWN).bHeld) {
pitch -= 1 * fElapsedTime;
}
if (GetKey(olc::UP).bHeld) {
if (GetKey(UP).bHeld) {
pitch += 1 * fElapsedTime;
}
}
else {
pitch = 0;
}
if (GetKey(olc::W).bHeld) {
vCamera = Vector_Add(vCamera, vForward);
if (GetKey(W).bHeld) {
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(olc::S).bHeld) {
vCamera = Vector_Sub(vCamera, vForward);
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::A).bHeld) {
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) {
map[gridSquare.y][gridSquare.x].wallN = dot;
if (gridSquare.y > 0) {
@ -615,99 +738,8 @@ bool FaceBall::OnUserCreate()
Mesh testEnemy("assets/enemies/ShootMe.obj", enemy_ShootMe_tex);
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++) {
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);
LoadLevel(1);
return true;
}
@ -716,9 +748,6 @@ bool FaceBall::OnUserUpdate(float fElapsedTime)
{
switch (mode) {
case GAME: {
for (Object& o : objects) {
o.rot += PI / 8 * fElapsedTime;
}
HandleKeys(fElapsedTime);
RenderWorld();
}break;
@ -728,6 +757,9 @@ bool FaceBall::OnUserUpdate(float fElapsedTime)
}
if (GetKey(olc::F5).bPressed) {
mode = (GAMEMODE)!(mode&1);
if (mode == GAMEMODE::GAME) {
LoadLevel(level);
}
}
return true;
}

@ -1,6 +1,7 @@
#pragma once
#include "pixelGameEngine.h"
#include "Editor.h"
#include "geometry2d.h"
using namespace olc;
const float PI = 3.14159f;
@ -31,6 +32,25 @@ struct vec3d
float y = 0;
float z = 0;
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
@ -160,10 +180,10 @@ class FaceBall : public PixelGameEngine
std::vector<Object>objects;
GAMEMODE mode=GAMEMODE::GAME;
Editor editor;
int level=1;
mat4x4 matProj;
vec3d vCamera = { 5,0.5,5 };
vec3d vLookDir;
float zOffset = 2;
@ -172,6 +192,8 @@ class FaceBall : public PixelGameEngine
float fYaw = 0;
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);
mat4x4 Matrix_MakeIdentity();
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);
void RenderWorld();
void HandleKeys(float fElapsedTime);
void AddWall(Direction dir, vi2d gridSquare);
void AddWall(int dir, vi2d gridSquare);
bool OnUserCreate() override;
bool OnUserUpdate(float fElapsedTime) override;
void OnTextEntryComplete(const std::string& sText) override;
void InitializeEnemyData();
void LoadLevel(int level);
bool CheckCollision(vec3d movementVector);
};
Loading…
Cancel
Save