[Updated] Code for use with WASM, [TODO] Make enemy gun follow player, spawn bullets from tip of the barrel
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include "raylib.h"
|
||||
#include "services/Service.h"
|
||||
#include "services/AssetService.h"
|
||||
#include "entities/Bullet.h"
|
||||
#include "entities/PBullet.h"
|
||||
#include "entities/EBullet.h"
|
||||
#include <optional>
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#include "raylib.h"
|
||||
#include "services/Service.h"
|
||||
#include "services/MapService.h"
|
||||
#include "entities/Vector2i.h"
|
||||
#include "entities/Crate.h"
|
||||
#include "entities/Jelly.h"
|
||||
@@ -94,198 +93,22 @@ private:
|
||||
std::vector<Powerup> powerUps;
|
||||
Flag flag;
|
||||
|
||||
void initCrates()
|
||||
{
|
||||
crates.clear();
|
||||
int tileSize = MapService::getInstance().getMap().getTileSize();
|
||||
for (int i = 0; i < amountOfCrates; i++)
|
||||
{
|
||||
crates.push_back(Crate());
|
||||
}
|
||||
crates[0].getObject().setPosition({38.0f * tileSize, 9.0f * tileSize});
|
||||
crates[1].getObject().setPosition({41.0f * tileSize, 5.0f * tileSize});
|
||||
crates[2].getObject().setPosition({68.0f * tileSize, 4.0f * tileSize});
|
||||
|
||||
for (int i = 0; i < crates.size(); i++)
|
||||
{
|
||||
Vector2i crateTilePos = Vector2i(crates[i].getObject().getPosition().x / tileSize, crates[i].getObject().getPosition().y / tileSize);
|
||||
MapService::getInstance().getMap().updateColmapTile(crateTilePos, true);
|
||||
}
|
||||
}
|
||||
|
||||
void initJellies()
|
||||
{
|
||||
jellies.clear();
|
||||
}
|
||||
|
||||
void initAmmoPacks()
|
||||
{
|
||||
ammoPacks.clear();
|
||||
int tileSize = MapService::getInstance().getMap().getTileSize();
|
||||
for (int i = 0; i < amountOfAmmoPacks; i++)
|
||||
{
|
||||
ammoPacks.push_back(AmmoPack());
|
||||
}
|
||||
ammoPacks[0].getObject().setPosition({32.0f * tileSize, 9.0f * tileSize});
|
||||
ammoPacks[1].getObject().setPosition({48.0f * tileSize, 5.0f * tileSize});
|
||||
ammoPacks[2].getObject().setPosition({88.0f * tileSize, 9.0f * tileSize});
|
||||
ammoPacks[3].getObject().setPosition({105.0f * tileSize, 15.0f * tileSize});
|
||||
}
|
||||
|
||||
void initHealthPacks()
|
||||
{
|
||||
healthPacks.clear();
|
||||
int tileSize = MapService::getInstance().getMap().getTileSize();
|
||||
for (int i = 0; i < amountOfHealthPacks; i++)
|
||||
{
|
||||
healthPacks.push_back(HealthPack());
|
||||
}
|
||||
healthPacks[0].getObject().setPosition({28.0f * tileSize, 10.0f * tileSize});
|
||||
healthPacks[1].getObject().setPosition({64.0f * tileSize, 6.0f * tileSize});
|
||||
healthPacks[2].getObject().setPosition({118.0f * tileSize, 11.0f * tileSize});
|
||||
healthPacks[3].getObject().setPosition({74.0f * tileSize, 8.0f * tileSize});
|
||||
}
|
||||
|
||||
void initPowerups()
|
||||
{
|
||||
powerUps.clear();
|
||||
int tileSize = MapService::getInstance().getMap().getTileSize();
|
||||
for (int i = 0; i < amountOfPowerups; i++)
|
||||
{
|
||||
powerUps.push_back(Powerup());
|
||||
}
|
||||
powerUps[0].getObject().setPosition({41.0f * tileSize+8, 10.0f * tileSize-25});
|
||||
}
|
||||
|
||||
void initFlag() {
|
||||
int tileSize = MapService::getInstance().getMap().getTileSize();
|
||||
this->flag = Flag({118.0f*tileSize-tileSize/2.0f, 3.0f * tileSize-8});
|
||||
}
|
||||
|
||||
void crateLogic()
|
||||
{
|
||||
for (auto it = crates.begin(); it != crates.end();)
|
||||
{
|
||||
auto &crate = *it;
|
||||
/**
|
||||
* If the crate has been hit:
|
||||
* 1. Create a Jelly on the location of the crate
|
||||
* 2. Remove the collision tile from the collision map
|
||||
* 3. Remove the crate
|
||||
*/
|
||||
if (crate.hasBeenHit())
|
||||
{
|
||||
Vector2 pos = crate.getObject().getPosition();
|
||||
|
||||
jellies.push_back(Jelly(pos));
|
||||
|
||||
Vector2i tilePos = Vector2i(pos.x / MapService::getInstance().getMap().getTileSize(), pos.y / MapService::getInstance().getMap().getTileSize());
|
||||
MapService::getInstance().getMap().updateColmapTile(tilePos, false);
|
||||
it = crates.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void jellyLogic()
|
||||
{
|
||||
for (int i = 0; i < jellies.size(); i++)
|
||||
{
|
||||
jellies[i].logic();
|
||||
}
|
||||
}
|
||||
|
||||
void ammoPackLogic()
|
||||
{
|
||||
for (auto it = ammoPacks.begin(); it != ammoPacks.end();)
|
||||
{
|
||||
auto &ammoPack = *it;
|
||||
if (ammoPack.hasBeenPickedUp())
|
||||
{
|
||||
it = ammoPacks.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void healthPackLogic()
|
||||
{
|
||||
for (auto it = healthPacks.begin(); it != healthPacks.end();)
|
||||
{
|
||||
auto &healthPack = *it;
|
||||
if (healthPack.hasBeenPickedUp())
|
||||
{
|
||||
it = healthPacks.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void powerupLogic()
|
||||
{
|
||||
for (auto it = powerUps.begin(); it != powerUps.end();)
|
||||
{
|
||||
auto &powerUp = *it;
|
||||
if (powerUp.hasBeenPickedUp())
|
||||
{
|
||||
it = powerUps.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
powerUp.logic();
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void drawCrates()
|
||||
{
|
||||
for (int i = 0; i < crates.size(); i++)
|
||||
{
|
||||
crates[i].draw();
|
||||
}
|
||||
}
|
||||
|
||||
void drawJellies()
|
||||
{
|
||||
for (int i = 0; i < jellies.size(); i++)
|
||||
{
|
||||
jellies[i].draw();
|
||||
}
|
||||
}
|
||||
|
||||
void drawAmmoPacks()
|
||||
{
|
||||
for (int i = 0; i < ammoPacks.size(); i++)
|
||||
{
|
||||
ammoPacks[i].draw();
|
||||
}
|
||||
}
|
||||
|
||||
void drawHealthpacks()
|
||||
{
|
||||
for (int i = 0; i < healthPacks.size(); i++)
|
||||
{
|
||||
healthPacks[i].draw();
|
||||
}
|
||||
}
|
||||
|
||||
void drawPowerups()
|
||||
{
|
||||
for (int i = 0; i < powerUps.size(); i++)
|
||||
{
|
||||
powerUps[i].draw();
|
||||
}
|
||||
}
|
||||
void initCrates();
|
||||
void initJellies();
|
||||
void initAmmoPacks();
|
||||
void initHealthPacks();
|
||||
void initPowerups();
|
||||
void initFlag();
|
||||
void crateLogic();
|
||||
void jellyLogic();
|
||||
void ammoPackLogic();
|
||||
void healthPackLogic();
|
||||
void powerupLogic();
|
||||
void drawCrates();
|
||||
void drawJellies();
|
||||
void drawAmmoPacks();
|
||||
void drawHealthpacks();
|
||||
void drawPowerups();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -3,61 +3,16 @@
|
||||
|
||||
#include "raylib.h"
|
||||
#include "services/Service.h"
|
||||
#include "services/AssetService.h"
|
||||
#include "entities/Enemy.h"
|
||||
#include <optional>
|
||||
|
||||
class EnemyService : public Service<EnemyService>
|
||||
{
|
||||
public:
|
||||
void initEnemies()
|
||||
{
|
||||
this->enemies.clear();
|
||||
const int AMNT_OF_ENEMIES = 8;
|
||||
int tileSize = MapService::getInstance().getMap().getTileSize();
|
||||
for (int i = 0; i < AMNT_OF_ENEMIES; i++)
|
||||
{
|
||||
enemies.push_back(Enemy());
|
||||
}
|
||||
this->enemies[0].getObject().setPosition({31.0f * tileSize, 15.0f * tileSize});
|
||||
this->enemies[1].getObject().setPosition({51.0f * tileSize, 10.0f * tileSize});
|
||||
this->enemies[2].getObject().setPosition({56.0f * tileSize, 5.0f * tileSize});
|
||||
this->enemies[3].getObject().setPosition({78.0f * tileSize, 10.0f * tileSize});
|
||||
this->enemies[4].getObject().setPosition({82.0f * tileSize, 15.0f * tileSize});
|
||||
this->enemies[5].getObject().setPosition({88.0f * tileSize, 4.0f * tileSize});
|
||||
this->enemies[6].getObject().setPosition({98.0f * tileSize, 11.0f * tileSize});
|
||||
this->enemies[7].getObject().setPosition({103.0f * tileSize, 15.0f * tileSize});
|
||||
}
|
||||
|
||||
void enemyLogic()
|
||||
{
|
||||
for (auto it = enemies.begin(); it != enemies.end();)
|
||||
{
|
||||
auto &enemy = *it;
|
||||
if (enemy.getObject().getPosition().y < 0)
|
||||
{
|
||||
it = enemies.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
enemy.logic();
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void drawEnemies()
|
||||
{
|
||||
for (int i = 0; i < enemies.size(); i++)
|
||||
{
|
||||
enemies[i].draw();
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Enemy> &getEnemies()
|
||||
{
|
||||
return this->enemies;
|
||||
}
|
||||
void initEnemies();
|
||||
void enemyLogic();
|
||||
void drawEnemies();
|
||||
std::vector<Enemy> &getEnemies();
|
||||
|
||||
protected:
|
||||
private:
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "raylib.h"
|
||||
#include "services/Service.h"
|
||||
#include "services/AudioService.h"
|
||||
#include "services/InputService.h"
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
@@ -49,7 +50,8 @@ public:
|
||||
|
||||
Vector2 getMouseWorldPos()
|
||||
{
|
||||
Vector2 mouseWorldPos = {GetMousePosition().x + camera.target.x - camera.offset.x, GetMousePosition().y};
|
||||
Vector2 mouseWorldPos = {InputService::getInstance().getMousePos().x + camera.target.x - camera.offset.x,
|
||||
InputService::getInstance().getMousePos().y};
|
||||
return mouseWorldPos;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef INPUT_SERVICE_H
|
||||
#define INPUT_SERVICE_H
|
||||
|
||||
#include "Service.h"
|
||||
#include "raylib.h"
|
||||
#include "Service.h"
|
||||
#include <vector>
|
||||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
@@ -69,6 +69,14 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void checkMousePos() {
|
||||
this->mousePosition = GetMousePosition();
|
||||
}
|
||||
|
||||
Vector2 getMousePos() {
|
||||
return this->mousePosition;
|
||||
}
|
||||
|
||||
bool isKeyPressed(KeyboardKey key)
|
||||
{
|
||||
return keysPressed[key];
|
||||
@@ -88,6 +96,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
Vector2 mousePosition {0,0};
|
||||
std::unordered_set<KeyboardKey> watchedKeys;
|
||||
std::unordered_map<KeyboardKey, bool> keysPressed;
|
||||
std::unordered_map<KeyboardKey, bool> keysDown;
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
#ifndef STATE_SERVICE_H
|
||||
#define STATE_SERVICE_H
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include "services/Service.h"
|
||||
#include "screens/Screen.h"
|
||||
#include "enums/EScreen.h"
|
||||
#include "raylib.h"
|
||||
#include "enums/EScreen.h"
|
||||
#include "services/Service.h"
|
||||
#include "services/InputService.h"
|
||||
#include "screens/Screen.h"
|
||||
#include <memory>
|
||||
|
||||
#if defined(PLATFORM_WEB)
|
||||
#include <emscripten/emscripten.h>
|
||||
#endif
|
||||
// #include <emscripten/html5.h>
|
||||
|
||||
/**
|
||||
* Handles switching between screens
|
||||
@@ -34,14 +35,10 @@ public:
|
||||
void startGame();
|
||||
static void staticStep()
|
||||
{
|
||||
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
std::cout << "Input!" << std::endl;
|
||||
}
|
||||
// Access the singleton instance and call step
|
||||
getInstance().step();
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
std::unique_ptr<Screen> currentScreen;
|
||||
bool gameRunning = true;
|
||||
@@ -52,6 +49,7 @@ private:
|
||||
*/
|
||||
void step();
|
||||
void handleMusic();
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user