Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main menu #2

Merged
merged 3 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion data/hero.txt

This file was deleted.

7 changes: 4 additions & 3 deletions include/client_entity_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ class C_EntityManager : public InContext, public sf::Drawable
bool renderingEntityData;
#endif

bool renderingEntitiesUI;
mutable bool renderingEntitiesUI;

static void loadEntityData(const Context& context);
static C_Entity* getEntityData(u8 entityType);

private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
Expand All @@ -112,8 +115,6 @@ class C_EntityManager : public InContext, public sf::Drawable

static bool m_entitiesJsonLoaded;
static std::unique_ptr<C_Entity> m_entityData[ENTITY_MAX_TYPES];

static void loadEntityData(const Context& context);

u32 m_controlledEntityUniqueId;
u8 m_controlledEntityTeamId;
Expand Down
5 changes: 5 additions & 0 deletions include/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ class C_Entity : public BaseEntityComponent

virtual void insertRenderNode(const C_ManagersContext& managersContext, const Context& context);

u16 getTextureId() const;
float getScale() const;
bool getUseSubTextureRect() const;
sf::IntRect getSubTextureRect() const;

protected:
u16 m_textureId;
float m_scale;
Expand Down
36 changes: 17 additions & 19 deletions include/game_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ struct GameClientCallbacks : public ISteamNetworkingSocketsCallbacks
GameClient* parent;
};

class GameClient : public InContext, public NetPeer
class GameClient : public InContext, public NetPeer, public sf::Drawable
{
public:
friend struct GameClientCallbacks;

struct ConfigData {
SteamNetworkingIPAddr endpoint;
sf::Time inputRate;
std::string displayName;
u8 pickedHero;
};

struct Snapshot {
C_EntityManager entityManager;
u32 id = 0;
Expand All @@ -52,13 +59,12 @@ class GameClient : public InContext, public NetPeer
};

public:
GameClient(const Context& context);
GameClient(const Context& context, const ConfigData& configData);
~GameClient();

void mainLoop(bool& running);

void receiveLoop();
void update(sf::Time eTime);
void updateWorldTime(sf::Time eTime);

void renderUpdate(sf::Time eTime);

Expand All @@ -83,12 +89,10 @@ class GameClient : public InContext, public NetPeer

Snapshot* findSnapshotById(u32 snapshotId);

void loadFromJson(const rapidjson::Document& doc);

private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const;

void loadMap(const std::string& filename);
void readDisplayName(const std::string& filename);
void readSelectedHero(const std::string& filename);

private:
GameClientCallbacks m_gameClientCallbacks;
Expand All @@ -98,16 +102,9 @@ class GameClient : public InContext, public NetPeer
bool m_connected;
sf::Time m_infoTimer;

Vector2u m_screenSize;
u32 m_screenStyle;

sf::RenderTexture m_canvas;
mutable sf::RenderTexture m_canvas;
bool m_canvasCreated;

sf::Time m_updateRate;
sf::Time m_inputRate;
sf::Time m_renderRate;

C_EntityManager m_entityManager;
std::list<Snapshot> m_snapshots;

Expand All @@ -116,6 +113,7 @@ class GameClient : public InContext, public NetPeer

sf::Time m_worldTime;
sf::Time m_interElapsed;
sf::Time m_inputRate;

//Number of snapshots required to start rendering
int m_requiredSnapshotsToRender;
Expand All @@ -137,15 +135,15 @@ class GameClient : public InContext, public NetPeer
float m_smoothUnitRadius;
Vector2 m_smoothUnitPos;

std::unique_ptr<ClientCaster> m_clientCaster;
ClientCaster m_clientCaster;

bool m_forceFullSnapshotUpdate;
bool m_fullUpdateReceived;

std::string m_displayName;
int m_selectedHero;
u8 m_pickedHero;

sf::Sprite m_mouseSprite;

std::unique_ptr<ConnectionStatusRender> m_connectionStatusRender;
ConnectionStatusRender m_connectionStatusRender;
};
2 changes: 1 addition & 1 deletion include/game_mode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GameMode
virtual void draw(sf::RenderTexture& renderTexture, const TextureLoader* textures);

//we don't use drawable since this class is shared by client and server
virtual void drawGameEndInfo(sf::RenderWindow& window, const FontLoader* fonts);
virtual void drawGameEndInfo(sf::RenderTarget& target, const FontLoader* fonts);

//creates chests and other game mode specific entities
//(called after heroes are created)
Expand Down
2 changes: 1 addition & 1 deletion include/game_modes/battle_royale_mode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BattleRoyaleMode : public GameMode
//@storm_remove: remove this when the storm gets implemented with tiles
virtual void draw(sf::RenderTexture& renderTexture, const TextureLoader* textures);

virtual void drawGameEndInfo(sf::RenderWindow& window, const FontLoader* fonts);
virtual void drawGameEndInfo(sf::RenderTarget& target, const FontLoader* fonts);

virtual void onGameStarted(u8 numberOfPlayers, const ManagersContext& context);
virtual void C_onGameStarted();
Expand Down
4 changes: 2 additions & 2 deletions include/game_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class GameServer : public InContext, public NetPeer
bool heroDead = true;

//EntityType of hero selected (before/after matchmaking)
u8 selectedHeroType = ENTITY_MAX_TYPES;
u8 pickedHeroType = ENTITY_MAX_TYPES;

int ping = -1;
};
Expand All @@ -66,7 +66,7 @@ class GameServer : public InContext, public NetPeer
//@TODO: Should we use this??
struct ClientInfo_cold {
//display name
//selectedHeroType
//pickedHeroType
//cosmetics??
};

Expand Down
4 changes: 4 additions & 0 deletions include/hero.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "unit.hpp"
#include "hero_ui.hpp"

//@TODO: Automate this for new heroes
constexpr u8 g_numberOfHeroes = 3;
constexpr u8 g_heroTypes[g_numberOfHeroes] = {ENTITY_RED_DEMON, ENTITY_BLONDIE, ENTITY_FISH_OGRE};

class HeroBase
{
public:
Expand Down
60 changes: 60 additions & 0 deletions include/main_menu.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once

#include <SFML/Graphics/Drawable.hpp>

#include "context.hpp"
#include "game_client.hpp"
#include "hero.hpp"

class MainMenu : public InContext, public sf::Drawable
{
public:
MainMenu(const Context& context);

void mainLoop(bool& running);

void startGame();
void stopGame();

void update(sf::Time eTime);
void handleInput(const sf::Event& event, bool focused);

void loadFromJson(const rapidjson::Document& doc);

void readDisplayName();

private:
void draw(sf::RenderTarget& target, sf::RenderStates tates) const;

void _doUpdate(sf::Time eTime);
void _doHandleInput(const sf::Event& event);
void _doDraw(sf::RenderTarget& target, sf::RenderStates states) const;

void resetHeroSelection();

std::unique_ptr<GameClient> m_gameClient;

std::unique_ptr<sf::RenderWindow> m_window;
sf::View m_view;

bool m_stopRunning;

SteamNetworkingIPAddr m_endpoint;

sf::Time m_updateRate;
sf::Time m_renderRate;
sf::Time m_inputRate;
Vector2u m_screenSize;
u32 m_screenStyle;

std::string m_displayName;

bool m_selectedHeroes[g_numberOfHeroes];
sf::FloatRect m_heroButtons[g_numberOfHeroes];
sf::FloatRect m_playButton;

static const sf::Color m_backgroundColor;
static const sf::Color m_buttonColor;
static const sf::Color m_hoveringColor;
static const sf::Color m_selectedColor;
};
2 changes: 1 addition & 1 deletion include/network_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ enum class ServerCommand {
ChangeInputRate,
ChangeSnapshotRate,
DisplayName,
SelectedHero
PickedHero
};
4 changes: 2 additions & 2 deletions include/tilemap_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class TileMapRenderer : public InContext
void generateLayers();
void updateLayers();

void renderBeforeEntities(sf::RenderTexture& window);
void renderAfterEntities(sf::RenderTexture& window);
void renderBeforeEntities(sf::RenderTexture& window) const;
void renderAfterEntities(sf::RenderTexture& window) const;

private:
Vector2u _getTextureCoords(TileType tile);
Expand Down
2 changes: 2 additions & 0 deletions include/unit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class _UnitBase
Status& getStatus();
const Status& getStatus() const;

u8 getWeaponId() const;

static Vector2 moveCollidingTilemap_impl(const Vector2& oldPos, Vector2 newPos, float collisionRadius, TileMap* map);

protected:
Expand Down
2 changes: 1 addition & 1 deletion src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ Vector2 Camera::_clamp(const Vector2& pos) const
}

return newPos;
}
}
33 changes: 21 additions & 12 deletions src/client_entity_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,27 @@ std::vector<RenderNode>& C_EntityManager::getUIRenderNodes()
return m_uiRenderNodes;
}

void C_EntityManager::loadEntityData(const Context& context)
{
if (m_entitiesJsonLoaded) return;

#define DoEntity(class_name, type, json_id) \
m_entityData[ENTITY_##type] = std::unique_ptr<C_Entity>(new C_##class_name()); \
m_entityData[ENTITY_##type]->setEntityType(ENTITY_##type); \
m_entityData[ENTITY_##type]->loadFromJson(*(context.jsonParser->getDocument(json_id)), TextureId::type, context);
#include "entities.inc"
#undef DoEntity

m_entitiesJsonLoaded = true;
}

C_Entity* C_EntityManager::getEntityData(u8 entityType)
{
if (entityType >= ENTITY_MAX_TYPES) return nullptr;

return m_entityData[entityType].get();
}

void C_EntityManager::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
if (renderingEntitiesUI) {
Expand Down Expand Up @@ -478,16 +499,4 @@ void C_EntityManager::draw(sf::RenderTarget& target, sf::RenderStates states) co
bool C_EntityManager::m_entitiesJsonLoaded = false;
std::unique_ptr<C_Entity> C_EntityManager::m_entityData[ENTITY_MAX_TYPES];

void C_EntityManager::loadEntityData(const Context& context)
{
if (m_entitiesJsonLoaded) return;

#define DoEntity(class_name, type, json_id) \
m_entityData[ENTITY_##type] = std::unique_ptr<C_Entity>(new C_##class_name()); \
m_entityData[ENTITY_##type]->setEntityType(ENTITY_##type); \
m_entityData[ENTITY_##type]->loadFromJson(*(context.jsonParser->getDocument(json_id)), TextureId::type, context);
#include "entities.inc"
#undef DoEntity

m_entitiesJsonLoaded = true;
}
20 changes: 20 additions & 0 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,26 @@ void C_Entity::insertRenderNode(const C_ManagersContext& managersContext, const
#endif
}

u16 C_Entity::getTextureId() const
{
return m_textureId;
}

float C_Entity::getScale() const
{
return m_scale;
}

bool C_Entity::getUseSubTextureRect() const
{
return m_useSubTextureRect;
}

sf::IntRect C_Entity::getSubTextureRect() const
{
return m_subTextureRect;
}

void HealthComponent::takeDamage(u16 damage, Entity* source, u32 uniqueId, u8 teamId)
{
m_health = std::max((int) m_health - (int) damage, 0);
Expand Down
Loading