Skip to content

Commit

Permalink
feat(JOML): migrate FactionEnemiesSystem (#132)
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias Nett <[email protected]>
  • Loading branch information
pollend and skaldarnar authored Jan 21, 2021
1 parent 4815819 commit fabb1e8
Showing 1 changed file with 18 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
/*
* Copyright 2018 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.metalrenegades.ai.system;

import com.google.common.collect.Lists;
import org.joml.Vector3f;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.systems.BaseComponentSystem;
import org.terasology.entitySystem.systems.RegisterMode;
import org.terasology.entitySystem.systems.RegisterSystem;
import org.terasology.entitySystem.systems.UpdateSubscriberSystem;
import org.terasology.logic.location.LocationComponent;
import org.terasology.math.geom.Vector3f;
import org.terasology.metalrenegades.ai.component.CitizenComponent;
import org.terasology.metalrenegades.ai.component.FactionAlignmentComponent;
import org.terasology.metalrenegades.ai.component.NearbyCitizenEnemiesComponent;
Expand All @@ -45,9 +32,9 @@ public class FactionEnemiesSystem extends BaseComponentSystem implements UpdateS

@Override
public void update(float delta) {
counter+=delta;
counter += delta;

if(counter < ENEMY_CHECK_DELAY) {
if (counter < ENEMY_CHECK_DELAY) {
return;
}

Expand All @@ -59,19 +46,19 @@ public void update(float delta) {
}

/**
* Checks for faction enemies nearby a particular citizen. Enemies are defined as follows:
* {@link org.terasology.metalrenegades.ai.system.FactionAlignmentSystem.Alignment#GOOD} citizens are enemies of
* {@link org.terasology.metalrenegades.ai.system.FactionAlignmentSystem.Alignment#BAD} citizens, and vice-versa,
* while {@link org.terasology.metalrenegades.ai.system.FactionAlignmentSystem.Alignment#NEUTRAL} are enemies with
* no other citizens.
*
* Checks for faction enemies nearby a particular citizen. Enemies are defined as follows: {@link
* org.terasology.metalrenegades.ai.system.FactionAlignmentSystem.Alignment#GOOD} citizens are enemies of {@link
* org.terasology.metalrenegades.ai.system.FactionAlignmentSystem.Alignment#BAD} citizens, and vice-versa, while
* {@link org.terasology.metalrenegades.ai.system.FactionAlignmentSystem.Alignment#NEUTRAL} are enemies with no
* other citizens.
* <p>
* Results are stored in {@link NearbyCitizenEnemiesComponent}.
*
* @param citizen The citizen to check enemies for.
*/
private void checkForEnemies(EntityRef citizen) {
if (!citizen.hasComponent(NearbyCitizenEnemiesComponent.class)
|| !citizen.hasComponent(FactionAlignmentComponent.class)) {
|| !citizen.hasComponent(FactionAlignmentComponent.class)) {
return;
}

Expand All @@ -82,31 +69,31 @@ private void checkForEnemies(EntityRef citizen) {
enemiesComponent.closestEnemy = EntityRef.NULL;

float minDistance = Float.MAX_VALUE;
Vector3f actorPosition = citizen.getComponent(LocationComponent.class).getWorldPosition();
Vector3f actorPosition = citizen.getComponent(LocationComponent.class).getWorldPosition(new Vector3f());

for (EntityRef otherCitizen : entityManager.getEntitiesWith(CitizenComponent.class)) {
if (!otherCitizen.hasComponent(FactionAlignmentComponent.class)
|| otherCitizen.equals(citizen)) {
|| otherCitizen.equals(citizen)) {
continue;
}

FactionAlignmentComponent otherAlignmentComponent = otherCitizen.getComponent(FactionAlignmentComponent.class);
if (otherAlignmentComponent.alignment.equals(alignmentComponent.alignment) // continue if alignments are
|| otherAlignmentComponent.alignment.equals(FactionAlignmentSystem.Alignment.NEUTRAL) // the same, or either alignment
|| alignmentComponent.alignment.equals(FactionAlignmentSystem.Alignment.NEUTRAL)) { // is neutral.
|| otherAlignmentComponent.alignment.equals(FactionAlignmentSystem.Alignment.NEUTRAL) // the same, or either alignment
|| alignmentComponent.alignment.equals(FactionAlignmentSystem.Alignment.NEUTRAL)) { // is neutral.
continue;
}

LocationComponent otherLocationComponent = otherCitizen.getComponent(LocationComponent.class);
float distanceApart = otherLocationComponent.getWorldPosition().distanceSquared(actorPosition);
float distanceApart = otherLocationComponent.getWorldPosition(new Vector3f()).distanceSquared(actorPosition);

if (distanceApart > enemiesComponent.searchRadius * enemiesComponent.searchRadius) {
continue;
}

if (distanceApart < minDistance) {
enemiesComponent.closestEnemy = otherCitizen;
minDistance = otherLocationComponent.getWorldPosition().distanceSquared(actorPosition);
minDistance = distanceApart;
}

enemiesComponent.enemiesWithinRange.add(otherCitizen);
Expand Down

0 comments on commit fabb1e8

Please sign in to comment.