Add player/model components + input/render systems
This commit is contained in:
parent
e38b538a1e
commit
6fb664adaf
10
core/src/com/me/asteroids/components/ModelComponent.java
Normal file
10
core/src/com/me/asteroids/components/ModelComponent.java
Normal file
@ -0,0 +1,10 @@
|
||||
package com.me.asteroids.components;
|
||||
|
||||
import com.badlogic.gdx.math.Polygon;
|
||||
import com.me.common.ecs.Component;
|
||||
|
||||
public class ModelComponent extends Component {
|
||||
|
||||
public Polygon model;
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.me.asteroids.components;
|
||||
|
||||
import com.me.common.ecs.Component;
|
||||
|
||||
public class PlayerComponent extends Component {
|
||||
// TODO: implement engine feature for tagging entities (as player, for e.g.)
|
||||
}
|
48
core/src/com/me/asteroids/systems/ModelRenderSystem.java
Normal file
48
core/src/com/me/asteroids/systems/ModelRenderSystem.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.me.asteroids.systems;
|
||||
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||
import com.badlogic.gdx.math.Polygon;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.me.asteroids.Graphics;
|
||||
import com.me.asteroids.components.ModelComponent;
|
||||
import com.me.asteroids.components.PositionComponent;
|
||||
import com.me.common.ecs.Entity;
|
||||
import com.me.common.ecs.EntitySystem;
|
||||
|
||||
public class ModelRenderSystem extends EntitySystem {
|
||||
|
||||
private ShapeRenderer renderer;
|
||||
|
||||
public ModelRenderSystem(Graphics graphics) {
|
||||
super(ModelComponent.class, PositionComponent.class);
|
||||
this.renderer = graphics.getShapeRenderer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preProcessing() {
|
||||
renderer.setColor(Color.WHITE);
|
||||
renderer.begin(ShapeRenderer.ShapeType.Line);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processEntity(Entity entity, float dt) {
|
||||
ModelComponent modelComponent = entity.getComponent(ModelComponent.class);
|
||||
PositionComponent positionComponent = entity.getComponent(PositionComponent.class);
|
||||
|
||||
Polygon model = modelComponent.model;
|
||||
Vector2 position = positionComponent.position;
|
||||
float rotation = positionComponent.rotation;
|
||||
|
||||
model.setPosition(position.x, position.y);
|
||||
model.setRotation(rotation - 90);
|
||||
|
||||
renderer.polygon(model.getTransformedVertices());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessing() {
|
||||
renderer.end();
|
||||
}
|
||||
|
||||
}
|
55
core/src/com/me/asteroids/systems/PlayerInputSystem.java
Normal file
55
core/src/com/me/asteroids/systems/PlayerInputSystem.java
Normal file
@ -0,0 +1,55 @@
|
||||
package com.me.asteroids.systems;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.me.asteroids.Utils;
|
||||
import com.me.asteroids.components.AccelerationComponent;
|
||||
import com.me.asteroids.components.PlayerComponent;
|
||||
import com.me.asteroids.components.PositionComponent;
|
||||
import com.me.asteroids.components.VelocityComponent;
|
||||
import com.me.common.ecs.Entity;
|
||||
import com.me.common.ecs.EntitySystem;
|
||||
|
||||
public class PlayerInputSystem extends EntitySystem {
|
||||
|
||||
public Vector2 tmp = new Vector2(0, 1);
|
||||
|
||||
public PlayerInputSystem() {
|
||||
super(
|
||||
PositionComponent.class,
|
||||
VelocityComponent.class,
|
||||
AccelerationComponent.class,
|
||||
PlayerComponent.class
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processEntity(Entity entity, float dt) {
|
||||
PositionComponent positionComponent = entity.getComponent(PositionComponent.class);
|
||||
VelocityComponent velocityComponent = entity.getComponent(VelocityComponent.class);
|
||||
AccelerationComponent accelComponent = entity.getComponent(AccelerationComponent.class);
|
||||
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.D)) {
|
||||
positionComponent.rotation = Utils.rotate(positionComponent.rotation, -5);
|
||||
}
|
||||
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.A)) {
|
||||
positionComponent.rotation = Utils.rotate(positionComponent.rotation, 5);
|
||||
}
|
||||
|
||||
Vector2 acceleration = accelComponent.acceleration;
|
||||
Vector2 velocity = velocityComponent.velocity;
|
||||
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.W)) {
|
||||
acceleration.set(Utils.setUnitVectorAngle(tmp, positionComponent.rotation).scl(500));
|
||||
} else {
|
||||
if (!velocity.isZero(1f)) {
|
||||
acceleration.set(Utils.setUnitVectorAngleRad(tmp, velocity.angleRad()).scl(-100));
|
||||
} else {
|
||||
acceleration.set(0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user