From 62d7318cdaa4a1074a738fb473b6736d1bb7346a Mon Sep 17 00:00:00 2001 From: GammelJAN Date: Sun, 20 Dec 2020 22:35:54 +0100 Subject: [PATCH] map has doorlayere with id - npc class added --- core/assets/map.tmx | 20 ++- core/assets/map2.tmx | 13 +- core/src/com/trs/main/MovingNpc.java | 124 ++++++++++++++++++ .../com/trs/main/view/screens/GameScreen.java | 2 + 4 files changed, 149 insertions(+), 10 deletions(-) create mode 100644 core/src/com/trs/main/MovingNpc.java diff --git a/core/assets/map.tmx b/core/assets/map.tmx index 22f38f7..ad820e3 100644 --- a/core/assets/map.tmx +++ b/core/assets/map.tmx @@ -1,5 +1,5 @@ - + @@ -18,11 +18,11 @@ 137,221,221,221,221,221,221,222,260,261, -201,0,236,237,233,0,0,0,281,178, -201,0,236,233,232,232,0,0,0,199, -201,0,233,178,179,180,0,0,0,199, -201,0,234,199,200,201,0,0,0,199, -201,0,0,220,221,222,236,234,0,199, +201,257,258,237,233,0,0,0,281,178, +201,278,279,233,232,232,0,0,0,199, +201,0,233,0,0,0,0,0,0,199, +201,0,0,0,0,0,0,0,0,199, +201,0,0,0,0,0,236,234,0,199, 201,0,0,0,233,235,235,235,0,199, 201,0,0,0,0,237,232,235,0,199, 158,179,179,179,179,179,179,179,179,159, @@ -35,6 +35,12 @@ - + + + + + + + diff --git a/core/assets/map2.tmx b/core/assets/map2.tmx index eb94114..d435e26 100644 --- a/core/assets/map2.tmx +++ b/core/assets/map2.tmx @@ -1,5 +1,5 @@ - + @@ -88,8 +88,8 @@ 237,237,235,233,232,236,237,234,232,235,237,232,234,234,234,235,236,237,234,236,233,236,236,232,234,233,234,232,235,232,235,232,233,236,235,236,236,234,232,232,232,237,235,236,237,233,235,232,233,236,234,234,235,233,232,235,232,233,232,235,235,236,232,233,232,236,237,235,237,237,237,232,232,234,232,234,236,232,235,232,234,237,235,237,235,234,235,233,236,234,236,232,233,234,235,236,237,235,235,235, 235,235,234,233,235,235,236,234,233,233,232,235,236,237,233,237,232,235,236,237,237,235,232,235,235,233,234,233,234,237,235,232,235,234,234,235,235,235,233,235,232,234,235,235,235,233,236,236,236,232,234,235,233,233,236,234,237,233,232,236,234,234,233,234,235,233,237,236,235,234,232,233,235,233,236,237,233,233,232,232,237,234,235,235,233,237,235,235,236,237,236,235,236,233,237,232,235,237,236,236, 236,237,235,235,232,234,237,233,232,235,232,233,237,237,235,232,236,232,236,236,235,233,235,233,232,235,232,237,236,236,237,232,233,235,232,237,234,237,236,234,235,237,237,234,236,236,232,237,233,233,237,237,233,233,236,233,232,235,232,232,232,232,233,235,233,236,234,233,233,237,237,232,236,234,233,234,233,237,237,232,232,236,237,236,234,237,233,232,237,236,232,235,233,232,237,233,234,233,232,234, -233,232,232,236,237,233,234,237,236,236,236,232,237,235,236,234,233,237,237,232,232,235,232,237,237,235,236,233,236,236,235,234,233,234,232,233,237,237,232,233,233,232,234,232,233,233,232,233,235,235,236,233,237,232,236,234,232,233,233,236,236,236,232,237,233,234,234,235,234,236,237,232,236,236,236,232,235,236,233,232,235,232,233,232,233,233,232,237,234,234,233,232,235,232,236,233,235,233,237,237, -232,233,236,237,237,234,237,234,236,235,234,237,234,232,232,233,236,236,232,234,233,234,237,237,233,237,233,233,235,233,232,234,234,233,234,232,235,234,235,234,234,236,236,237,233,234,232,233,232,235,236,234,237,234,237,232,232,237,233,235,237,235,233,235,236,232,232,235,236,234,232,232,235,237,234,232,233,232,235,233,232,232,236,235,235,232,237,235,232,236,232,234,235,234,236,232,235,236,234,234, +233,232,232,236,237,233,257,258,236,236,236,232,237,235,236,234,233,237,237,232,232,235,232,237,237,235,236,233,236,236,235,234,233,234,232,233,237,237,232,233,233,232,234,232,233,233,232,233,235,235,236,233,237,232,236,234,232,233,233,236,236,236,232,237,233,234,234,235,234,236,237,232,236,236,236,232,235,236,233,232,235,232,233,232,233,233,232,237,234,234,233,232,235,232,236,233,235,233,237,237, +232,233,236,237,237,234,278,279,236,235,234,237,234,232,232,233,236,236,232,234,233,234,237,237,233,237,233,233,235,233,232,234,234,233,234,232,235,234,235,234,234,236,236,237,233,234,232,233,232,235,236,234,237,234,237,232,232,237,233,235,237,235,233,235,236,232,232,235,236,234,232,232,235,237,234,232,233,232,235,233,232,232,236,235,235,232,237,235,232,236,232,234,235,234,236,232,235,236,234,234, 232,235,235,237,233,235,237,234,237,235,234,235,237,233,234,237,232,237,233,233,232,237,233,232,232,237,235,233,233,235,233,237,235,232,237,236,237,233,233,232,234,232,236,235,236,235,237,236,235,236,232,237,234,235,235,237,234,237,236,234,233,232,233,234,234,233,236,237,235,235,234,236,236,235,237,232,234,232,234,236,237,235,232,235,235,234,232,233,232,232,235,237,234,233,232,235,233,233,233,232, 236,236,234,236,237,234,233,237,233,235,235,233,235,234,234,237,232,235,233,235,232,236,234,236,234,233,232,237,235,234,237,232,234,234,237,237,234,232,235,234,236,237,234,233,236,232,235,235,234,236,236,232,235,236,232,237,235,234,236,236,235,234,234,234,236,234,232,234,233,232,234,236,237,235,232,234,236,236,236,237,234,233,233,237,236,236,232,235,235,232,235,234,237,237,233,232,236,234,233,233, 236,235,237,237,234,234,234,236,235,235,233,235,234,237,235,236,232,237,232,236,236,233,234,232,234,234,232,235,237,237,232,233,234,233,236,234,233,234,236,236,232,233,237,236,233,234,234,232,236,237,235,232,236,233,233,233,235,234,237,233,237,235,237,232,236,235,235,237,234,237,232,233,232,232,233,237,235,234,232,236,236,233,234,234,234,235,234,237,234,237,232,232,232,237,235,236,232,236,235,236, @@ -238,4 +238,11 @@ + + + + + + + diff --git a/core/src/com/trs/main/MovingNpc.java b/core/src/com/trs/main/MovingNpc.java new file mode 100644 index 0000000..05bddb1 --- /dev/null +++ b/core/src/com/trs/main/MovingNpc.java @@ -0,0 +1,124 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.trs.main; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.math.Intersector; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.scenes.scene2d.Actor; +import static com.trs.main.Player.SQRT2; + +/** + * + * @author Jan + */ +public class MovingNpc extends Actor{ + + public static final float SQRT2 = 1.414f; + Textbox t; + Rectangle area; + Rectangle collisionRect; + float speed; + private AnimatedSprite animatedSprite; + int facing; + float movementX; + float movementY; + + Vector2 POI; + + public MovingNpc(Rectangle area, float xPos, float yPos){ + setName("npc"); + Texture t = new Texture(Gdx.files.internal("player.png")); + animatedSprite = new AnimatedSprite(t, 64, 64); + animatedSprite.setRow(0); + collisionRect = new Rectangle(xPos + 16, yPos, 32, 48); + this.area = area; + speed = 2.5f; + setBounds(xPos, yPos, animatedSprite.getSprite().getWidth(), animatedSprite.getSprite().getHeight()); + } + + @Override + protected void positionChanged() { + animatedSprite.setSpritePosition((int)getX(), (int)getY()); + collisionRect = new Rectangle(getX() + 16, getY(), 32, 48); + super.positionChanged(); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void act(float delta) { + + if(POI == null || Math.random() < 0.05f){ + POI = new Vector2(area.getX()+(float)Math.random() * (float)area.getWidth(), area.getY()+(float)Math.random()*(float)area.getHeight()); + } + Vector2 movement = new Vector2(speed,0); + movement.setAngleRad((float)Math.atan((double)(POI.y-getY())/(double)(POI.x-getX()))); + System.out.println(movement.angleDeg()); + System.out.println(POI.x + " " + POI.y); + System.out.println(); + movementX = movement.x; + movementY = movement.y; + if(movementX == 0 && movementY == 0){ + } + else if(movementX == 0 && movementY != 0){ + setY(getY()+movementY); + if(collidingWithMapCollisionObject()){ + setY(getY()-movementY); + } + } + else if(movementY == 0 && movementX != 0){ + setX(getX()+movementX); + if(collidingWithMapCollisionObject()){ + setX(getX()-movementX); + } + } + else if(movementX != 0 && movementY != 0){ + setX(getX()+ (movementX)); + if(collidingWithMapCollisionObject()){ + setX(getX() - (movementX)); + } + + setY(getY() + (movementY)); + if(collidingWithMapCollisionObject()){ + setY(getY()- (movementY)); + } + } + + int animationRow = 0; + if(movementX != 0 || movementY != 0) { + animationRow = 8; + } + + animatedSprite.setRow(animationRow + facing); + animatedSprite.updateAnimation(delta); + movementX = 0; + movementY = 0; + } + + @Override + public void draw(Batch batch, float parentAlpha) { + animatedSprite.draw(batch); + super.draw(batch, parentAlpha); //To change body of generated methods, choose Tools | Templates. + } + + public boolean collidingWithMapCollisionObject(){ + boolean value = false; + for(Actor a : getStage().getActors()){ + if(a.getName().equals("mapobject")){ + //Rectangle p = new Rectangle(getX(), getY(), getWidth(), getHeight()); + Rectangle o = new Rectangle(a.getX(), a.getY(), a.getWidth(), a.getHeight()); + if(Intersector.overlaps(collisionRect, o)){ + value = true; + break; + } + } + } + return value; + } + +} diff --git a/core/src/com/trs/main/view/screens/GameScreen.java b/core/src/com/trs/main/view/screens/GameScreen.java index 3a6bd57..7daf8a2 100644 --- a/core/src/com/trs/main/view/screens/GameScreen.java +++ b/core/src/com/trs/main/view/screens/GameScreen.java @@ -22,6 +22,7 @@ import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Group; import com.trs.main.Main; import com.trs.main.MapCollisionObject; +import com.trs.main.MovingNpc; import com.trs.main.Player; import com.trs.main.Textbox; import java.util.ArrayList; @@ -42,6 +43,7 @@ public class GameScreen extends AbstractScreen{ public GameScreen(Game game, float CAMERA_WIDTH, float CAMERA_HEIGHT) { super(game, CAMERA_WIDTH, CAMERA_HEIGHT); stage.addActor(new Player(250,50)); + stage.addActor(new MovingNpc(new Rectangle(20,20,400,400), 80, 80)); //setTextbox(new Textbox("How are you doing my friend How are you doing my friend How are you doing my friend How are you doing my friend", "good", "bad")); maploader = new TmxMapLoader();