From 77f574e9744e98258afc78a7e48be73ad019ec56 Mon Sep 17 00:00:00 2001 From: Anuken Date: Tue, 26 Sep 2017 17:59:29 -0400 Subject: [PATCH] Fixed bugs with enemies not getting put on paths correctly --- core/src/io/anuke/mindustry/Control.java | 4 +- .../mindustry/entities/enemies/Enemy.java | 9 ++++- core/src/io/anuke/mindustry/world/World.java | 39 +++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/core/src/io/anuke/mindustry/Control.java b/core/src/io/anuke/mindustry/Control.java index 488570801a..6e283b8b6c 100644 --- a/core/src/io/anuke/mindustry/Control.java +++ b/core/src/io/anuke/mindustry/Control.java @@ -130,12 +130,12 @@ public class Control extends Module{ ); - + /* //TODO remove this debugging for(int i = 1; i < 60; i ++){ UCore.log("\n\n--WAVE " + i); printEnemies(i); - } + }*/ } diff --git a/core/src/io/anuke/mindustry/entities/enemies/Enemy.java b/core/src/io/anuke/mindustry/entities/enemies/Enemy.java index 0c4a934ce6..30b3044ebb 100644 --- a/core/src/io/anuke/mindustry/entities/enemies/Enemy.java +++ b/core/src/io/anuke/mindustry/entities/enemies/Enemy.java @@ -105,9 +105,14 @@ public class Enemy extends DestructibleEntity{ node = cindex; - //node = 0; + int x2 = path[node].x, y2 = path[node].y; - //set(World.spawnpoints.get(spawn).worldx(), World.spawnpoints.get(spawn).worldy()); + if(World.raycast(Mathf.scl2(x, Vars.tilesize), Mathf.scl2(y, Vars.tilesize), x2, y2) != null){ + Timers.run(Mathf.random(10f), ()->{ + set(x2 * Vars.tilesize, y2 * Vars.tilesize); + }); + + } } @Override diff --git a/core/src/io/anuke/mindustry/world/World.java b/core/src/io/anuke/mindustry/world/World.java index 7a7317e2da..f3e9d80acb 100644 --- a/core/src/io/anuke/mindustry/world/World.java +++ b/core/src/io/anuke/mindustry/world/World.java @@ -20,6 +20,7 @@ import io.anuke.ucore.entities.Entities; import io.anuke.ucore.entities.Entity; import io.anuke.ucore.entities.SolidEntity; import io.anuke.ucore.util.Mathf; +import io.anuke.ucore.util.Tmp; public class World{ public static int worldsize = 128; @@ -274,4 +275,42 @@ public class World{ texture.dispose(); } } + + /** + * Input is in block coordinates, not world coordinates. + * @return null if no collisions found, block position otherwise. + */ + public static Vector2 raycast(int x0f, int y0f, int x1f, int y1f){ + int x0 = (int)x0f; + int y0 = (int)y0f; + int x1 = (int)x1f; + int y1 = (int)y1f; + int dx = Math.abs(x1 - x0); + int dy = Math.abs(y1 - y0); + + int sx = x0 < x1 ? 1 : -1; + int sy = y0 < y1 ? 1 : -1; + + int err = dx - dy; + int e2; + while(true){ + + if(solid(x0, y0)){ + return Tmp.v3.set(x0, y0); + } + if(x0 == x1 && y0 == y1) break; + + e2 = 2 * err; + if(e2 > -dy){ + err = err - dy; + x0 = x0 + sx; + } + + if(e2 < dx){ + err = err + dx; + y0 = y0 + sy; + } + } + return null; + } }