Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mygdx.game.Entity;
- import java.io.File;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Batch;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.graphics.g2d.TextureRegion;
- import com.badlogic.gdx.physics.box2d.Body;
- import com.badlogic.gdx.physics.box2d.BodyDef;
- import com.badlogic.gdx.physics.box2d.PolygonShape;
- import com.badlogic.gdx.physics.box2d.World;
- import com.mygdx.game.DitFMain;
- import com.mygdx.game.GameAssetManager;
- public class Player {
- private String locationgame = DitFMain.class.getProtectionDomain().getCodeSource().getLocation().getPath();
- private String dirPathgame = new File(locationgame).getParent();
- private int x;
- private int y;
- public Body player;
- int chrl[] = new int[3];
- Texture clothes[];
- TextureRegion clothesReg[];
- Texture playerTexture;
- TextureRegion playerRegion;
- public Sprite playerSprite;
- Sprite chr[] = new Sprite[3];
- public int slots;
- public int weight;
- public int maxWeight;
- public Player(int x, int y, World world, int w, int h, String pname){
- this.ReadXML(pname);
- this.x = x;
- this.y = y;
- player = playerBox(world, x, y, w, h);
- playerTexture = GameAssetManager.getInstance().get("sprites/player/player.png");
- playerRegion = new TextureRegion(playerTexture);
- playerSprite = new Sprite(playerTexture);
- playerSprite.setRegion(playerRegion, 0, 0, playerTexture.getWidth(), playerTexture.getHeight());
- playerSprite.setSize(playerTexture.getWidth()/5, playerTexture.getHeight()/6);
- clothes = new Texture[3];
- clothesReg = new TextureRegion[3];
- for(int i = 0;i<3;i++)
- {
- clothes[i] = GameAssetManager.getInstance().get("sprites/player/" + (i+1) + "/" + (chrl[i]+1) + ".png");
- clothesReg[i] = new TextureRegion(clothes[i]);
- }
- for(int i = 0;i<=2;i++)
- {
- chr[i] = new Sprite(clothesReg[i]);
- chr[i].setRegion(clothesReg[i], 0, 0, clothes[i].getWidth(), clothes[i].getHeight());
- chr[i].setSize(clothes[i].getWidth()/5, clothes[i].getHeight()/6);
- }
- }
- public void ReadXML(String fXmls)
- {
- System.out.println(fXmls);
- File fXml=new File(dirPathgame + "/assets/" + fXmls + "/data.dfe");
- try{
- DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
- DocumentBuilder db=dbf.newDocumentBuilder();
- Document doc=db.parse(fXml);
- doc.getDocumentElement().normalize();
- System.out.println("Root element ["+doc.getDocumentElement().getNodeName()+"]");
- NodeList nodeLst=doc.getElementsByTagName("Hair");
- Node hair=nodeLst.item(0);
- Node node = hair.getFirstChild();
- chrl[0] = new Integer(node.getNodeValue());
- NodeList nodeLst2=doc.getElementsByTagName("Shirt");
- Node Shirt=nodeLst2.item(0);
- node = Shirt.getFirstChild();
- chrl[1] = new Integer(node.getNodeValue());
- NodeList nodeLst3=doc.getElementsByTagName("Pant");
- Node Pant=nodeLst3.item(0);
- node = Pant.getFirstChild();
- chrl[2] = new Integer(node.getNodeValue());
- System.out.println(chrl[0]);
- System.out.println(chrl[1]);
- System.out.println(chrl[2]);
- //Slots
- NodeList nodeLst4=doc.getElementsByTagName("FreeSlot");
- Node FreeSlot=nodeLst3.item(0);
- node = FreeSlot.getFirstChild();
- slots = new Integer(node.getNodeValue());
- NodeList nodeLst5=doc.getElementsByTagName("Weight");
- Node Weight=nodeLst3.item(0);
- node = Weight.getFirstChild();
- weight = new Integer(node.getNodeValue());
- NodeList nodeLst6=doc.getElementsByTagName("MaxWeight");
- Node MaxWeight=nodeLst3.item(0);
- node = MaxWeight.getFirstChild();
- maxWeight = new Integer(node.getNodeValue());
- }catch(Exception ei){System.out.println("NOFILE");}
- }
- private Body playerBox(World world, int x, int y, int w, int h) {
- Body body2;
- BodyDef bdef2 = new BodyDef();
- bdef2.type = BodyDef.BodyType.DynamicBody;
- bdef2.position.set(x, y);
- bdef2.fixedRotation = true;
- body2 = world.createBody(bdef2);
- PolygonShape ps = new PolygonShape();
- ps.setAsBox(w/5,h/5);
- body2.createFixture(ps, 1.0f);
- ps.dispose();
- return body2;
- }
- public void render(SpriteBatch batch) {
- update();
- playerSprite.draw(batch);
- for(int i =0;i<=2;i++)
- {
- chr[i].draw(batch);
- }
- }
- private void update() {
- movingUpdate();
- playerSprite.setPosition((float) ((float)player.getPosition().x - 3.5),player.getPosition().y - 10);
- chr[0].setPosition(playerSprite.getX(),playerSprite.getY()+playerSprite.getHeight());
- chr[1].setPosition(playerSprite.getX(),playerSprite.getY()+playerSprite.getHeight()/2);
- chr[2].setPosition(playerSprite.getX(),playerSprite.getY());
- }
- private void movingUpdate() {
- int horizontspeed = 0;
- if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
- horizontspeed = -10;
- }
- if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
- horizontspeed = 10;
- }
- if(Gdx.input.isKeyJustPressed(Input.Keys.UP) && player.getLinearVelocity().y == 0){
- player.applyForceToCenter(0, 100000000, true);
- }
- player.setLinearVelocity(horizontspeed * 5, player.getLinearVelocity().y);
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public void dispose() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement