Bullet spawning and rendering.

linux_template
sigonasr2 2 years ago
parent 3e52016067
commit 973eef9302
  1. 11
      Faceball2030/assets/enemies/bullet.mtl
  2. BIN
      Faceball2030/assets/enemies/bullet.png
  3. BIN
      Faceball2030/assets/enemies/bullet.wings
  4. 97
      Faceball2030/main.cpp
  5. 21
      Faceball2030/main.h

@ -0,0 +1,11 @@
# Exported from Wings 3D 2.2.9
newmtl sphere1_auv
Ns 19.999999999999996
d 1.0
illum 2
Kd 1.0 1.0 1.0
Ka 0.0 0.0 0.0
Ks 0.19 0.19 0.19
Ke 0.0 0.0 0.0
map_Kd sphere1_auv.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

@ -75,7 +75,7 @@ void FaceBall::LoadLevel(int level)
} }
} }
} }
objects.push_back({ mapMesh }); objects.push_back({ mapMesh,{0,0},0,0 });
} }
bool FaceBall::CheckCollision(vec3d movementVector,vf2d pos,float radius){ bool FaceBall::CheckCollision(vec3d movementVector,vf2d pos,float radius){
@ -85,8 +85,8 @@ bool FaceBall::CheckCollision(vec3d movementVector,vf2d pos,float radius){
for (int x = -1; x <= 1; x++) { for (int x = -1; x <= 1; x++) {
int offsetX = (int)pos.x + x; int offsetX = (int)pos.x + x;
int offsetY = (int)pos.y + y; int offsetY = (int)pos.y + y;
if (offsetX >= 0 && offsetX < MAP_SIZE.x && offsetY >= 0 && offsetY < MAP_SIZE.y) { if (offsetX >= 0 && offsetX < game->MAP_SIZE.x && offsetY >= 0 && offsetY < game->MAP_SIZE.y) {
MapSquare tile = map[offsetY][offsetX]; MapSquare tile = game->map[offsetY][offsetX];
if (tile.wallN != NULL) { if (tile.wallN != NULL) {
utils::geom2d::line<float>wall{ {(float)offsetX,(float)offsetY},{(float)offsetX+1,(float)offsetY} }; utils::geom2d::line<float>wall{ {(float)offsetX,(float)offsetY},{(float)offsetX+1,(float)offsetY} };
if (((int)(newPos.pos.x-radius)==offsetX|| if (((int)(newPos.pos.x-radius)==offsetX||
@ -456,38 +456,13 @@ int FaceBall::Triangle_ClipAgainstPlane(vec3d plane_p, vec3d plane_n, Triangle&
} }
} }
void FaceBall::RenderWorld() { void FaceBall::RenderMesh(mat4x4&matView,std::vector<Triangle>&vecTrianglesToRaster, Object&o) {
// Set up rotation matrices for (auto& tri : o.mesh.tris) {
mat4x4 matRotZ, matRotX, matWorld;
matRotZ = Matrix_MakeRotationZ(fTheta * 0.5f);
matRotX = Matrix_MakeRotationX(fTheta);
//matTrans = Matrix_MakeTranslation(0.0f, 0.0f, 0.0f);
matWorld = Matrix_MakeIdentity();
matWorld = Matrix_MultiplyMatrix(matRotZ, matRotX);
//matWorld = Matrix_MultiplyMatrix(matWorld, matTrans);
vec3d vUp = { 0,1,0 };
vec3d vTarget = { 0,sinf(pitch),cosf(pitch) };
mat4x4 matCameraRot = Matrix_MakeRotationY(fYaw);
vLookDir = Matrix_MultiplyVector(matCameraRot, vTarget);
vTarget = Vector_Add(freeRoam?freeRoamCamera:player.GetPos(), vLookDir);
mat4x4 matCamera = Matrix_PointAt(freeRoam?freeRoamCamera:player.GetPos(), vTarget, vUp);
mat4x4 matView = Matrix_QuickInverse(matCamera);
std::vector<Triangle>vecTrianglesToRaster;
// Draw Triangles
for (auto& obj : objects) {
for (auto& tri : obj.mesh.tris) {
Triangle triProjected, triTransformed, triViewed; Triangle triProjected, triTransformed, triViewed;
//mat4x4 matTrans = Matrix_MakeTranslation(-mesh.size.x/2, 0, -mesh.size.x/2);
mat4x4 localMat = Matrix_MakeIdentity(); mat4x4 localMat = Matrix_MakeIdentity();
//localMat = Matrix_MultiplyMatrix(localMat, matTrans); mat4x4 rotMat = Matrix_MakeRotationY(o.rot);
mat4x4 rotMat = Matrix_MakeRotationY(obj.rot);
localMat = Matrix_MultiplyMatrix(localMat, rotMat); localMat = Matrix_MultiplyMatrix(localMat, rotMat);
mat4x4 matTrans = Matrix_MakeTranslation(obj.pos.x, 0, obj.pos.y); mat4x4 matTrans = Matrix_MakeTranslation(o.pos.x-o.radius, o.pos.y-o.radius, o.pos.z - o.radius);
localMat = Matrix_MultiplyMatrix(localMat, matTrans); localMat = Matrix_MultiplyMatrix(localMat, matTrans);
triTransformed.p[0] = Matrix_MultiplyVector(localMat, tri.p[0]); triTransformed.p[0] = Matrix_MultiplyVector(localMat, tri.p[0]);
@ -573,7 +548,7 @@ void FaceBall::RenderWorld() {
triProjected.p[1].y *= 0.5f * (float)ScreenHeight(); triProjected.p[1].y *= 0.5f * (float)ScreenHeight();
triProjected.p[2].x *= 0.5f * (float)ScreenWidth(); triProjected.p[2].x *= 0.5f * (float)ScreenWidth();
triProjected.p[2].y *= 0.5f * (float)ScreenHeight(); triProjected.p[2].y *= 0.5f * (float)ScreenHeight();
triProjected.tex = obj.mesh.texture; triProjected.tex = o.mesh.texture;
vecTrianglesToRaster.push_back(triProjected); vecTrianglesToRaster.push_back(triProjected);
} }
@ -581,6 +556,37 @@ void FaceBall::RenderWorld() {
} }
} }
void FaceBall::RenderWorld() {
// Set up rotation matrices
mat4x4 matRotZ, matRotX, matWorld;
matRotZ = Matrix_MakeRotationZ(fTheta * 0.5f);
matRotX = Matrix_MakeRotationX(fTheta);
//matTrans = Matrix_MakeTranslation(0.0f, 0.0f, 0.0f);
matWorld = Matrix_MakeIdentity();
matWorld = Matrix_MultiplyMatrix(matRotZ, matRotX);
//matWorld = Matrix_MultiplyMatrix(matWorld, matTrans);
vec3d vUp = { 0,1,0 };
vec3d vTarget = { 0,sinf(pitch),cosf(pitch) };
mat4x4 matCameraRot = Matrix_MakeRotationY(fYaw-PI/2);
vLookDir = Matrix_MultiplyVector(matCameraRot, vTarget);
vec3d playerCenter = { player.GetPos().x, player.GetPos().y, player.GetPos().z };
vTarget = Vector_Add(freeRoam ? freeRoamCamera : playerCenter, vLookDir);
mat4x4 matCamera = Matrix_PointAt(freeRoam?freeRoamCamera: playerCenter, vTarget, vUp);
mat4x4 matView = Matrix_QuickInverse(matCamera);
std::vector<Triangle>vecTrianglesToRaster;
// Draw Triangles
for (auto& obj : objects) {
RenderMesh(matView,vecTrianglesToRaster,obj);
}
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);
int triRenderCount = 0; int triRenderCount = 0;
@ -668,6 +674,9 @@ void FaceBall::HandleKeys(float fElapsedTime) {
} }
else { else {
pitch = 0; pitch = 0;
if (GetMouse(0).bPressed) {
bullets.push_back({ bullet,{player.GetPos().x + player.GetRadius(),player.GetPos().y, player.GetPos().z + player.GetRadius()},fYaw,0.25f,{shotSpd * std::cosf(fYaw),shotSpd * std::sinf(fYaw)} });
}
} }
if (GetKey(W).bHeld) { if (GetKey(W).bHeld) {
if (freeRoam) { if (freeRoam) {
@ -748,10 +757,12 @@ bool FaceBall::OnUserCreate()
arrow = new Decal(new Sprite("assets/arrow.png")); arrow = new Decal(new Sprite("assets/arrow.png"));
enemy_ShootMe_tex = new Decal(new Sprite("assets/enemies/ShootMe.png")); enemy_ShootMe_tex = new Decal(new Sprite("assets/enemies/ShootMe.png"));
YAZAWA = new Decal(new Sprite("assets/yazawa.png")); YAZAWA = new Decal(new Sprite("assets/yazawa.png"));
bullet_tex = new Decal(new Sprite("assets/enemies/bullet.png"));
InitializeEnemyData(); InitializeEnemyData();
Mesh testEnemy("assets/enemies/ShootMe.obj", enemy_ShootMe_tex); Mesh testEnemy("assets/enemies/ShootMe.obj", enemy_ShootMe_tex);
bullet = { "assets/enemies/bullet.obj",bullet_tex };
mapMesh.texture = YAZAWA; mapMesh.texture = YAZAWA;
LoadLevel(1); LoadLevel(1);
@ -759,8 +770,28 @@ bool FaceBall::OnUserCreate()
return true; return true;
} }
bool Bullet::Update(float fElapsedTime) {
if (!FaceBall::CheckCollision({ spd.x,0,spd.y }, {pos.x,pos.z}, radius)) {
pos.x += spd.x*fElapsedTime;
pos.z += spd.y*fElapsedTime;
}
else {
return false;
}
return true;
}
bool FaceBall::OnUserUpdate(float fElapsedTime) bool FaceBall::OnUserUpdate(float fElapsedTime)
{ {
for (std::vector<Bullet>::iterator it = bullets.begin(); it != bullets.end(); ++it) {
Bullet& b = *it;
if (!b.Update(fElapsedTime)) {
it=bullets.erase(it);
if (it == bullets.end()) {
break;
}
}
}
switch (mode) { switch (mode) {
case GAME: { case GAME: {
HandleKeys(fElapsedTime); HandleKeys(fElapsedTime);

@ -139,8 +139,14 @@ private:
struct Object { struct Object {
Mesh mesh; Mesh mesh;
vf2d pos = { 0,0 }; vec3d pos = { 0,0 };
float rot = 0; float rot = 0;
float radius = 0.2f;
};
struct Bullet : Object{
vf2d spd = { 0,0 };
bool Update(float fElapsedTime);
}; };
struct EnemyData { struct EnemyData {
@ -171,13 +177,14 @@ class FaceBall : public PixelGameEngine
EnemyData GetData(EnemyID id); EnemyData GetData(EnemyID id);
Decal* circle,*arrow,*YAZAWA; Decal* circle,*arrow,*YAZAWA;
private: private:
Mesh mapMesh; Mesh mapMesh,enemy_ShootMe,bullet;
Decal* dot, * enemy_ShootMe_tex; Decal* dot, * enemy_ShootMe_tex,*bullet_tex;
vi2d MAP_SIZE = { 10,10 }; vi2d MAP_SIZE;
std::vector<std::vector<MapSquare>>map; std::vector<std::vector<MapSquare>>map;
std::map<EnemyID, EnemyData>enemyData; std::map<EnemyID, EnemyData>enemyData;
std::vector<Object>objects; std::vector<Object>objects;
std::vector<Bullet>bullets;
GAMEMODE mode=GAMEMODE::GAME; GAMEMODE mode=GAMEMODE::GAME;
Editor editor; Editor editor;
int level=1; int level=1;
@ -195,6 +202,8 @@ class FaceBall : public PixelGameEngine
Player player = { {3.7,0.3,0.7}, {{0.5,0.5},0.2} }; Player player = { {3.7,0.3,0.7}, {{0.5,0.5},0.2} };
vec3d freeRoamCamera = { 1,0.5,1 }; vec3d freeRoamCamera = { 1,0.5,1 };
float shotSpd = 1.5f;
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);
@ -223,5 +232,7 @@ class FaceBall : public PixelGameEngine
void OnTextEntryComplete(const std::string& sText) override; void OnTextEntryComplete(const std::string& sText) override;
void InitializeEnemyData(); void InitializeEnemyData();
void LoadLevel(int level); void LoadLevel(int level);
bool CheckCollision(vec3d movementVector,vf2d pos,float radius); void RenderMesh(mat4x4&matView, std::vector<Triangle>&vecTrianglesToRaster,Object&o);
public:
static bool CheckCollision(vec3d movementVector,vf2d pos,float radius);
}; };
Loading…
Cancel
Save