Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Population Component (Specific to MR) #77

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,25 @@
*/
package org.terasology.metalrenegades.ai.event;

import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.event.Event;
import org.terasology.protobuf.EntityData;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import


/**
* Fired when a new citizen character is spawned by {@link org.terasology.metalrenegades.ai.system.CitizenSpawnSystem}.
*/
public class CitizenSpawnedEvent implements Event {


// Stores the settlement to which the Citizen belongs to
private EntityRef settlement;

public CitizenSpawnedEvent(EntityRef settlement) {
this.settlement = settlement;
}

public EntityRef getSettlement() {
return this.settlement;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.terasology.registry.CoreRegistry;
import org.terasology.metalrenegades.minimap.events.AddCharacterToOverlayEvent;
import org.terasology.registry.In;

import java.util.ArrayList;

/**
Expand Down Expand Up @@ -108,9 +109,11 @@ public void update(float delta) {
*/
private EntityRef spawnCitizen(EntityRef homeEntity) {
SettlementRefComponent settlementRefComponent = homeEntity.getComponent(SettlementRefComponent.class);
FactionAlignmentComponent settlementAlignmentComponent = settlementRefComponent.settlement.getComponent(FactionAlignmentComponent.class);
FactionAlignmentComponent settlementAlignmentComponent =
settlementRefComponent.settlement.getComponent(FactionAlignmentComponent.class);

EntityBuilder entityBuilder = entityManager.newBuilder(citizenAlignmentSystem.getPrefab(settlementAlignmentComponent.alignment));
EntityBuilder entityBuilder =
entityManager.newBuilder(citizenAlignmentSystem.getPrefab(settlementAlignmentComponent.alignment));

LocationComponent homeLocationComponent = homeEntity.getComponent(LocationComponent.class);
LocationComponent citizenLocationComponent = entityBuilder.getComponent(LocationComponent.class);
Expand Down Expand Up @@ -139,7 +142,7 @@ private EntityRef spawnCitizen(EntityRef homeEntity) {
setupStartInventory(entityRef);
}

entityRef.send(new CitizenSpawnedEvent());
entityRef.send(new CitizenSpawnedEvent(settlementRefComponent.settlement));

return entityRef;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.metalrenegades.population.component;

import org.terasology.entitySystem.Component;

/**
* Track total population information of good, bad, and neutral citizens.
*
* The entity this component belongs to defines the scope of the tracked population.
* For instance, it may be attached to a world entity for the overall population or a settlement entity for the population of that settlement.
*
* The population is usually tracked by the {@link PopulationSystem}.
*/
public class FactionDistributionComponent implements Component {
public int goodCitizens;
public int badCitizens;
public int neutralCitizens;

public FactionDistributionComponent(){
this.badCitizens = 0;
this.goodCitizens = 0;
this.neutralCitizens = 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.metalrenegades.population.system;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.dynamicCities.buildings.components.SettlementRefComponent;
import org.terasology.dynamicCities.settlements.events.SettlementRegisterEvent;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.event.ReceiveEvent;
import org.terasology.entitySystem.systems.BaseComponentSystem;
import org.terasology.entitySystem.systems.RegisterMode;
import org.terasology.entitySystem.systems.RegisterSystem;
import org.terasology.metalrenegades.ai.component.FactionAlignmentComponent;
import org.terasology.metalrenegades.ai.component.HomeComponent;
import org.terasology.metalrenegades.ai.event.CitizenSpawnedEvent;
import org.terasology.metalrenegades.population.component.FactionDistributionComponent;

@RegisterSystem(value = RegisterMode.AUTHORITY)
public class PopulationSystem extends BaseComponentSystem {


Logger logger = LoggerFactory.getLogger(PopulationSystem.class);

@ReceiveEvent
public void onSettlementRegisterEvent(SettlementRegisterEvent buildingEntitySpawnedEvent, EntityRef entityRef) {
entityRef.addComponent(new FactionDistributionComponent());
}

@ReceiveEvent
public void citizenSpawned(CitizenSpawnedEvent event, EntityRef citizen,
FactionAlignmentComponent factionAlignmentComponent) {

EntityRef settlementEntity = event.getSettlement();

FactionDistributionComponent populationComponent =
settlementEntity.getComponent(FactionDistributionComponent.class);


switch (factionAlignmentComponent.alignment) {
case NEUTRAL:
populationComponent.neutralCitizens++;
break;
case GOOD:
populationComponent.goodCitizens++;
break;
case BAD:
populationComponent.badCitizens++;
break;
default:
logger.error("Invalid Faction Alignment");
}

settlementEntity.saveComponent(populationComponent);
}


}