<?php

// --------------------------------------------
abstract class Task {
	abstract function has_ended();
	abstract function start();
	abstract function update($time);
}

// --------------------------------------------
class TaskSequencer {
	protected $tasks = array();
	protected $active_task = NULL;

	// --------------------------------------------
	function add($task) {
		$this->tasks[] = $task;
	}

	// --------------------------------------------
	function update($time) {
		if (count($this->tasks) <= 0) {
			return;
		}

		if ($this->active_task == NULL) {
			// start now
			$this->active_task = reset($this->tasks);
			$this->active_task->start();
		}

		// update
		// -- if active_task->start causes pre_begin_match to be ran, task manager can get cleared during that, prepare for it
		if ($this->active_task) {
			$this->active_task->update($time);
		}

		// same for update
		if ($this->active_task) {
			if ($this->active_task->has_ended()) {
				// cleanup and forget at end
				array_shift($this->tasks);
				$this->active_task = NULL;
			}
		}
	}

	// --------------------------------------------
	function clear() {
		$this->tasks = array();
		$this->active_task = NULL;
	}
};


?>