Skip to content

Commit

Permalink
Implement State component and systems
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
whoshuu committed Feb 17, 2015
1 parent cd807ba commit cca45cf
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
4 changes: 4 additions & 0 deletions artbox/src/main/java/com/whoshuu/artbox/WorldView.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import com.whoshuu.artbox.R;
import com.whoshuu.artbox.artemis.EntitySystem;
import com.whoshuu.artbox.system.AnimationSystem;
import com.whoshuu.artbox.system.AnimationStateSystem;
import com.whoshuu.artbox.system.BodyPositionSystem;
import com.whoshuu.artbox.system.DebugBodyRenderSystem;
import com.whoshuu.artbox.system.SpriteRenderSystem;
import com.whoshuu.artbox.system.SpriteStateSystem;
import com.whoshuu.artbox.system.SystemType;
import com.whoshuu.artbox.system.TouchUpdateSystem;

Expand Down Expand Up @@ -42,6 +44,8 @@ public WorldView(Context context, AttributeSet attrs) {
setFocusable(true);

addSystem(new BodyPositionSystem(), SystemType.BASE_LOGIC);
addSystem(new AnimationStateSystem(), SystemType.BASE_LOGIC);
addSystem(new SpriteStateSystem(), SystemType.BASE_LOGIC);
addSystem(new AnimationSystem(), SystemType.BASE_LOGIC);
addSystem(new SpriteRenderSystem(), SystemType.BASE_RENDER);
addSystem(new DebugBodyRenderSystem(), SystemType.BASE_RENDER);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.whoshuu.artbox.component;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.whoshuu.artbox.artemis.utils.JSONComponent;

public class StateComponent extends JSONComponent {

public StateComponent() {
states = new ArrayList<String>();
}

public String getState() {
return current;
}

public void setState(String state) {
if (states.contains(state)) {
current = state;
}
}

@Override
public void fromJSON(JSONObject json, float x, float y, float angle) throws JSONException {
/*
* "type": "com.whoshuu.artbox.component.StateComponent",
* "states": [
* "name_of_first_state",
* "name_of_second_state",
* ...
* "name_of_last_state"
* ]
*
* The first state is assumed to be the first activated state.
*/
JSONArray jsonStates = json.getJSONArray("states");
for (int i = 0; i < jsonStates.length(); i++) {
String state = jsonStates.getString(i);
states.add(state);
if (i == 0) {
setState(state);
}
}
}

private ArrayList<String> states;
private String current;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.whoshuu.artbox.system;

import com.whoshuu.artbox.artemis.ComponentMapper;
import com.whoshuu.artbox.artemis.Entity;
import com.whoshuu.artbox.artemis.EntitySystem;
import com.whoshuu.artbox.component.AnimationComponent;
import com.whoshuu.artbox.component.StateComponent;

public class AnimationStateSystem extends EntitySystem {

@SuppressWarnings("unchecked")
public AnimationStateSystem() {
super(AnimationComponent.class, StateComponent.class);
}

@Override
protected void initialize() {
this.animations = new ComponentMapper<AnimationComponent>(AnimationComponent.class,
this.world);
this.states = new ComponentMapper<StateComponent>(StateComponent.class, this.world);
super.initialize();
}

@Override
protected void processEntity(Entity entity) {
StateComponent stateComponent = states.get(entity);
AnimationComponent animation = animations.get(entity);

if (!animation.getState().equals(stateComponent.getState())) {
animation.setState(stateComponent.getState());
}
}

@Override
protected boolean checkProcessing() {
return true;
}

private ComponentMapper<AnimationComponent> animations;
private ComponentMapper<StateComponent> states;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.whoshuu.artbox.system;

import com.whoshuu.artbox.artemis.ComponentMapper;
import com.whoshuu.artbox.artemis.Entity;
import com.whoshuu.artbox.artemis.EntitySystem;
import com.whoshuu.artbox.component.SpriteComponent;
import com.whoshuu.artbox.component.StateComponent;

public class SpriteStateSystem extends EntitySystem {

@SuppressWarnings("unchecked")
public SpriteStateSystem() {
super(SpriteComponent.class, StateComponent.class);
}

@Override
protected void initialize() {
this.sprites = new ComponentMapper<SpriteComponent>(SpriteComponent.class, this.world);
this.states = new ComponentMapper<StateComponent>(StateComponent.class, this.world);
super.initialize();
}

@Override
protected void processEntity(Entity entity) {
StateComponent stateComponent = states.get(entity);
SpriteComponent sprite = sprites.get(entity);

if (!sprite.getState().equals(stateComponent.getState())) {
sprite.setState(stateComponent.getState());
}
}

@Override
protected boolean checkProcessing() {
return true;
}

private ComponentMapper<SpriteComponent> sprites;
private ComponentMapper<StateComponent> states;
}

0 comments on commit cca45cf

Please sign in to comment.