Skip to content

Instantly share code, notes, and snippets.

@1e4
Created December 5, 2020 04:12
Show Gist options
  • Save 1e4/29132c51f71a2983e86ee15af60e5f99 to your computer and use it in GitHub Desktop.
Save 1e4/29132c51f71a2983e86ee15af60e5f99 to your computer and use it in GitHub Desktop.
Simple turn based PvE script
<?php
$monster = [
'health' => 100,
'aps' => 1.3,
'damage' => 40
];
$player = [
'health' => 1000,
'aps' => 0.4,
'damage' => 20
];
//echo 100 / ($monster['aps'] * 100);
class Fight {
private $playerTicks = 0;
private $monsterTicks = 0;
private $log = [];
public function __construct(
private array $player,
private array $monster
) {
$this->fight();
}
private function fight() {
$this->playerTicks += 0.1;
$this->monsterTicks += 0.1;
if($this->playerTicks === $this->player['aps'])
{
$this->monster['health'] -= $this->player['damage'];
$this->log[] = "Player attacked dealing {$this->player['damage']}";
$this->playerTicks = 0;
}
if($this->monsterTicks === $this->monster['aps'])
{
$this->player['health'] -= $this->monster['damage'];
$this->log[] = "Monster attacked dealing {$this->monster['damage']}";
$this->monsterTicks = 0;
}
if($this->monster['health'] <= 0) {
$this->log[] = "Battle Finished, you won";
} elseif($this->player['health'] <= 0) {
$this->log[] = "Battle Finished, you lost";
} else {
$this->fight();
}
}
public function printLog() {
var_dump($this->log);
}
}
$fight = new Fight($player, $monster);
$fight->printLog();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment