Added AutoInteractSystem which will automatically hover and sort nearby entities for selection on mobile

This commit is contained in:
Collin Smith 2019-11-15 01:22:51 -08:00
parent 00947ba87d
commit d0e323e320
2 changed files with 94 additions and 0 deletions

View File

@ -24,6 +24,7 @@ import com.riiablo.engine.component.ClassnameComponent;
import com.riiablo.engine.component.CofComponent;
import com.riiablo.engine.component.DS1Component;
import com.riiablo.engine.component.IdComponent;
import com.riiablo.engine.component.InteractableComponent;
import com.riiablo.engine.component.LabelComponent;
import com.riiablo.engine.component.MapComponent;
import com.riiablo.engine.component.MonsterComponent;
@ -223,6 +224,12 @@ public class Engine extends PooledEngine {
labelComponent.offset.y = -base.NameOffset;
labelComponent.actor = createLabel(name);
InteractableComponent interactableComponent = null;
if (base.OperateRange > 0) {
interactableComponent = createComponent(InteractableComponent.class);
interactableComponent.range = base.OperateRange;
}
Entity entity = createEntity(base.Description);
entity.add(typeComponent);
if (draw) entity.add(cofComponent);
@ -233,6 +240,7 @@ public class Engine extends PooledEngine {
entity.add(ds1Component);
entity.add(objectComponent);
entity.add(labelComponent);
if (interactableComponent != null) entity.add(interactableComponent);
labelComponent.actor.setUserObject(entity);
@ -293,6 +301,13 @@ public class Engine extends PooledEngine {
labelComponent.offset.y = monstats2.pixHeight;
labelComponent.actor = createLabel(name);
InteractableComponent interactableComponent = null;
if (monstats.interact) {
interactableComponent = createComponent(InteractableComponent.class);
interactableComponent.range = monstats2.SizeX;
// FIXME: SizeX and SizeY appear to always be equal -- is this method sufficient?
}
Entity entity = createEntity(monstats.Id);
entity.add(typeComponent);
entity.add(cofComponent);
@ -303,6 +318,7 @@ public class Engine extends PooledEngine {
entity.add(ds1Component);
entity.add(monsterComponent);
entity.add(labelComponent);
if (interactableComponent != null) entity.add(interactableComponent);
labelComponent.actor.setUserObject(entity);
@ -377,6 +393,9 @@ public class Engine extends PooledEngine {
labelComponent.offset.set(box.xMin + box.width / 2, -box.yMax + box.height / 2);
labelComponent.actor = createLabel(name);
InteractableComponent interactableComponent = createComponent(InteractableComponent.class);
interactableComponent.range = 2f;
Entity entity = createEntity("warp");
entity.add(typeComponent);
entity.add(mapComponent);
@ -384,6 +403,7 @@ public class Engine extends PooledEngine {
entity.add(warpComponent);
entity.add(boxComponent);
entity.add(labelComponent);
entity.add(interactableComponent);
labelComponent.actor.setUserObject(entity);

View File

@ -0,0 +1,74 @@
package com.riiablo.engine.system;
import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.riiablo.engine.component.HoveredComponent;
import com.riiablo.engine.component.InteractableComponent;
import com.riiablo.engine.component.PositionComponent;
import com.riiablo.engine.component.SelectableComponent;
import com.riiablo.map.RenderSystem;
import java.util.Comparator;
public class AutoInteractSystem extends IteratingSystem implements Comparator<Entity> {
private final ComponentMapper<PositionComponent> positionComponent = ComponentMapper.getFor(PositionComponent.class);
private final ComponentMapper<InteractableComponent> interactableComponent = ComponentMapper.getFor(InteractableComponent.class);
private final ComponentMapper<HoveredComponent> hoveredComponent = ComponentMapper.getFor(HoveredComponent.class);
private final Vector2 position = new Vector2();
private final Array<Entity> sortedEntities = new Array<>(false, 16);
private final ImmutableArray<Entity> entities = new ImmutableArray<>(sortedEntities);
private RenderSystem renderer;
private float scalar;
public AutoInteractSystem(RenderSystem renderer, float scalar) {
super(Family.all(PositionComponent.class, SelectableComponent.class, InteractableComponent.class).get());
this.renderer = renderer;
this.scalar = scalar;
}
public ImmutableArray<Entity> getEntities() {
return entities;
}
@Override
public void update(float delta) {
Entity src = renderer.getSrc();
PositionComponent positionComponent = this.positionComponent.get(src);
position.set(positionComponent.position);
sortedEntities.clear();
super.update(delta);
sortedEntities.sort(this);
}
@Override
protected void processEntity(Entity entity, float delta) {
PositionComponent positionComponent = this.positionComponent.get(entity);
InteractableComponent interactableComponent = this.interactableComponent.get(entity);
setHovered(entity, position.dst(positionComponent.position) <= interactableComponent.range * scalar);
}
private void setHovered(Entity entity, boolean b) {
if (b) {
if (!hoveredComponent.has(entity)) entity.add(getEngine().createComponent(HoveredComponent.class));
sortedEntities.add(entity);
} else {
entity.remove(HoveredComponent.class);
}
}
@Override
public int compare(Entity e1, Entity e2) {
Vector2 p1 = positionComponent.get(e1).position;
Vector2 p2 = positionComponent.get(e2).position;
float d1 = position.dst2(p1);
float d2 = position.dst2(p2);
return Float.compare(d1, d2);
}
}