Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.game.tank.system;
- 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.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.math.Matrix4;
- import com.badlogic.gdx.math.Quaternion;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.math.Vector3;
- import com.badlogic.gdx.physics.bullet.collision.btBoxShape;
- import com.badlogic.gdx.physics.bullet.collision.btBroadphasePair;
- import com.badlogic.gdx.physics.bullet.collision.btBroadphasePairArray;
- import com.badlogic.gdx.physics.bullet.collision.btBroadphaseProxy;
- import com.badlogic.gdx.physics.bullet.collision.btCollisionAlgorithm;
- import com.badlogic.gdx.physics.bullet.collision.btCollisionObject;
- import com.badlogic.gdx.physics.bullet.collision.btHashedOverlappingPairCache;
- import com.badlogic.gdx.physics.bullet.collision.btOverlappingPairCache;
- import com.badlogic.gdx.physics.bullet.collision.btPairCachingGhostObject;
- import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody;
- import com.badlogic.gdx.utils.Array;
- import com.game.tank.component.AnimationComponent;
- import com.game.tank.component.CharacterInputComponent;
- import com.game.tank.component.PhysicsComponent;
- /**
- *
- * @author Fian Manafe
- */
- public class CharacterInputSystem extends EntitySystem implements EntityListener {
- private Entity entity;
- private final ComponentMapper<PhysicsComponent> pcMapper;
- private final Vector3 vec0 = new Vector3();
- private final Vector3 vec1 = new Vector3();
- private final Vector3 vec3 = new Vector3();
- private final Quaternion qua0 = new Quaternion();
- private final Matrix4 mat0 = new Matrix4();
- private final int[] userDataPointer = new int[10];
- private final Vector2 playerDirection = new Vector2(0, 1);
- private final Vector2 tempDirection = new Vector2();
- private final Array<Entity> markedEntity = new Array<Entity>();
- private boolean fly;
- private PhysicsSystem physicsSystem;
- private btPairCachingGhostObject frustrum;
- private btPairCachingGhostObject gsensor;
- public CharacterInputSystem() {
- this.pcMapper = ComponentMapper.getFor(PhysicsComponent.class);
- }
- @Override
- public void addedToEngine(Engine engine) {
- physicsSystem = engine.getSystem(PhysicsSystem.class);
- engine.addEntityListener(Family.all(CharacterInputComponent.class).get(), this);
- frustrum = new btPairCachingGhostObject();
- frustrum.setCollisionShape(new btBoxShape(new Vector3(1, 1, 1)));
- frustrum.setCollisionFlags(btCollisionObject.CollisionFlags.CF_NO_CONTACT_RESPONSE | btCollisionObject.CollisionFlags.CF_CHARACTER_OBJECT);
- physicsSystem.collisionWorld.addCollisionObject(frustrum);
- // gsensor = new btPairCachingGhostObject();
- // gsensor.setCollisionShape(new btBoxShape(new Vector3(1, 0.1f, 1)));
- // gsensor.setCollisionFlags(btCollisionObject.CollisionFlags.CF_NO_CONTACT_RESPONSE);
- // physicsSystem.collisionWorld.addCollisionObject(gsensor, btBroadphaseProxy.CollisionFilterGroups.SensorTrigger, btBroadphaseProxy.CollisionFilterGroups.AllFilter);
- }
- @Override
- public void update(float deltaTime) {
- if (entity == null) {
- return;
- }
- tempDirection.set(playerDirection);
- PhysicsComponent pc = pcMapper.get(entity);
- AnimationComponent an = entity.getComponent(AnimationComponent.class);
- btRigidBody body = (btRigidBody) pc.body;
- if (!fly) {
- vec0.set(0, 0, 0);
- if (Gdx.input.isKeyPressed(Input.Keys.A)) {
- vec0.x--;
- vec0.z++;
- tempDirection.set(vec0.x, vec0.z);
- }
- if (Gdx.input.isKeyPressed(Input.Keys.W)) {
- vec0.z--;
- vec0.x--;
- tempDirection.set(vec0.x, vec0.z);
- }
- if (Gdx.input.isKeyPressed(Input.Keys.S)) {
- vec0.z++;
- vec0.x++;
- tempDirection.set(vec0.x, vec0.z);
- }
- if (Gdx.input.isKeyPressed(Input.Keys.D)) {
- vec0.x++;
- vec0.z--;
- tempDirection.set(vec0.x, vec0.z);
- }
- vec0.nor();
- if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
- vec0.y += 3;
- }
- body.setGravity(new Vector3(0, -98, 0));
- if (!vec0.isZero()) {
- body.setFriction(0);
- body.activate();
- body.setLinearVelocity(vec0.scl(10));
- if (!tempDirection.isZero() && (an.getCurrentAnimation() == null || !an.getCurrentAnimation().animation.id.equals("Armature|run"))) {
- an.animate("Armature|run", -1, 5);
- }
- if (vec0.y != 0) {
- an.animate("Armature|jump", -1, 4);
- }
- } else {
- body.setFriction(100);
- if (an.getCurrentAnimation() == null || !an.getCurrentAnimation().animation.id.equals("Armature|standby")) {
- an.animate("Armature|standby", -1, 1);
- }
- }
- }
- if (!tempDirection.isCollinear(playerDirection)) {
- playerDirection.set(tempDirection);
- body.getWorldTransform(mat0);
- body.activate();
- float angle = (float) Math.atan2(playerDirection.x, playerDirection.y);
- qua0.set(new Vector3(0, 1, 0), (float) Math.toDegrees(angle));
- mat0.getTranslation(vec1);
- mat0.set(vec1, qua0);
- body.setWorldTransform(mat0);
- }
- if (Gdx.input.isKeyPressed(Input.Keys.ENTER)) {
- body.getWorldTransform(mat0);
- mat0.getTranslation(vec1);
- vec3.set(playerDirection.x, 0, playerDirection.y).nor().scl(2);
- vec1.add(vec3);
- getEngine().getSystem(AK47System.class).shoot(deltaTime, vec1, vec3);
- }
- if (body.isActive()) {
- pc.body.getWorldTransform(mat0);
- float transx = pc.dimension.x / 2;
- mat0.translate(0, 0, transx);
- frustrum.setWorldTransform(mat0);
- int num = frustrum.getNumOverlappingObjects();
- for (int i = 0; i < num; i++) {
- btCollisionObject over = frustrum.getOverlappingObject(i);
- System.out.println(over.getUserValue());
- }
- // btBroadphasePairArray arr = physicsSystem.broadphase.getOverlappingPairCache().getOverlappingPairArray();
- //
- // for (int i = 0; i < markedEntity.size; i++) {
- // Entity marked = markedEntity.get(i);
- // ModelComponent mc = marked.getComponent(ModelComponent.class);
- // Array<Material> materials = mc.modelInstance.materials;
- // for (int j = 0; j < materials.size; j++) {
- // materials.get(j).remove(ColorAttribute.Diffuse);
- // }
- // }
- // markedEntity.clear();
- //
- // int num = arr.getCollisionObjectsValue(userDataPointer, frustrum);
- // for (int i = 0; i < num; i++) {
- // Entity en = physicsSystem.getEntityByAddress(userDataPointer[i]);
- // TargetComponent target = en.getComponent(TargetComponent.class);
- // if (target != null) {
- // ModelComponent mc = en.getComponent(ModelComponent.class);
- // Array<Material> materials = mc.modelInstance.materials;
- // for (int j = 0; j < materials.size; j++) {
- // materials.get(j).set(ColorAttribute.createDiffuse(Color.ROYAL));
- // }
- // markedEntity.add(en);
- // }
- // }
- //
- // float transy = (pc.dimension.y / 2) + 1;
- // pc.body.getWorldTransform(mat0);
- // mat0.translate(0, -transy, 0);
- // gsensor.setWorldTransform(mat0);
- //
- // int maxIndex = arr.getCollisionObjectsValue(userDataPointer, gsensor);
- // int groundContact = 0;
- // for (int i = 0; i < maxIndex; i++) {
- // int pointer = userDataPointer[i];
- // if (pointer != pc.body.getUserValue()) {
- // groundContact++;
- // }
- // }
- // fly = groundContact < 1;
- }
- }
- @Override
- public void entityAdded(Entity entity) {
- this.entity = entity;
- }
- @Override
- public void entityRemoved(Entity entity) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement