Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. package com.mygdx.game.Entity;
  2.  
  3. import java.io.File;
  4.  
  5. import javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7.  
  8. import org.w3c.dom.Document;
  9. import org.w3c.dom.Node;
  10. import org.w3c.dom.NodeList;
  11.  
  12. import com.badlogic.gdx.Gdx;
  13. import com.badlogic.gdx.Input;
  14. import com.badlogic.gdx.graphics.Texture;
  15. import com.badlogic.gdx.graphics.g2d.Batch;
  16. import com.badlogic.gdx.graphics.g2d.Sprite;
  17. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  18. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  19. import com.badlogic.gdx.physics.box2d.Body;
  20. import com.badlogic.gdx.physics.box2d.BodyDef;
  21. import com.badlogic.gdx.physics.box2d.PolygonShape;
  22. import com.badlogic.gdx.physics.box2d.World;
  23. import com.mygdx.game.DitFMain;
  24. import com.mygdx.game.GameAssetManager;
  25.  
  26. public class Player {
  27. private String locationgame = DitFMain.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  28. private String dirPathgame = new File(locationgame).getParent();
  29. private int x;
  30. private int y;
  31. public Body player;
  32. int chrl[] = new int[3];
  33. Texture clothes[];
  34. TextureRegion clothesReg[];
  35. Texture playerTexture;
  36. TextureRegion playerRegion;
  37. public Sprite playerSprite;
  38. Sprite chr[] = new Sprite[3];
  39. public int slots;
  40. public int weight;
  41. public int maxWeight;
  42.  
  43. public Player(int x, int y, World world, int w, int h, String pname){
  44. this.ReadXML(pname);
  45. this.x = x;
  46. this.y = y;
  47. player = playerBox(world, x, y, w, h);
  48. playerTexture = GameAssetManager.getInstance().get("sprites/player/player.png");
  49. playerRegion = new TextureRegion(playerTexture);
  50. playerSprite = new Sprite(playerTexture);
  51. playerSprite.setRegion(playerRegion, 0, 0, playerTexture.getWidth(), playerTexture.getHeight());
  52. playerSprite.setSize(playerTexture.getWidth()/5, playerTexture.getHeight()/6);
  53. clothes = new Texture[3];
  54. clothesReg = new TextureRegion[3];
  55. for(int i = 0;i<3;i++)
  56. {
  57. clothes[i] = GameAssetManager.getInstance().get("sprites/player/" + (i+1) + "/" + (chrl[i]+1) + ".png");
  58. clothesReg[i] = new TextureRegion(clothes[i]);
  59. }
  60. for(int i = 0;i<=2;i++)
  61. {
  62. chr[i] = new Sprite(clothesReg[i]);
  63. chr[i].setRegion(clothesReg[i], 0, 0, clothes[i].getWidth(), clothes[i].getHeight());
  64. chr[i].setSize(clothes[i].getWidth()/5, clothes[i].getHeight()/6);
  65. }
  66. }
  67.  
  68. public void ReadXML(String fXmls)
  69. {
  70. System.out.println(fXmls);
  71. File fXml=new File(dirPathgame + "/assets/" + fXmls + "/data.dfe");
  72. try{
  73. DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
  74. DocumentBuilder db=dbf.newDocumentBuilder();
  75. Document doc=db.parse(fXml);
  76.  
  77. doc.getDocumentElement().normalize();
  78. System.out.println("Root element ["+doc.getDocumentElement().getNodeName()+"]");
  79.  
  80. NodeList nodeLst=doc.getElementsByTagName("Hair");
  81. Node hair=nodeLst.item(0);
  82. Node node = hair.getFirstChild();
  83.  
  84. chrl[0] = new Integer(node.getNodeValue());
  85. NodeList nodeLst2=doc.getElementsByTagName("Shirt");
  86. Node Shirt=nodeLst2.item(0);
  87. node = Shirt.getFirstChild();
  88.  
  89. chrl[1] = new Integer(node.getNodeValue());
  90. NodeList nodeLst3=doc.getElementsByTagName("Pant");
  91. Node Pant=nodeLst3.item(0);
  92. node = Pant.getFirstChild();
  93. chrl[2] = new Integer(node.getNodeValue());
  94. System.out.println(chrl[0]);
  95. System.out.println(chrl[1]);
  96. System.out.println(chrl[2]);
  97. //Slots
  98. NodeList nodeLst4=doc.getElementsByTagName("FreeSlot");
  99. Node FreeSlot=nodeLst3.item(0);
  100. node = FreeSlot.getFirstChild();
  101. slots = new Integer(node.getNodeValue());
  102. NodeList nodeLst5=doc.getElementsByTagName("Weight");
  103. Node Weight=nodeLst3.item(0);
  104. node = Weight.getFirstChild();
  105. weight = new Integer(node.getNodeValue());
  106. NodeList nodeLst6=doc.getElementsByTagName("MaxWeight");
  107. Node MaxWeight=nodeLst3.item(0);
  108. node = MaxWeight.getFirstChild();
  109. maxWeight = new Integer(node.getNodeValue());
  110. }catch(Exception ei){System.out.println("NOFILE");}
  111. }
  112.  
  113. private Body playerBox(World world, int x, int y, int w, int h) {
  114. Body body2;
  115. BodyDef bdef2 = new BodyDef();
  116. bdef2.type = BodyDef.BodyType.DynamicBody;
  117. bdef2.position.set(x, y);
  118. bdef2.fixedRotation = true;
  119. body2 = world.createBody(bdef2);
  120. PolygonShape ps = new PolygonShape();
  121. ps.setAsBox(w/5,h/5);
  122. body2.createFixture(ps, 1.0f);
  123. ps.dispose();
  124. return body2;
  125. }
  126.  
  127. public void render(SpriteBatch batch) {
  128. update();
  129. playerSprite.draw(batch);
  130. for(int i =0;i<=2;i++)
  131. {
  132. chr[i].draw(batch);
  133. }
  134. }
  135.  
  136. private void update() {
  137. movingUpdate();
  138. playerSprite.setPosition((float) ((float)player.getPosition().x - 3.5),player.getPosition().y - 10);
  139.  
  140. chr[0].setPosition(playerSprite.getX(),playerSprite.getY()+playerSprite.getHeight());
  141. chr[1].setPosition(playerSprite.getX(),playerSprite.getY()+playerSprite.getHeight()/2);
  142. chr[2].setPosition(playerSprite.getX(),playerSprite.getY());
  143. }
  144.  
  145. private void movingUpdate() {
  146. int horizontspeed = 0;
  147. if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
  148. horizontspeed = -10;
  149. }
  150. if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
  151. horizontspeed = 10;
  152. }
  153. if(Gdx.input.isKeyJustPressed(Input.Keys.UP) && player.getLinearVelocity().y == 0){
  154. player.applyForceToCenter(0, 100000000, true);
  155. }
  156.  
  157. player.setLinearVelocity(horizontspeed * 5, player.getLinearVelocity().y);
  158. }
  159.  
  160. public int getX() {
  161. return x;
  162. }
  163.  
  164. public int getY() {
  165. return y;
  166. }
  167.  
  168. public void dispose() {
  169.  
  170. }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement