-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement State component and systems
Closes #10
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
artbox/src/main/java/com/whoshuu/artbox/component/StateComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
41 changes: 41 additions & 0 deletions
41
artbox/src/main/java/com/whoshuu/artbox/system/AnimationStateSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
40 changes: 40 additions & 0 deletions
40
artbox/src/main/java/com/whoshuu/artbox/system/SpriteStateSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |