Arcturus-Community/src/main/java/com/eu/habbo/threading/runnables/RoomUnitWalkToRoomUnit.java

120 lines
3.6 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.threading.runnables;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.habbohotel.rooms.RoomTile;
import com.eu.habbo.habbohotel.rooms.RoomUnit;
import com.eu.habbo.habbohotel.wired.WiredHandler;
import com.eu.habbo.habbohotel.wired.WiredTriggerType;
import java.util.List;
public class RoomUnitWalkToRoomUnit implements Runnable
{
private final int minDistance;
2018-07-06 15:30:00 +02:00
private RoomUnit walker;
private RoomUnit target;
private Room room;
private List<Runnable> targetReached;
private List<Runnable> failedReached;
private RoomTile goalTile = null;
public RoomUnitWalkToRoomUnit(RoomUnit walker, RoomUnit target, Room room, List<Runnable> targetReached, List<Runnable> failedReached)
{
this.walker = walker;
this.target = target;
this.room = room;
this.targetReached = targetReached;
this.failedReached = failedReached;
this.minDistance = 1;
}
public RoomUnitWalkToRoomUnit(RoomUnit walker, RoomUnit target, Room room, List<Runnable> targetReached, List<Runnable> failedReached, int minDistance)
{
this.walker = walker;
this.target = target;
this.room = room;
this.targetReached = targetReached;
this.failedReached = failedReached;
this.minDistance = minDistance;
2018-07-06 15:30:00 +02:00
}
@Override
public void run()
{
if(this.goalTile == null)
{
this.findNewLocation();
Emulator.getThreading().run(this, 500);
}
if(this.goalTile == null)
return;
if(this.walker.getGoal().equals(this.goalTile)) //Check if the goal is still the same. Chances are something is running the same task. If so we dump this task.
2018-07-06 15:30:00 +02:00
{
//Check if arrived.
if(this.walker.getCurrentLocation().distance(this.goalTile) <= this.minDistance)
2018-07-06 15:30:00 +02:00
{
for(Runnable r : this.targetReached)
{
Emulator.getThreading().run(r);
WiredHandler.handle(WiredTriggerType.BOT_REACHED_AVTR, this.target, this.room, new Object[]{this.walker});
}
}
else
{
2019-03-18 02:22:00 +01:00
List<RoomTile> tiles = this.room.getLayout().getTilesAround(this.target.getCurrentLocation());
2018-07-06 15:30:00 +02:00
for(RoomTile t : tiles)
{
if(t.equals(this.goalTile))
{
Emulator.getThreading().run(this, 500);
return;
}
}
this.findNewLocation();
Emulator.getThreading().run(this, 500);
}
}
}
private void findNewLocation()
{
this.goalTile = this.room.getLayout().getTileInFront(this.target.getCurrentLocation(), this.target.getBodyRotation().getValue());
if (this.goalTile == null)
return;
if (!this.room.tileWalkable(this.goalTile))
{
2019-03-18 02:22:00 +01:00
List<RoomTile> tiles = this.room.getLayout().getTilesAround(this.target.getCurrentLocation());
2018-07-06 15:30:00 +02:00
for (RoomTile t : tiles)
{
if (this.room.tileWalkable(t))
{
this.goalTile = t;
break;
}
}
}
this.walker.setGoalLocation(this.goalTile);
if (this.walker.getPath().isEmpty() && this.failedReached != null)
2018-07-06 15:30:00 +02:00
{
for(Runnable r : this.failedReached)
{
Emulator.getThreading().run(r);
}
}
}
}