patternphpMinor
Checking game objects versus each other to determine targets, comparing arrays
Viewed 0 times
arrayseachobjectsversuscomparingcheckinggamedeterminetargetsother
Problem
I have a flight class that looks like this:
If two opposing flights are close to each other, I initiate a dogfight. I determine if they are dogfighting like this:
Basicly, I compare all ships versus all ships (aka flights), compare their distance to each other. If it's below a threshold, I push the opposing flights ID into the flights->dogfights array.
I check if the dogfights are new, because they might have been transferred from the DB and alas be initiated a turn ago. So, each flight's
class Flight {
public $id; // unique id
public $structures = array(); // each "structure" indicates one fighter class assembled in this flight class
public $dogfights = array() if the flight is dogfighting, this array holds the ID of each opposing flight we are dogfighting
}
class Fighter { // this is whats inside $Flight->structures
public $id
public $parentid
}If two opposing flights are close to each other, I initiate a dogfight. I determine if they are dogfighting like this:
public function initiateDogfights(){
$dogfights = array();
for ($i = 0; $i ships)-1; $i++){
for ($j = $i+1; $j ships); $j++){
$dist = Math::getDist2($this->ships[$i]->getCurrentPosition(), $this->ships[$j]->getCurrentPosition());
if ($dist ships[$i]->size / 2 + $this->ships[$j]->size / 2){
$new = true;
for ($k = 0; $k ships[$i]->dogfights); $k++){
if ($this->ships[$i]->dogfights[$k] == $this->ships[$j]->id){
$new = false;
}
}
if ($new){
$this->ships[$i]->dogfights[] = $this->ships[$j]->id;
$this->ships[$j]->dogfights[] = $this->ships[$i]->id;
$dogfights[] = array(0 => $this->ships[$i]->id, 1 => $this->ships[$j]->id);
}
}
}
}
if (sizeof($dogfights)){
DBManager::app()->insertDogfights($this->gameid, $this->turn, $dogfights);
}
}Basicly, I compare all ships versus all ships (aka flights), compare their distance to each other. If it's below a threshold, I push the opposing flights ID into the flights->dogfights array.
I check if the dogfights are new, because they might have been transferred from the DB and alas be initiated a turn ago. So, each flight's
$dogfights prop holds the IDs of all opposing flights this flight is tackling. Now, thisSolution
I agree with Mike Brant's comments that it would be better to have the complete code posted for a broad review but that likely won't happen- if you do want to do so then I advise you to do so in a new post, since editing your post might invalidate the advice below. Nonetheless i see a few idiomatic PHP aspects that could be improved.
The code could be simplified using PHP’s
this could be simplified using
and also the loop in
with
and instead of referring to
The array on this line in
doesn't need to have the indexes explicitly set:
The code could be simplified using PHP’s
foreach loops instead of for loops- e.g. for ($i = 0; $i ships)-1; $i++){
for ($j = $i+1; $j ships); $j++){this could be simplified using
foreach loops with the range() function:foreach (range(0, sizeof($this->ships)-2) as $i) {
foreach (range($i, sizeof($this->ships)-1) as $j) {and also the loop in
createFireOrders():for ($i = 0; $i structures); $i++){
if (!$this->structures[$i]->destroyed){with
foreach there is no need to do the bookkeeping on the counter variable:foreach ($this->structures as $i => $structure) {and instead of referring to
$this->structures[$i] just use $structure.The array on this line in
initiateDogfights():$dogfights[] = array(0 => $this->ships[$i]->id, 1 => $this->ships[$j]->id);doesn't need to have the indexes explicitly set:
$dogfights[] = array($this->ships[$i]->id, $this->ships[$j]->id);Code Snippets
for ($i = 0; $i < sizeof($this->ships)-1; $i++){
for ($j = $i+1; $j < sizeof($this->ships); $j++){foreach (range(0, sizeof($this->ships)-2) as $i) {
foreach (range($i, sizeof($this->ships)-1) as $j) {for ($i = 0; $i < sizeof($this->structures); $i++){
if (!$this->structures[$i]->destroyed){foreach ($this->structures as $i => $structure) {$dogfights[] = array(0 => $this->ships[$i]->id, 1 => $this->ships[$j]->id);Context
StackExchange Code Review Q#155541, answer score: 4
Revisions (0)
No revisions yet.