你应该做的是让一些真正的组织进入游戏。
我以前从未构建过 PHP 游戏,但我非常了解结构应该如何。
一个实体/怪物应该由几个定义其特征的类组成
这是我头顶的一个小例子:
abstract class NonHuman implements Strengh,Weapons,Vehicles
{
var $strength;
}
abstract class Vermin implements Strengh,Chemicals
{
var $strength = 20;
var $poisonous = true;
}
abstract class Humanoid implements Strengh,Weapons,Vehicles,Arms,Legs
{
}
抽象类的基本布局如下:
abstract class <BeingType> implements < Characteristics , Weapons , Etc>
{
// Depending on < Characteristics , Weapons , Etc> you should
// Build the methods here so that theres less work in the long run.
}
然后,一旦你有了你的基本类型,你就可以做类似的事情
class Rat extends Vermin
{
public function __construct($name,$strength = 50)
{
$this->strength = $strength;
}
//Any new methods here would be specific to this Being / Rat.
}
$Robert = new Rat('Robert',80);
$Andrew = new Rat('Andrew',22);
if($Robert->strength > 50)
{
$Robert->Kick($Andrew,'left',20); //20 mph lol
if($Andrew->IsAlive())
{
if($Robert->TakeWeapon($Andrew,20)) //Uses 20% force
{
$Robert->FireWeaponAt($Andrew,-1); //Use all bullets on andrew!
}
}
if(!$Andrew->IsAlive())
{
$Robert->UpdateScoreFromPLayer($Andrew,100); //Max of 100 points if andrew has them.
}
}
通过这样做,为实体生成特征并不难。
您还可以设置父析构函数将用户名数据保存在数据库中以备下次使用,并使用 __construct 更新类数据。
希望这能给你一个好主意:)
还有更多:)
如果您为 SpecialMoves 上课,可以说您总是可以这样做
$Robert->AddSpecialMove('Roundhouse',new SpecialMove_Roundhouse(12));
$Robert->UserSpecialMove('Roundhouse',2);/ x2
if($Robert->_SpecialMoves->Roundhouse->Left() < 12)
{
$Robert->UserSpecialMove('Roundhouse',-1);/ Use all kicks.
}
在SpecialMove_Roundhouse 内,它会包含一些参数,例如损坏、完成所需的时间、它使用多少能量、你可以使用多少次。
在范围内的第一类应该总是我一个计算器,用于计算心率、血液水平、能量、库存等,所以你总是有必需品!
实现示例
实现确保更高的类包含某些函数和变量
interface Weapons
{
public function Fire($target,$bullets);
}
class Colt45 implements Weapons
{
var $damage = 2;
var $max_bullets = 80;
var $clip = 80;
//THIS CLASS MUST HAVE FIRE
public function fire($target,$bullets)
{
$ammo = $bullets > $clip ? $clip : $ammo;
for($shot=0;$shot<=$ammo;$shot++)
{
$target->ReduceHealth($damage);
if(!$target->IsAlive())
{
break;
}
$clip--; //Reduce ammo in clip.
}
}
}
此处的示例取自 php.net | http://www.php.net/manual/en/language.oop5.interfaces.php#96368
<?php
interface Auxiliary_Platform
{
public function Weapon();
public function Health();
public function Shields();
}
class T805 implements Auxiliary_Platform
{
public function Weapon()
{
var_dump(__CLASS__);
}
public function Health()
{
var_dump(__CLASS__ . "::" . __FUNCTION__);
}
public function Shields()
{
var_dump(__CLASS__ . "->" . __FUNCTION__);
}
}
class T806 extends T805 implements Auxiliary_Platform
{
public function Weapon()
{
var_dump(__CLASS__);
}
public function Shields()
{
var_dump(__CLASS__ . "->" . __FUNCTION__);
}
}
$T805 = new T805();
$T805->Weapon();
$T805->Health();
$T805->Shields();
echo "<hr />";
$T806 = new T806();
$T806->Weapon();
$T806->Health();
$T806->Shields();
/* Output:
string(4) "T805"
string(12) "T805::Health"
string(13) "T805->Shields"
<hr />string(4) "T806"
string(12) "T805::Health"
string(13) "T806->Shields"
*/
?>