如何使函数可用于多个实例?例如,player1,player2,

时间:2016-06-17 15:33:58

标签: php laravel oop

似乎我忘了什么,现在卡住了这个小东西。

两个小组中的多个玩家之间存在匹配。我如何编写此功能以使其处理每个玩家?

Match.php

public function __construct()
{
    $this->redTeam = new Team();
    $this->blueTeam = new Team();
    $this->player1 = new Player('Bensen');
    $this->player2 = new Player('Kicksen');
    $this->startMatch();
}    

public function fightCreeps()
{   
    $creeps = 0;
    $gold = 0;
    for ($this->wave; $this->wave > 0; $this->wave--) {
        if ($this->player1->lasthitting >= rand(1, 100)) {
            $creeps++;
            $gold += 40; 
            $this->player1->creeps++;
            $this->player1->gold += 40;
            $this->player1->totalGold += 40;
            $this->player1->experience += 40;
            $this->player1->health -= rand(5, 10);
        }
    }
    return "<span style=\"color: red;\">{$this->player1->name}</span> has slain {$creeps} Creeps. +{$gold} Gold<br>";
}

2 个答案:

答案 0 :(得分:1)

public $players = [];

public function __construct()
{
    $this->redTeam = new Team();
    $this->blueTeam = new Team();
    $this->addPlayer('Bensen');
    $this->addPlayer('Kicksen');
    $this->startMatch();
}


public function addPlayer($name)
{
    $this->players[] = new Player($name);
}


public function startMatch()
{
    // do other starting match related stuff

    foreach ($this->players as $player) {
        $this->fightCreeps($player);
    }

}

public function fightCreeps(Player $player)
{
    // do the fighting stuff
}    

答案 1 :(得分:1)

我认为您需要考虑您的应用架构。从OOP的角度来看,Match不会fightCreeps()Player会发生战斗。

我会以不同的方式定义关系(参见https://laravel.com/docs/5.1/eloquent-relationships)。例如,如果您知道只有两支球队参加比赛,那么在比赛模型上,您可以定义:

use App\Team;

class Match extends Model {

    protected static function boot()
    {
        parent::boot();

        // when a match is created, auto-create teams
        static::created(function ($match) {
            $match->redTeam()->create([]);
            $match->blueTeam()->create([]);
        });
    }

    public function startMatch()
    {
        //fight
    }

    public function redTeam()
    {
        return $this->hasOne(Team::class);
    }

    public function blueTeam()
    {
        return $this->hasOne(Team::class);
    }
}

团队模型:

use App\Match;
use App\Player;

class Team extends Model {

    protected static function boot()
    {
        parent::boot();

        // when a team is created auto-add players
        static::created(function ($team) {
            $team->players()->create(['name' => 'Bensen']);
            $team->players()->create(['name' => 'Kicksen']);
        });
    }

    public function players()
    {
        return $this->hasMany(Player::class);
    }

    public function match()
    {
        return $this->belongsTo(Match::class);
    }

}

玩家模型:

use App\Team;

class Player extends Model {

    public function team()
    {
        return $this->belongsTo(Team::class);
    }

    public function fightCreeps()
    {
        // fight!
    }
}

然后,您可以执行以下操作:

foreach ($this->redTeam->players as $player) {
    $player->fightCreeps();
}

你当然可以改变你创建球队和球员的方式,但我认为这些是你想要的一般关系。

在Laravel关系中,多个关系会自动返回为Collectionhttps://laravel.com/docs/5.1/collections),这是数组的更高版本。如果需要,您可以自己手动创建一个集合,然后循环遍历它,如下所示:

$players = collect([$player1, $player2]);

foreach ($players as $player) {
    // do something
}