Advertisement
Laputa889272

Untitled

Feb 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. package com.game.tank.system;
  2.  
  3. import com.badlogic.ashley.core.ComponentMapper;
  4. import com.badlogic.ashley.core.Engine;
  5. import com.badlogic.ashley.core.Entity;
  6. import com.badlogic.ashley.core.EntityListener;
  7. import com.badlogic.ashley.core.EntitySystem;
  8. import com.badlogic.ashley.core.Family;
  9. import com.badlogic.gdx.Gdx;
  10. import com.badlogic.gdx.Input;
  11. import com.badlogic.gdx.math.Matrix4;
  12. import com.badlogic.gdx.math.Quaternion;
  13. import com.badlogic.gdx.math.Vector2;
  14. import com.badlogic.gdx.math.Vector3;
  15. import com.badlogic.gdx.physics.bullet.collision.btBoxShape;
  16. import com.badlogic.gdx.physics.bullet.collision.btBroadphasePair;
  17. import com.badlogic.gdx.physics.bullet.collision.btBroadphasePairArray;
  18. import com.badlogic.gdx.physics.bullet.collision.btBroadphaseProxy;
  19. import com.badlogic.gdx.physics.bullet.collision.btCollisionAlgorithm;
  20. import com.badlogic.gdx.physics.bullet.collision.btCollisionObject;
  21. import com.badlogic.gdx.physics.bullet.collision.btHashedOverlappingPairCache;
  22. import com.badlogic.gdx.physics.bullet.collision.btOverlappingPairCache;
  23. import com.badlogic.gdx.physics.bullet.collision.btPairCachingGhostObject;
  24. import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody;
  25. import com.badlogic.gdx.utils.Array;
  26. import com.game.tank.component.AnimationComponent;
  27. import com.game.tank.component.CharacterInputComponent;
  28. import com.game.tank.component.PhysicsComponent;
  29.  
  30. /**
  31. *
  32. * @author Fian Manafe
  33. */
  34. public class CharacterInputSystem extends EntitySystem implements EntityListener {
  35.  
  36. private Entity entity;
  37. private final ComponentMapper<PhysicsComponent> pcMapper;
  38. private final Vector3 vec0 = new Vector3();
  39. private final Vector3 vec1 = new Vector3();
  40. private final Vector3 vec3 = new Vector3();
  41. private final Quaternion qua0 = new Quaternion();
  42. private final Matrix4 mat0 = new Matrix4();
  43. private final int[] userDataPointer = new int[10];
  44. private final Vector2 playerDirection = new Vector2(0, 1);
  45. private final Vector2 tempDirection = new Vector2();
  46. private final Array<Entity> markedEntity = new Array<Entity>();
  47. private boolean fly;
  48. private PhysicsSystem physicsSystem;
  49. private btPairCachingGhostObject frustrum;
  50. private btPairCachingGhostObject gsensor;
  51.  
  52. public CharacterInputSystem() {
  53. this.pcMapper = ComponentMapper.getFor(PhysicsComponent.class);
  54. }
  55.  
  56. @Override
  57. public void addedToEngine(Engine engine) {
  58. physicsSystem = engine.getSystem(PhysicsSystem.class);
  59. engine.addEntityListener(Family.all(CharacterInputComponent.class).get(), this);
  60. frustrum = new btPairCachingGhostObject();
  61. frustrum.setCollisionShape(new btBoxShape(new Vector3(1, 1, 1)));
  62. frustrum.setCollisionFlags(btCollisionObject.CollisionFlags.CF_NO_CONTACT_RESPONSE | btCollisionObject.CollisionFlags.CF_CHARACTER_OBJECT);
  63. physicsSystem.collisionWorld.addCollisionObject(frustrum);
  64. // gsensor = new btPairCachingGhostObject();
  65. // gsensor.setCollisionShape(new btBoxShape(new Vector3(1, 0.1f, 1)));
  66. // gsensor.setCollisionFlags(btCollisionObject.CollisionFlags.CF_NO_CONTACT_RESPONSE);
  67. // physicsSystem.collisionWorld.addCollisionObject(gsensor, btBroadphaseProxy.CollisionFilterGroups.SensorTrigger, btBroadphaseProxy.CollisionFilterGroups.AllFilter);
  68. }
  69.  
  70. @Override
  71. public void update(float deltaTime) {
  72. if (entity == null) {
  73. return;
  74. }
  75. tempDirection.set(playerDirection);
  76. PhysicsComponent pc = pcMapper.get(entity);
  77. AnimationComponent an = entity.getComponent(AnimationComponent.class);
  78. btRigidBody body = (btRigidBody) pc.body;
  79. if (!fly) {
  80. vec0.set(0, 0, 0);
  81. if (Gdx.input.isKeyPressed(Input.Keys.A)) {
  82. vec0.x--;
  83. vec0.z++;
  84. tempDirection.set(vec0.x, vec0.z);
  85. }
  86. if (Gdx.input.isKeyPressed(Input.Keys.W)) {
  87. vec0.z--;
  88. vec0.x--;
  89. tempDirection.set(vec0.x, vec0.z);
  90. }
  91. if (Gdx.input.isKeyPressed(Input.Keys.S)) {
  92. vec0.z++;
  93. vec0.x++;
  94. tempDirection.set(vec0.x, vec0.z);
  95. }
  96. if (Gdx.input.isKeyPressed(Input.Keys.D)) {
  97. vec0.x++;
  98. vec0.z--;
  99. tempDirection.set(vec0.x, vec0.z);
  100. }
  101. vec0.nor();
  102. if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
  103. vec0.y += 3;
  104. }
  105. body.setGravity(new Vector3(0, -98, 0));
  106. if (!vec0.isZero()) {
  107. body.setFriction(0);
  108. body.activate();
  109. body.setLinearVelocity(vec0.scl(10));
  110. if (!tempDirection.isZero() && (an.getCurrentAnimation() == null || !an.getCurrentAnimation().animation.id.equals("Armature|run"))) {
  111. an.animate("Armature|run", -1, 5);
  112. }
  113. if (vec0.y != 0) {
  114. an.animate("Armature|jump", -1, 4);
  115. }
  116. } else {
  117. body.setFriction(100);
  118. if (an.getCurrentAnimation() == null || !an.getCurrentAnimation().animation.id.equals("Armature|standby")) {
  119. an.animate("Armature|standby", -1, 1);
  120. }
  121. }
  122. }
  123.  
  124. if (!tempDirection.isCollinear(playerDirection)) {
  125. playerDirection.set(tempDirection);
  126. body.getWorldTransform(mat0);
  127. body.activate();
  128. float angle = (float) Math.atan2(playerDirection.x, playerDirection.y);
  129. qua0.set(new Vector3(0, 1, 0), (float) Math.toDegrees(angle));
  130. mat0.getTranslation(vec1);
  131. mat0.set(vec1, qua0);
  132. body.setWorldTransform(mat0);
  133. }
  134.  
  135. if (Gdx.input.isKeyPressed(Input.Keys.ENTER)) {
  136. body.getWorldTransform(mat0);
  137. mat0.getTranslation(vec1);
  138. vec3.set(playerDirection.x, 0, playerDirection.y).nor().scl(2);
  139. vec1.add(vec3);
  140. getEngine().getSystem(AK47System.class).shoot(deltaTime, vec1, vec3);
  141. }
  142.  
  143. if (body.isActive()) {
  144. pc.body.getWorldTransform(mat0);
  145. float transx = pc.dimension.x / 2;
  146. mat0.translate(0, 0, transx);
  147. frustrum.setWorldTransform(mat0);
  148.  
  149. int num = frustrum.getNumOverlappingObjects();
  150. for (int i = 0; i < num; i++) {
  151. btCollisionObject over = frustrum.getOverlappingObject(i);
  152. System.out.println(over.getUserValue());
  153. }
  154.  
  155. // btBroadphasePairArray arr = physicsSystem.broadphase.getOverlappingPairCache().getOverlappingPairArray();
  156. //
  157. // for (int i = 0; i < markedEntity.size; i++) {
  158. // Entity marked = markedEntity.get(i);
  159. // ModelComponent mc = marked.getComponent(ModelComponent.class);
  160. // Array<Material> materials = mc.modelInstance.materials;
  161. // for (int j = 0; j < materials.size; j++) {
  162. // materials.get(j).remove(ColorAttribute.Diffuse);
  163. // }
  164. // }
  165. // markedEntity.clear();
  166. //
  167. // int num = arr.getCollisionObjectsValue(userDataPointer, frustrum);
  168. // for (int i = 0; i < num; i++) {
  169. // Entity en = physicsSystem.getEntityByAddress(userDataPointer[i]);
  170. // TargetComponent target = en.getComponent(TargetComponent.class);
  171. // if (target != null) {
  172. // ModelComponent mc = en.getComponent(ModelComponent.class);
  173. // Array<Material> materials = mc.modelInstance.materials;
  174. // for (int j = 0; j < materials.size; j++) {
  175. // materials.get(j).set(ColorAttribute.createDiffuse(Color.ROYAL));
  176. // }
  177. // markedEntity.add(en);
  178. // }
  179. // }
  180. //
  181. // float transy = (pc.dimension.y / 2) + 1;
  182. // pc.body.getWorldTransform(mat0);
  183. // mat0.translate(0, -transy, 0);
  184. // gsensor.setWorldTransform(mat0);
  185. //
  186. // int maxIndex = arr.getCollisionObjectsValue(userDataPointer, gsensor);
  187. // int groundContact = 0;
  188. // for (int i = 0; i < maxIndex; i++) {
  189. // int pointer = userDataPointer[i];
  190. // if (pointer != pc.body.getUserValue()) {
  191. // groundContact++;
  192. // }
  193. // }
  194. // fly = groundContact < 1;
  195. }
  196. }
  197.  
  198. @Override
  199. public void entityAdded(Entity entity) {
  200. this.entity = entity;
  201. }
  202.  
  203. @Override
  204. public void entityRemoved(Entity entity) {
  205.  
  206. }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement