Miner Worker Unit - UQdeco2800/2022-studio-3 GitHub Wiki
A miner is a unit that is responsible for taking stone and metal attributes from a resource.
Miner is created by MinerFactory.java which uses WorkerFactory.java to create the worker. So, Miner has the components from WorkerFactory as well. Here is how to create a Miner entity:
Entity miner = MinerFactory.createMiner();
By default, Miner has 1 distinct components which is MinerComponent
. This component is added to Miner in order to distinct the entity's type from other workers such as Forager. Here is how to add MinerComponent to an entity:
Entity miner = WorkerFactory.createWorker().addComponent(new MinerComponent());
To check whether an entity is a Miner or not, get the MinerComponent of an entity and perform a check like the code below:
MinerComponent miner_comp = miner.getComponent(MinerComponent.class);
if((miner_comp != null) && (miner_comp.getIsMiner())){
// Code
}
Miner has a component called WorkerInventorComponent that holds all the amount of resources such as stone, metal, and wood that have been taken by the miner. Resource has a component called ResourceStatsComponent that holds all the amount of resources. The process of adding the amount of stone/metal/wood in Miner's inventory and reducing the amount of stone/metal/wood in a resource is mainly implemented in ResourceCollectComponent.
When a miner is colliding with a resource, onCollisionStart()
in ResourceCollectComponent will be triggered. In onCollisionStart()
, the logic of adding amount of resources in WorkerInventoryComponent and reducing amount of resources in ResourceStatsComponent had been implemented in collectStone()
, collectMetal()
, and collectWood()
. For Miner, collectStone()
and collectMetal()
will be executed.
Below is the code that presents the logic on how the resource collection works:
private void onCollisionStart(Fixture me, Fixture other){
Entity collector = ((BodyUserData) me.getBody().getUserData()).entity;
Entity resource = ((BodyUserData) me.getBody().getUserData()).entity;
ResourceStatsComponent resource_stats = resource.getComponent(ResourceStasComponent.class);
MinerComponent miner_comp = collector.getComponent(MinerComponent.class);
if((miner_comp != null) && (miner_comp.getIsMiner())){
collecStone(resource_stats);
collectMetal(resource_stats);
// Code to add the number of stone and metal in the miner's inventory
}
}
private void collectStone(resource_stats){
// Code to reduce the number of stone in resource_stats
}
private void collectStone(resource_stats){
// Code to reduce the number of metal in resource_stats
}
Miner has a component called EnemyDetectionComponent that has a method onCollisionStart()
. When a collision happen, it will check the component of others by obtaining TouchAttackComponent and ComboStatsComponent. Since enemy might have those components, when a collsion happened and those components are obtained, then the worker will retreat to the base by calling getBase()
and returnToBase()
.
private void onCollisionStart(Fixture me, Fixture other) {
Entity isEnemy = ((BodyUserData) other.getBody().getUserData()).entity;
TouchAttackComponent hasAttackComponent_ = isEnemy.getComponent(TouchAttackComponent.class);
CombatStatsComponent hasCombatStatsComponent_ = isEnemy.getComponent(CombatStatsComponent.class);
if(hasAttackComponent_ != null || hasCombatStatsComponent_ != null){
returnToBase();
return;
}
}
public Entity getBase() {
Entity base = null;
for (Entity entity : ServiceLocator.getEntityService().getEntities()) {
if (entity.getComponent(BaseComponent.class) != null) {
base = entity;
}
}
return base;
}
public void returnToBase() {
Entity base = this.getBase();
if (base != null) {
entity.getEvents().trigger("workerWalk", this.getBase().getCenterPosition());
}
}
For Miner, Its unit test might be identical to testing components. So, unit tests in Miner are performed to test the logic
of Miner that might be reflected in programming Miner in the game. There are 5 unit tests performed for Miner:
-
minerComponentDetected()
: Check whether MinerComponent could be added to Miner. MinerComponent is used by ResourceCollectComponent to perform verification for callingcollectStone()
andcollectMetal
. -
minerCollectStatsDetected()
: Check whether CollectStatsComponent is working properly. -
minerResourceStatsDetected()
: Check whether ResourceStatsComponent is working properly when Miner is colliding with a resource. -
minerAnimationControllerDetected()
: Check whether MinerAnimationController is working properly by checking Miner'sanimateIdle
(when miner is idling) andanimateMove
(when miner is moving). -
inventoryCouldBeAttachedToMiner()
: Check whether WorkerInventoryComponent works properly when added to the Miner.