diff --git a/Assets/Scripts/Core/player.lua b/Assets/Scripts/Core/player.lua index 21fc80a..1f82274 100644 --- a/Assets/Scripts/Core/player.lua +++ b/Assets/Scripts/Core/player.lua @@ -1,4 +1,12 @@ -Player = {} +Player = { + refineds = 0, + wealth = 0, + research = 0, + faction = -1, + difficulty = -1, + team = -1, + units = {} +} function Player:new(pl) player = pl or {} @@ -34,8 +42,5 @@ end Player.behaviour = { type = BTNodeType.SEQUENCE, children = { - {type = BTNodeType.FUNCTION, func = Player.foo1}, - {type = BTNodeType.FUNCTION, func = Player.foo2}, - {type = BTNodeType.SEQUENCE, children = {{type = BTNodeType.FUNCTION, func = Player.foo3}, {type = BTNodeType.FUNCTION, func = Player.foo4}}} } } diff --git a/game.cpp b/game.cpp index 2c62c3e..d1ef983 100644 --- a/game.cpp +++ b/game.cpp @@ -29,7 +29,17 @@ namespace battleship{ return game; } + void Game::resetLuaGameObjects(){ + sol::state_view SOL_LUA_VIEW = generateView(); + SOL_LUA_VIEW.script("game.players = {}"); + + for(int i = 0; i < players.size(); i++) + SOL_LUA_VIEW.script("game.players[" + to_string(i + 1) + "] = Player:new()"); + } + void Game::update(){ + resetLuaGameObjects(); + for(int i = 0; i < players.size(); i++){ players[i]->update(); diff --git a/game.h b/game.h index 644d277..94cdc2f 100644 --- a/game.h +++ b/game.h @@ -23,6 +23,7 @@ namespace battleship{ inline int getNumPlayers(){return players.size();} private: Game(){} + void resetLuaGameObjects(); bool paused = false; std::vector fx; diff --git a/player.cpp b/player.cpp index 5f45b9d..c7c846a 100755 --- a/player.cpp +++ b/player.cpp @@ -14,14 +14,22 @@ namespace battleship{ Player::Player(int diff, int fac, int t, bool cpuPl, Vector3 sp) : difficulty(diff), faction(fac), team(t), cpuPlayer(cpuPl), spawnPoint(sp) { sol::state_view SOL_LUA_VIEW = generateView(); SOL_LUA_VIEW.script("numPlayers = #game.players"); - int numPlayers = SOL_LUA_VIEW["numPlayers"]; - SOL_LUA_VIEW.script("game.players[" + to_string(numPlayers + 1) + "] = Player:new({id = " + to_string(numPlayers + 1) + "})"); - } + luaPlayerId = SOL_LUA_VIEW["numPlayers"]; + luaPlayerId++; + } - Player::~Player() { - } + Player::~Player() {} void Player::syncLuaPlayer(){ + string playerStr = "game.players[" + to_string(luaPlayerId) + "]."; + + sol::state_view SOL_LUA_VIEW = generateView(); + SOL_LUA_VIEW.script(playerStr + "refineds = " + to_string(refineds)); + SOL_LUA_VIEW.script(playerStr + "research = " + to_string(research)); + SOL_LUA_VIEW.script(playerStr + "wealth = " + to_string(wealth)); + SOL_LUA_VIEW.script(playerStr + "faction = " + to_string(faction)); + SOL_LUA_VIEW.script(playerStr + "difficulty = " + to_string(difficulty)); + SOL_LUA_VIEW.script(playerStr + "team = " + to_string(team)); } void Player::update() { diff --git a/player.h b/player.h index 582b2b3..b431883 100755 --- a/player.h +++ b/player.h @@ -49,7 +49,7 @@ namespace battleship{ inline bool isCpuPlayer(){return cpuPlayer;} private: bool cpuPlayer = false; - int refineds = 0, wealth = 0, research = 0, faction, difficulty, team; + int refineds = 0, wealth = 0, research = 0, faction, difficulty, team, luaPlayerId; std::vector units; std::vector projectiles; std::vector resourceDeposits;