NPC movement fixed

master
Jonathan Hager 5 years ago
parent 5ce7ed0208
commit ee6c8738a2

@ -241,7 +241,8 @@
<objectgroup id="4" name="tuerebene">
<object id="40" x="192" y="2720" width="64" height="64">
<properties>
<property name="id" type="int" value="0"/>
<property name="ID" type="int" value="1337"/>
<property name="test" value="Hallo"/>
</properties>
</object>
</objectgroup>

@ -1,5 +1,17 @@
package com.trs.main;
public class Door {
import com.badlogic.gdx.math.Rectangle;
public class Door{
int id;
int destinationMap;
int destinationDoor;
Rectangle rect;
public Door(int id, int destinationMap, int destinationDoor, Rectangle rect) {
this.id = id;
this.destinationMap = destinationMap;
this.destinationDoor = destinationDoor;
this.rect = rect;
}
}

@ -1,5 +1,41 @@
package com.trs.main;
import java.util.ArrayList;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
public class MapContainer {
Stage stage;
OrthographicCamera camera;
TmxMapLoader tmx;
TiledMap map;
OrthogonalTiledMapRenderer tmr;
ArrayList<Door> doors = new ArrayList<>();
public MapContainer(float CAMERA_WIDTH, float CAMERA_HEIGHT, TiledMap map) {
camera = new OrthographicCamera();
camera.setToOrtho(false, CAMERA_WIDTH, CAMERA_HEIGHT);
camera.update();
this.map = map;
stage = new Stage(new FitViewport(CAMERA_WIDTH, CAMERA_HEIGHT, camera));
for(MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class)){
Rectangle rect = ((RectangleMapObject) object).getRectangle();
MapProperties props = object.getProperties();
//Door door = new Door(props.get("id"), props.get("destinationMap"), props.get("destinationDoor"), rect);
//System.out.println("ID: " + props.get("ID", Integer.class));
//System.out.println("ID: " + props.get("y"));
}
}
}

@ -39,7 +39,7 @@ public class MovingNpc extends Actor{
animatedSprite.setRow(0);
collisionRect = new Rectangle(xPos + 16, yPos, 32, 48);
this.area = area;
speed = 2.5f;
speed = 1f;
setBounds(xPos, yPos, animatedSprite.getSprite().getWidth(), animatedSprite.getSprite().getHeight());
}
@ -47,17 +47,32 @@ public class MovingNpc extends Actor{
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.
super.positionChanged();
}
@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());
if(POI == null || Math.random() < 0.01f){
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())));
//movement.setAngleRad((float)Math.atan((double)(POI.y-getY())/(double)(POI.x-getX())));
movement.setAngleRad(StaticMath.calculateAngle(getX(), getY(), POI.x, POI.y));
if(movement.angleDeg() < 135 && movement.angleDeg() >= 45) {
facing = 0;
}
else if(movement.angleDeg() >= 135 && movement.angleDeg() < 225) {
facing = 1;
}
else if(movement.angleDeg() >= 225 && movement.angleDeg() < 315) {
facing = 2;
}
else {
facing = 3;
}
System.out.println(movement.angleDeg());
System.out.println(POI.x + " " + POI.y);
System.out.println();

@ -0,0 +1,52 @@
package com.trs.main;
/**
*
* @author jonathan
*/
public class StaticMath {
public static float calculateAngle(float xPos1, float yPos1, float xPos2, float yPos2){
float deltaX = xPos2 - xPos1;
float deltaY = yPos2 - yPos1;
double alpha;
if(deltaY == 0){
if(deltaX < 0){
alpha = Math.PI;
}
else{
alpha = 0;
}
}
else if(deltaX == 0 && deltaY >= 0){
alpha = Math.PI / 2;
}
else if(deltaX == 0 && deltaY < 0){
alpha = Math.PI / -2;
}
else{
alpha = Math.abs(Math.atan(deltaY / deltaX));
if(deltaX < 0 && deltaY < 0){
alpha = Math.PI + alpha;
}
else if(deltaX < 0 && deltaY > 0){
alpha = Math.PI - alpha;
}
else if(deltaX > 0 && deltaY < 0){
alpha = 2*Math.PI - alpha;
}
}
return (float) alpha;
}
public static double calculateDistance(int xPos1, int yPos1, int xPos2, int yPos2, double angle){
float deltaX = xPos2 - xPos1;
float deltaY = yPos2 - yPos1;
double distance = Math.abs((deltaY / Math.sin(angle)));
return distance;
}
}

@ -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.MapContainer;
import com.trs.main.MovingNpc;
import com.trs.main.Player;
import com.trs.main.Textbox;
@ -48,6 +49,7 @@ public class GameScreen extends AbstractScreen{
maploader = new TmxMapLoader();
map = maploader.load("map2.tmx");
MapContainer test = new MapContainer(CAMERA_WIDTH, CAMERA_HEIGHT, map);
renderer = new OrthogonalTiledMapRenderer(map);
renderer.setView((OrthographicCamera)stage.getCamera());
stage.getCamera().update();

Loading…
Cancel
Save