LobbyCountdown stuff

Signed-off-by: Aaron Brock <TheMineBench@gmail.com>
This commit is contained in:
Aaron Brock 2015-07-05 22:45:14 -04:00
parent b4349a164b
commit 3f202d39ae
6 changed files with 165 additions and 21 deletions

View File

@ -10,12 +10,17 @@ namespace mineplex\plugin\gameengine\game\components\countdown;
use mineplex\plugin\gameengine\arenas\Arena;
use mineplex\plugin\gameengine\arenas\events\ArenaEndEvent;
use mineplex\plugin\gameengine\arenas\events\ArenaJoinEvent;
use mineplex\plugin\gameengine\arenas\events\ArenaQuitEvent;
use mineplex\plugin\gameengine\game\components\gamestate\GameStateComponent;
use mineplex\plugin\gameengine\game\components\world\event\WorldLoadSuccessEvent;
use mineplex\plugin\gameengine\game\components\world\WorldComponent;
use mineplex\plugin\gameengine\time\BenchSchedule;
use mineplex\plugin\gameengine\time\BenchTask;
use mineplex\plugin\gameengine\time\BenchTaskData;
use pocketmine\event\HandlerList;
use pocketmine\event\Listener;
use pocketmine\Player;
use pocketmine\Server;
use mineplex\plugin\gameengine\game\components\gamestate\events\GameStateChangeEvent;
@ -24,14 +29,26 @@ class LobbyCountdown implements Listener, BenchTask {
private $startCount;
private $count;
private $gameStateComponent;
private $worldComponent;
private $arena;
private $startGameState;
private $setGameState;
private $minPlayers;
public function __construct(Arena $arena, GameStateComponent $gameStateComponent, $count, $startGameState, $setGameState)
private $message;
const WAITING_FOR_PLAYERS = "Waiting for players!";
const POPUP_ID = "popup";
const COUNTDOWN_ID = "count";
public function __construct(Arena $arena, GameStateComponent $gameStateComponent, WorldComponent $worldComponent, $startGameState, $setGameState, $count, $minPlayers = 1)
{
$this->arena = $arena;
$this->gameStateComponent = $gameStateComponent;
$this->worldComponent = $worldComponent;
$this->minPlayers = $minPlayers;
$this->startCount = $count;
$this->count = $count;
@ -39,18 +56,60 @@ class LobbyCountdown implements Listener, BenchTask {
$this->startGameState = $startGameState;
$this->setGameState = $setGameState;
$this->message = self::WAITING_FOR_PLAYERS;
Server::getInstance()->getPluginManager()->registerEvents($this, $arena->getPlugin());
}
function checkCountdown()
/**
* @param bool $addOne
*/
function checkCountdown($addOne = false)
{
$playerCount = (count($this->arena->getPlayers()) + $addOne);
if ($this->gameStateComponent->getGameState() == $this->startGameState && $this->worldComponent->isWorldReady() && $playerCount >= $this->minPlayers)
{
if (!BenchSchedule::isRunningWithId($this, self::COUNTDOWN_ID))
{
$this->count = $this->startCount;
$this->setCount();
BenchSchedule::runTaskTimerWithId($this, 1000, 1000, self::COUNTDOWN_ID);
}
}
else
{
$this->setWaiting($playerCount);
BenchSchedule::cancelTaskWithId($this, self::COUNTDOWN_ID);
}
}
public function onJoin(ArenaJoinEvent $event)
{
if ($event->getArena() !== $this->arena)
return;
$this->checkCountdown(true);
$this->popup($event->getPlayer());
}
public function onQuit(ArenaQuitEvent $event)
{
if ($event->getArena() !== $this->arena)
return;
$this->checkCountdown();
}
public function onWorldCreation(WorldLoadSuccessEvent $event)
{
if ($event->getArena() !== $this->arena)
return;
$this->checkCountdown();
}
public function onGameStateChange(GameStateChangeEvent $event)
{
if ($event->getArena() !== $this->arena)
@ -58,11 +117,15 @@ class LobbyCountdown implements Listener, BenchTask {
if ($event->getToGameState() == $this->startGameState)
{
BenchSchedule::runTaskTimer($this, 1000, 1000);
$this->checkCountdown();
BenchSchedule::runTaskTimerWithId($this, 500, 500, self::POPUP_ID);
}
else
elseif ($event->getFromGameState() == $this->startGameState)
{
//Cancels both tasks
BenchSchedule::cancelTask($this);
$this->count = $this->startCount;
}
}
@ -77,27 +140,51 @@ class LobbyCountdown implements Listener, BenchTask {
public function run(BenchTaskData $data)
{
print "Count: $this->count"."\n";
if ($data->getId() == self::POPUP_ID)
{
$this->popupAll();
}
else //if ($data->getId() == self::COUNTDOWN_ID)
{
if ($this->count <= 0)
{
$this->gameStateComponent->setGameState($this->setGameState);
$data->end();
return;
}
$this->popup();
$this->message = "§9Game starting in:§c $this->count";
$this->count--;
}
}
public function popup()
private function setWaiting($playerCount = null)
{
if ($playerCount == null)
$playerCount = count($this->arena->getPlayers());
$this->message = "§2Waiting for players! §a($playerCount/$this->minPlayers)";
$this->popupAll();
}
private function setCount($count = null)
{
if ($count == null)
$count = $this->count;
$this->message = "§9Game starting in:§c $count";
$this->popupAll();
}
private function popupAll()
{
foreach ($this->arena->getPlayers() as $player)
{
$player->sendPopup("Countdown: $this->count");
$this->popup($player);
}
}
private function popup(Player $player)
{
$player->sendPopup($this->message);
}
}

View File

@ -75,12 +75,14 @@ class GameStateComponent implements Listener {
private function localSetState($gameState)
{
$oldGameState = $this->getGameState();
$event = new GameStateChangeEvent($this->arena, $this->gameState, $gameState);
$this->gameState = $gameState;
$event = new GameStateChangeEvent($this->arena, $oldGameState, $gameState);
//Not sure if I should call the event before of after...
Server::getInstance()->getPluginManager()->callEvent($event);
$this->gameState = $gameState;
}
public function getGameState()

View File

@ -124,7 +124,7 @@ class WorldComponent implements Listener
print("Successfully Loaded World: " . $this->gameFolder . "\n");
Server::getInstance()->getPluginManager()->callEvent(new WorldLoadSuccessEvent($this->arena));
Server::getInstance()->getPluginManager()->callEvent(new WorldLoadSuccessEvent($this->arena, $this->world));
}
else
{

View File

@ -5,13 +5,23 @@ namespace mineplex\plugin\gameengine\game\components\world\event;
use mineplex\plugin\gameengine\arenas\ArenaEvent;
use mineplex\plugin\gameengine\arenas\Arena;
use pocketmine\level\Level;
class WorldLoadSuccessEvent extends ArenaEvent
{
public static $handlerList = null;
public function __construct(Arena $arena)
private $level;
public function __construct(Arena $arena, Level $level)
{
parent::__construct($arena);
$this->level = $level;
}
function getLevel()
{
return $this->level;
}
}

View File

@ -9,11 +9,13 @@
namespace mineplex\plugin\gameengine\game\games\sg;
use mineplex\plugin\gameengine\game\components\countdown\GameStateCountdown;
use mineplex\plugin\gameengine\game\components\countdown\LobbyCountdown;
use mineplex\plugin\gameengine\game\components\feature\features\NoBlockBreak;
use mineplex\plugin\gameengine\game\components\feature\GameStateFeatureManager;
use mineplex\plugin\gameengine\game\components\gamestate\GameState;
use mineplex\plugin\gameengine\game\components\gamestate\GameStateComponent;
use mineplex\plugin\gameengine\game\components\lobby\LobbyComponent;
use mineplex\plugin\gameengine\game\components\world\WorldComponent;
use mineplex\plugin\gameengine\game\Game;
use mineplex\plugin\gameengine\arenas\Arena;
use mineplex\plugin\gameengine\game\components\feature\Feature;
@ -40,7 +42,9 @@ class SurvivalGames implements Game {
//new GameStateFeatureManager($arena, $features);
new GameStateCountdown($arena, $gameStateComponent, 20, GameState::LOBBY, GameState::PRE_GAME);
$worldComponent = new WorldComponent($arena);
new LobbyCountdown($arena, $gameStateComponent, $worldComponent, GameState::LOBBY, GameState::PRE_GAME, 50);
new GameStateCountdown($arena, $gameStateComponent, 10, GameState::PRE_GAME, GameState::GAME);

View File

@ -62,31 +62,72 @@ class BenchSchedule extends Task
/**
* @param BenchTask $taskToCancel
* @return bool
*/
public static function cancelTask(BenchTask $taskToCancel)
{
$deleted = false;
foreach (self::getInstance()->tasks as $key => $task)
{
if ($task->getTaskData()->getTask() === $taskToCancel)
{
unset (self::getInstance()->tasks[$key]);
$deleted = true;
}
}
return $deleted;
}
/**
* @param BenchTask $taskToCancel
* @param $id
* @return bool
*/
public static function cancelTaskWithId(BenchTask $taskToCancel, $id)
{
$deleted = false;
foreach (self::getInstance()->tasks as $key => $task)
{
if ($task->getTaskData()->getTask() === $taskToCancel && $task->getTaskData()->getId() === $id)
{
unset (self::getInstance()->tasks[$key]);
$deleted = true;
}
}
return $deleted;
}
/**
* @param BenchTask $taskToCancel
* @return bool
*/
public static function isRunning(BenchTask $taskToCancel)
{
foreach (self::getInstance()->tasks as $key => $task)
{
if ($task->getTaskData()->getTask() === $taskToCancel)
{
return true;
}
}
return false;
}
/**
* @param BenchTask $taskToCancel
* @param $id
* @return bool
*/
public static function isRunningWithId(BenchTask $taskToCancel, $id)
{
foreach (self::getInstance()->tasks as $key => $task)
{
if ($task->getTaskData()->getTask() === $taskToCancel && $task->getTaskData()->getId() === $id)
{
return true;
}
}
return false;
}
/**