Created
November 19, 2016 16:19
-
-
Save zacharycarter/6a15df1b2f95dd1ed37d01d5d034f95b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.carterza.component; | |
import com.artemis.Component; | |
import com.badlogic.gdx.graphics.g2d.Animation; | |
/** | |
* Created by zachcarter on 10/12/16. | |
*/ | |
public class AnimationComponent extends Component { | |
private Animation animation; | |
private String name; | |
private float age = 0; | |
private float speed = 1; | |
private float scale = 1; | |
public AnimationComponent() { | |
} | |
public AnimationComponent(final String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return this.name; | |
} | |
public float getAge() { | |
return this.age; | |
} | |
public void setAge(final float age) { | |
this.age = age; | |
} | |
public float getSpeed() { | |
return speed; | |
} | |
public void setSpeed(final float speed) { | |
this.speed = speed; | |
} | |
public float getScale() { | |
return scale; | |
} | |
public void setScale(float scale) { | |
this.scale = scale; | |
} | |
public void setAnimation(Animation animation) { | |
this.animation = animation; | |
} | |
public Animation getAnimation() { | |
return animation; | |
} | |
} |
This file contains hidden or 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
package com.carterza.system.animation; | |
import com.artemis.Aspect; | |
import com.artemis.ComponentMapper; | |
import com.artemis.annotations.Wire; | |
import com.artemis.systems.IteratingSystem; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.graphics.g2d.Animation; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.badlogic.gdx.graphics.g2d.TextureRegion; | |
import com.carterza.Constants; | |
import com.carterza.component.AnimationComponent; | |
import com.carterza.component.PositionComponent; | |
import com.carterza.system.asset.AssetSystem; | |
import com.carterza.system.camera.CameraSystem; | |
import com.carterza.system.lighting.MuzzleFlashSystem; | |
@Wire | |
public class AnimationRenderSystem extends IteratingSystem { | |
private ComponentMapper<AnimationComponent> animationComponentMapper; | |
private ComponentMapper<PositionComponent> positionComponentMapper; | |
private CameraSystem cameraSystem; | |
private AssetSystem assetSystem; | |
// private MuzzleFlashSystem muzzleFlashSystem; | |
private SpriteBatch batch; | |
private float age; | |
private float animationRunTime = 0; | |
public AnimationRenderSystem() { | |
super(Aspect.all(PositionComponent.class, AnimationComponent.class)); | |
batch = new SpriteBatch(); | |
} | |
@Override | |
protected void begin() { | |
age += world.delta; | |
batch.setProjectionMatrix(cameraSystem.camera.combined); | |
batch.begin(); | |
batch.setColor(1f, 1f, 1f, 1f); | |
} | |
@Override | |
protected void end() { | |
batch.end(); | |
} | |
@Override | |
protected void process(int entityId) { | |
final AnimationComponent animationComponent = animationComponentMapper.get(entityId); | |
final PositionComponent positionComponent = positionComponentMapper.get(entityId); | |
float animationAge = animationComponent.getAge(); | |
animationComponent.setAge(animationAge += world.delta * animationComponent.getSpeed()); | |
drawAnimation(animationComponent, positionComponent, animationComponent.getName()); | |
} | |
private void drawAnimation(final AnimationComponent animationComponent, final PositionComponent positionComponent, final String animationName) { | |
final Animation animation = assetSystem.get(animationName); | |
if(animation == null) return; | |
animationComponent.setAnimation(animation); | |
if(animation.getPlayMode().equals(Animation.PlayMode.NORMAL)) { | |
final TextureRegion frame = animation.getKeyFrame(animationRunTime, true); | |
switch (animationName) { | |
case "muzzle_flash": | |
/*if(muzzleFlashSystem.isDrawMuzzleFlash()) { | |
animationRunTime += Gdx.graphics.getDeltaTime(); | |
batch.draw(frame, | |
(int)positionComponent.getPosition().x, | |
(int)positionComponent.getPosition().y, | |
frame.getRegionWidth() * animationComponent.getScale(), | |
frame.getRegionHeight() * animationComponent.getScale()); | |
if(animation.isAnimationFinished(animationRunTime)) { | |
muzzleFlashSystem.setDrawMuzzleFlash(false); | |
animationComponent.setAnimation(null); | |
animationRunTime = 0; | |
} | |
}*/ | |
break; | |
case "bullet": | |
} | |
} else { | |
age = animationComponent.getAge(); | |
final TextureRegion frame = animation.getKeyFrame(age, true); | |
batch.draw(frame, | |
(int)positionComponent.getPosition().x, | |
(int)positionComponent.getPosition().y, | |
(frame.getRegionWidth() * animationComponent.getScale())/24, | |
(frame.getRegionHeight() * animationComponent.getScale())/24); | |
} | |
} | |
} |
This file contains hidden or 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
package com.carterza.system.logic; | |
import com.artemis.Aspect; | |
import com.artemis.BaseSystem; | |
import com.artemis.ComponentMapper; | |
import com.artemis.Entity; | |
import com.artemis.systems.IteratingSystem; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.physics.box2d.*; | |
import com.carterza.component.AnimationComponent; | |
import com.carterza.component.BulletComponent; | |
import com.carterza.component.PhysicsComponent; | |
import com.carterza.component.PositionComponent; | |
import com.carterza.entity.EntityFactory; | |
import com.carterza.event.WeaponFiredEvent; | |
import com.carterza.strategy.LogicRenderMarker; | |
import com.carterza.system.physics.PhysicsSystem; | |
import com.carterza.system.player.PlayerSystem; | |
import net.mostlyoriginal.api.component.graphics.Anim; | |
import net.mostlyoriginal.api.event.common.Subscribe; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* Created by zachcarter on 11/11/16. | |
*/ | |
public class BulletSystem extends IteratingSystem { | |
private PlayerSystem playerSystem; | |
private PhysicsSystem physicsSystem; | |
private ComponentMapper<AnimationComponent> animationComponentMapper; | |
private ComponentMapper<PositionComponent> positionComponentMapper; | |
private ComponentMapper<PhysicsComponent> physicsComponentMapper; | |
private List<Entity> bullets; | |
/** | |
* Creates a new EntityProcessingSystem. | |
* | |
* @param aspect the aspect to match entities | |
*/ | |
public BulletSystem() { | |
super(Aspect.all(BulletComponent.class)); | |
bullets = new ArrayList<>(); | |
} | |
@Subscribe | |
void handleEvent(WeaponFiredEvent event) { | |
Vector2 position = positionComponentMapper.get(playerSystem.getPlayer().getId()).getPosition(); | |
Entity bullet = EntityFactory.createBullet(world, position.x-1, position.y); | |
bullets.add(bullet); | |
PhysicsComponent physicsComponent = physicsComponentMapper.get(bullet.getId()); | |
PositionComponent positionComponent = positionComponentMapper.get(bullet.getId()); | |
BodyDef bodyDef = new BodyDef(); | |
bodyDef.type = BodyDef.BodyType.DynamicBody; | |
// Set our body to the same position as our sprite | |
bodyDef.position.set(positionComponent.getPosition().x-1, positionComponent.getPosition().y); | |
bodyDef.fixedRotation = true; | |
// Create a body in the world using our definition | |
Body body = physicsSystem.getPhysics().createBody(bodyDef); | |
physicsComponent.setBody(body); | |
body.setBullet(true); | |
CircleShape shape = new CircleShape(); | |
shape.setRadius(.1f); | |
FixtureDef fixtureDef = new FixtureDef(); | |
fixtureDef.shape = shape; | |
fixtureDef.density = .1f; | |
fixtureDef.friction = 0; | |
physicsComponent.setFixture(physicsComponent.getBody().createFixture(fixtureDef)); | |
// Shape is the only disposable of the lot, so get rid of it | |
shape.dispose(); | |
} | |
@Override | |
protected void process(int entityId) { | |
PhysicsComponent physicsComponent = physicsComponentMapper.get(entityId); | |
Body body = physicsComponent.getBody(); | |
body.setLinearVelocity(new Vector2(-20f, 0)); | |
} | |
@Override | |
protected void end() { | |
Iterator<Entity> bulletIterator = bullets.iterator(); | |
while(bulletIterator.hasNext()) { | |
Entity bullet = bulletIterator.next(); | |
PhysicsComponent physicsComponent = physicsComponentMapper.get(bullet.getId()); | |
Map<String, Boolean> userData = (Map<String, Boolean>) physicsComponent.getBody().getUserData(); | |
if(userData != null) { | |
if(userData.get("deleteFlag")) { | |
physicsSystem.getPhysics().destroyBody(physicsComponent.getBody()); | |
world.delete(bullet.getId()); | |
bulletIterator.remove(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment