feat(engine): creats local player when running game

This commit is contained in:
WindClan 2025-07-08 07:06:11 -04:00 committed by m-doescode
parent 0134d5c0a9
commit 3d23c446ea
5 changed files with 24 additions and 14 deletions

View file

@ -32,6 +32,10 @@ void DataModel::Init(bool runMode) {
service->InitService();
if (runMode) service->OnRun();
}
if (runMode) {
GetService<Players>()->createLocalPlayer(0);
}
}
void DataModel::SaveToFile(std::optional<std::string> path) {

View file

@ -2,10 +2,5 @@
#include "objects/service/players.h"
#include "objects/datamodel.h"
Player::Player(): Instance(&TYPE) {
dataModel().value()->GetService<Players>()->numPlayers += 1;
}
Player::~Player() {
// Subtract from player count on remove
dataModel().value()->GetService<Players>()->numPlayers -= 1;
}
Player::Player(): Instance(&TYPE) {}
Player::~Player() {}

View file

@ -5,7 +5,7 @@
#include "objects/model.h"
#include <memory>
class DEF_INST_(not_creatable) Player : public Instance {
class DEF_INST_() Player : public Instance {
AUTOGEN_PREAMBLE
public:

View file

@ -3,6 +3,8 @@
#include "objects/datamodel.h"
#include "objects/player.h"
#include <memory>
#include <string>
#include <iostream>
int numPlayers;
int maxPlayers;
@ -22,10 +24,18 @@ void Players::InitService() {
}
}
std::shared_ptr<Player> Players::createLocalPlayer(int userId) {
std::optional<std::shared_ptr<Player>> Players::createLocalPlayer(int userId) {
if (!dataModel()) return std::nullopt;
std::shared_ptr<Player> newPlr = Player::New();
newPlr->name = "Player"+std::to_string(userId);
newPlr->userId = userId;
dataModel().value()->GetService<Players>()->AddChild(newPlr);
this->localPlayer = newPlr;
this->AddChild(newPlr);
return newPlr;
}
void Players::removeLocalPlayer() {
if (!this->localPlayer.lock()) return;
this->localPlayer.lock()->Destroy();
this->localPlayer = std::weak_ptr<Player>();
}

View file

@ -14,11 +14,12 @@ public:
~Players();
static inline std::shared_ptr<Instance> Create() { return std::make_shared<Players>(); };
std::shared_ptr<Player> createLocalPlayer(int userId);
std::optional<std::shared_ptr<Player>> createLocalPlayer(int userId);
void removeLocalPlayer();
DEF_PROP_CATEGORY(DATA)
DEF_PROP_(readonly,no_save)
int numPlayers = 0;
DEF_PROP_(readonly,no_save) int numPlayers = 0;
DEF_PROP int maxPlayers = 10;
DEF_PROP std::weak_ptr<Player> localPlayer;
DEF_PROP_(readonly,no_save) std::weak_ptr<Player> localPlayer;
};