mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-07-05 15:58:38 +07:00
Added support for selectable entities
Added support for selectable entities (BoxComponent) Added entity Flags class Displays active flags for entities in debug mode ObjectSystem changed to IteratingSystem setting SELECTABLE flag per mode Added SelectableSystem to set SELECTABLE using primary Gdx.input pointer
This commit is contained in:
@ -10,6 +10,7 @@ import com.riiablo.codec.excel.MonStats;
|
||||
import com.riiablo.codec.excel.MonStats2;
|
||||
import com.riiablo.codec.excel.Objects;
|
||||
import com.riiablo.engine.component.AnimationComponent;
|
||||
import com.riiablo.engine.component.BoxComponent;
|
||||
import com.riiablo.engine.component.ClassnameComponent;
|
||||
import com.riiablo.engine.component.CofComponent;
|
||||
import com.riiablo.engine.component.IdComponent;
|
||||
@ -114,6 +115,8 @@ public class Engine extends PooledEngine {
|
||||
|
||||
AnimationComponent animationComponent = createComponent(AnimationComponent.class);
|
||||
|
||||
BoxComponent boxComponent = createComponent(BoxComponent.class);
|
||||
|
||||
PositionComponent positionComponent = createComponent(PositionComponent.class);
|
||||
|
||||
ObjectComponent objectComponent = createComponent(ObjectComponent.class);
|
||||
@ -129,10 +132,14 @@ public class Engine extends PooledEngine {
|
||||
entity.add(typeComponent);
|
||||
entity.add(cofComponent);
|
||||
entity.add(animationComponent);
|
||||
entity.add(boxComponent);
|
||||
entity.add(positionComponent);
|
||||
entity.add(mapComponent);
|
||||
entity.add(objectComponent);
|
||||
entity.getComponent(ClassnameComponent.class).classname = base.Description;
|
||||
|
||||
// flags
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
@ -174,16 +181,22 @@ public class Engine extends PooledEngine {
|
||||
|
||||
AnimationComponent animationComponent = createComponent(AnimationComponent.class);
|
||||
|
||||
BoxComponent boxComponent = createComponent(BoxComponent.class);
|
||||
|
||||
PositionComponent positionComponent = createComponent(PositionComponent.class);
|
||||
|
||||
Entity entity = createEntity();
|
||||
entity.add(typeComponent);
|
||||
entity.add(cofComponent);
|
||||
entity.add(animationComponent);
|
||||
entity.add(boxComponent);
|
||||
entity.add(positionComponent);
|
||||
entity.add(mapComponent);
|
||||
entity.add(monsterComponent);
|
||||
entity.getComponent(ClassnameComponent.class).classname = monstats.Id;
|
||||
|
||||
if (monstats2.isSel) entity.flags |= Flags.SELECTABLE;
|
||||
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
23
core/src/com/riiablo/engine/Flags.java
Normal file
23
core/src/com/riiablo/engine/Flags.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.riiablo.engine;
|
||||
|
||||
public final class Flags {
|
||||
public static final int NONE = 0;
|
||||
public static final int DEBUG = 1 << 0;
|
||||
public static final int SELECTABLE = 1 << 1;
|
||||
public static final int SELECTED = 1 << 2;
|
||||
|
||||
public static String toString(int bits) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
if (bits == NONE) {
|
||||
builder.append("NONE");
|
||||
} else {
|
||||
if ((bits & DEBUG) == DEBUG) builder.append("DEBUG").append("|");
|
||||
if ((bits & SELECTABLE) == SELECTABLE) builder.append("SELECTABLE").append("|");
|
||||
if ((bits & SELECTED) == SELECTED) builder.append("SELECTED").append("|");
|
||||
if (builder.length() > 0) builder.setLength(builder.length() - 1);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private Flags() {}
|
||||
}
|
14
core/src/com/riiablo/engine/component/BoxComponent.java
Normal file
14
core/src/com/riiablo/engine/component/BoxComponent.java
Normal file
@ -0,0 +1,14 @@
|
||||
package com.riiablo.engine.component;
|
||||
|
||||
import com.badlogic.ashley.core.Component;
|
||||
import com.badlogic.gdx.utils.Pool;
|
||||
import com.riiablo.codec.util.BBox;
|
||||
|
||||
public class BoxComponent implements Component, Pool.Poolable {
|
||||
public BBox box;
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
box = null;
|
||||
}
|
||||
}
|
@ -13,6 +13,7 @@ import com.riiablo.codec.DC;
|
||||
import com.riiablo.engine.Dirty;
|
||||
import com.riiablo.engine.SystemPriority;
|
||||
import com.riiablo.engine.component.AnimationComponent;
|
||||
import com.riiablo.engine.component.BoxComponent;
|
||||
import com.riiablo.engine.component.CofComponent;
|
||||
|
||||
@DependsOn(CofLoaderSystem.class)
|
||||
@ -24,6 +25,7 @@ public class AnimationLoaderSystem extends IteratingSystem {
|
||||
|
||||
private final ComponentMapper<CofComponent> cofComponent = ComponentMapper.getFor(CofComponent.class);
|
||||
private final ComponentMapper<AnimationComponent> animComponent = ComponentMapper.getFor(AnimationComponent.class);
|
||||
private final ComponentMapper<BoxComponent> boxComponent = ComponentMapper.getFor(BoxComponent.class);
|
||||
|
||||
public AnimationLoaderSystem() {
|
||||
super(Family.all(CofComponent.class, AnimationComponent.class).get(), SystemPriority.AnimationLoaderSystem);
|
||||
@ -65,7 +67,11 @@ public class AnimationLoaderSystem extends IteratingSystem {
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) anim.updateBox();
|
||||
if (changed) {
|
||||
anim.updateBox();
|
||||
BoxComponent boxComponent = this.boxComponent.get(entity);
|
||||
if (boxComponent != null) boxComponent.box = anim.getBox();
|
||||
}
|
||||
if (DEBUG_LOAD) Gdx.app.debug(TAG, "load layers: " + Dirty.toString(cofComponent.load));
|
||||
}
|
||||
}
|
||||
|
@ -4,32 +4,45 @@ import com.badlogic.ashley.core.ComponentMapper;
|
||||
import com.badlogic.ashley.core.Engine;
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.ashley.core.EntityListener;
|
||||
import com.badlogic.ashley.core.EntitySystem;
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.ashley.systems.IteratingSystem;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.riiablo.codec.excel.Objects;
|
||||
import com.riiablo.engine.Flags;
|
||||
import com.riiablo.engine.component.CofComponent;
|
||||
import com.riiablo.engine.component.ObjectComponent;
|
||||
|
||||
public class ObjectSystem extends EntitySystem implements EntityListener {
|
||||
public class ObjectSystem extends IteratingSystem implements EntityListener {
|
||||
private static final String TAG = "ObjectSystem";
|
||||
private final Family family = Family.all(ObjectComponent.class).get();
|
||||
private final ComponentMapper<ObjectComponent> objectComponent = ComponentMapper.getFor(ObjectComponent.class);
|
||||
private final ComponentMapper<CofComponent> cofComponent = ComponentMapper.getFor(CofComponent.class);
|
||||
private final Family family;
|
||||
|
||||
public ObjectSystem() {
|
||||
super(Family.all(ObjectComponent.class).get());
|
||||
family = getFamily();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addedToEngine(Engine engine) {
|
||||
super.addedToEngine(engine);
|
||||
engine.addEntityListener(family, 0, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removedFromEngine(Engine engine) {
|
||||
super.removedFromEngine(engine);
|
||||
engine.removeEntityListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processEntity(Entity entity, float deltaTime) {
|
||||
entity.flags &= ~Flags.SELECTABLE;
|
||||
ObjectComponent objectComponent = this.objectComponent.get(entity);
|
||||
CofComponent cofComponent = this.cofComponent.get(entity);
|
||||
if (objectComponent.base.Selectable[cofComponent.mode]) entity.flags |= Flags.SELECTABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void entityAdded(Entity entity) {
|
||||
init(entity);
|
||||
@ -37,7 +50,6 @@ public class ObjectSystem extends EntitySystem implements EntityListener {
|
||||
|
||||
@Override
|
||||
public void entityRemoved(Entity entity) {
|
||||
|
||||
}
|
||||
|
||||
private void init(Entity entity) {
|
||||
@ -49,9 +61,7 @@ public class ObjectSystem extends EntitySystem implements EntityListener {
|
||||
case 1: case 2: case 3: case 4: case 5: case 6: case 7:
|
||||
break;
|
||||
case 8: // torch
|
||||
if (cofComponent != null) {
|
||||
cofComponent.mode = com.riiablo.engine.Engine.Object.MODE_ON;
|
||||
}
|
||||
if (cofComponent != null) cofComponent.mode = com.riiablo.engine.Engine.Object.MODE_ON;
|
||||
|
||||
// FIXME: Set random start frame?
|
||||
//int framesPerDir = animation.getNumFramesPerDir();
|
||||
|
50
core/src/com/riiablo/engine/system/SelectableSystem.java
Normal file
50
core/src/com/riiablo/engine/system/SelectableSystem.java
Normal file
@ -0,0 +1,50 @@
|
||||
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.gdx.Gdx;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.riiablo.camera.IsometricCamera;
|
||||
import com.riiablo.codec.util.BBox;
|
||||
import com.riiablo.engine.Flags;
|
||||
import com.riiablo.engine.component.BoxComponent;
|
||||
import com.riiablo.engine.component.PositionComponent;
|
||||
|
||||
public class SelectableSystem extends IteratingSystem {
|
||||
private final ComponentMapper<BoxComponent> boxComponent = ComponentMapper.getFor(BoxComponent.class);
|
||||
private final ComponentMapper<PositionComponent> positionComponent = ComponentMapper.getFor(PositionComponent.class);
|
||||
|
||||
private final IsometricCamera iso;
|
||||
private final Vector2 coords = new Vector2();
|
||||
private final Vector2 tmpVec2 = new Vector2();
|
||||
|
||||
public SelectableSystem(IsometricCamera iso) {
|
||||
super(Family.all(BoxComponent.class).get());
|
||||
this.iso = iso;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float delta) {
|
||||
coords.set(Gdx.input.getX(), Gdx.input.getY());
|
||||
iso.unproject(coords);
|
||||
super.update(delta);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processEntity(Entity entity, float delta) {
|
||||
entity.flags &= ~Flags.SELECTED;
|
||||
if ((entity.flags & Flags.SELECTABLE) != Flags.SELECTABLE) return;
|
||||
BBox box = boxComponent.get(entity).box;
|
||||
if (box == null) return;
|
||||
Vector2 position = positionComponent.get(entity).position;
|
||||
iso.toScreen(tmpVec2.set(position));
|
||||
float x = tmpVec2.x + box.xMin;
|
||||
float y = tmpVec2.y - box.yMax;
|
||||
if (x <= coords.x && coords.x <= x + box.width
|
||||
&& y <= coords.y && coords.y <= y + box.height) {
|
||||
entity.flags |= Flags.SELECTED;
|
||||
}
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@ import com.riiablo.Riiablo;
|
||||
import com.riiablo.camera.IsometricCamera;
|
||||
import com.riiablo.codec.Animation;
|
||||
import com.riiablo.engine.Dirty;
|
||||
import com.riiablo.engine.Flags;
|
||||
import com.riiablo.engine.component.AnimationComponent;
|
||||
import com.riiablo.engine.component.ClassnameComponent;
|
||||
import com.riiablo.engine.component.CofComponent;
|
||||
@ -1146,6 +1147,7 @@ public class RenderSystem extends EntitySystem {
|
||||
Vector2 tmp = iso.agg(tmpVec2.set(position)).toTile().toScreen().ret();
|
||||
builder.setLength(0);
|
||||
builder.append(classnameComponent.get(entity).classname).append('\n');
|
||||
builder.append(Flags.toString(entity.flags)).append('\n');
|
||||
CofComponent cofComponent = this.cofComponent.get(entity);
|
||||
if (cofComponent != null) {
|
||||
TypeComponent.Type type = typeComponent.get(entity).type;
|
||||
|
Reference in New Issue
Block a user