Implemented enemy avoidance

This commit is contained in:
Anuken
2017-11-11 13:55:38 -05:00
parent fb5111a414
commit 12aa8c406b
7 changed files with 33 additions and 15 deletions
+1 -1
View File
@@ -79,7 +79,7 @@ project(":core") {
apply plugin: "java" apply plugin: "java"
dependencies { dependencies {
compile 'com.github.anuken:ucore:91e4e11010' compile 'com.github.anuken:ucore:c498e5920a'
compile "com.badlogicgames.gdx:gdx:$gdxVersion" compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-ai:1.8.1" compile "com.badlogicgames.gdx:gdx-ai:1.8.1"
} }
Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 714 B

@@ -2,6 +2,7 @@ package io.anuke.mindustry.entities.enemies;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.reflect.ClassReflection; import com.badlogic.gdx.utils.reflect.ClassReflection;
import io.anuke.mindustry.Shaders; import io.anuke.mindustry.Shaders;
@@ -15,6 +16,7 @@ import io.anuke.mindustry.world.World;
import io.anuke.ucore.core.*; import io.anuke.ucore.core.*;
import io.anuke.ucore.entities.*; import io.anuke.ucore.entities.*;
import io.anuke.ucore.util.Mathf; import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Tmp;
public class Enemy extends DestructibleEntity{ public class Enemy extends DestructibleEntity{
public final static Color[] tierColors = {Color.YELLOW, Color.ORANGE, Color.RED, Color.MAGENTA}; public final static Color[] tierColors = {Color.YELLOW, Color.ORANGE, Color.RED, Color.MAGENTA};
@@ -59,6 +61,22 @@ public class Enemy extends DestructibleEntity{
Vector2 vec = Pathfind.find(this); Vector2 vec = Pathfind.find(this);
vec.sub(x, y).setLength(speed); vec.sub(x, y).setLength(speed);
Array<SolidEntity> entities = Entities.getNearby(x, y, range);
Vector2 shift = Tmp.v3.setZero();
float shiftRange = hitbox.width + 3f;
for(SolidEntity other : entities){
float dst = other.distanceTo(this);
if(other != this && other instanceof Enemy && dst < shiftRange){
float scl = Mathf.clamp(1.4f - dst/shiftRange);
shift.add((x - other.x) * scl, (y - other.y) * scl);
}
}
shift.nor();
vec.add(shift.scl(0.5f));
move(vec.x*Timers.delta(), vec.y*Timers.delta()); move(vec.x*Timers.delta(), vec.y*Timers.delta());
if(Timers.get(this, "target", 15)){ if(Timers.get(this, "target", 15)){
@@ -66,9 +84,7 @@ public class Enemy extends DestructibleEntity{
//no tile found //no tile found
if(target == null){ if(target == null){
target = Entities.getClosest(x, y, range, e->{ target = Entities.getClosest(entities, x, y, range, e -> e instanceof Player);
return e instanceof Player;
});
} }
} }
@@ -35,7 +35,7 @@ public class LevelDialog extends FloatingDialog{
for(int i = 0; i < Map.values().length; i ++){ for(int i = 0; i < Map.values().length; i ++){
Map map = Map.values()[i]; Map map = Map.values()[i];
if(!map.visible) continue; if(!map.visible && !Vars.debug) continue;
if(i % maxwidth == 0){ if(i % maxwidth == 0){
maps.row(); maps.row();
@@ -48,18 +48,18 @@ public class LevelDialog extends FloatingDialog{
.pad(3f).units(Unit.dp); .pad(3f).units(Unit.dp);
inset.pack(); inset.pack();
float images = Unit.dp.inPixels(154); float images = 154f;
ImageButton image = new ImageButton(new TextureRegion(World.getTexture(map)), "togglemap"); ImageButton image = new ImageButton(new TextureRegion(World.getTexture(map)), "togglemap");
image.row(); image.row();
image.add(inset).width(images+6); image.add(inset).width(images+6).units(Unit.dp);
image.clicked(()->{ image.clicked(()->{
selectedMap = map; selectedMap = map;
hide(); hide();
Vars.control.playMap(selectedMap); Vars.control.playMap(selectedMap);
}); });
image.getImageCell().size(images); image.getImageCell().size(images);
maps.add(image).width(Unit.dp.inPixels(170)).pad(4f).units(Unit.dp); maps.add(image).width(170).pad(4f).units(Unit.dp);
} }
content().add(pane); content().add(pane);
+1 -2
View File
@@ -203,9 +203,8 @@ public class Block{
} }
//update the tile entity through the draw method, only if it's an entity without updating //update the tile entity through the draw method, only if it's an entity without updating
//TODO enable
if(destructible && !update && !GameState.is(State.paused)){ if(destructible && !update && !GameState.is(State.paused)){
// tile.entity.update(); tile.entity.update();
} }
} }
@@ -2,6 +2,7 @@ package io.anuke.mindustry.world;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ObjectMap; import com.badlogic.gdx.utils.ObjectMap;
import io.anuke.mindustry.Vars; import io.anuke.mindustry.Vars;
@@ -94,9 +95,9 @@ public class Generator{
} }
//preformance debugging //preformance debugging
//if(Vector2.dst(pixmap.getWidth()/2, pixmap.getHeight()/2, x, y) < 40){ if(Vector2.dst(pixmap.getWidth()/2, pixmap.getHeight()/2, x, y) < 30){
// block = DefenseBlocks.stonewall; // block = Mathf.choose(ProductionBlocks.stonedrill, DistributionBlocks.conveyor);
//} }
World.tile(x, y).setBlock(block); World.tile(x, y).setBlock(block);
World.tile(x, y).setFloor(floor); World.tile(x, y).setFloor(floor);
@@ -12,18 +12,20 @@ public class Drill extends Block{
protected Block resource; protected Block resource;
protected Item result; protected Item result;
protected int time = 5; protected int time = 5;
protected int capacity = 5;
public Drill(String name) { public Drill(String name) {
super(name); super(name);
update = true; update = true;
//update = false;
//destructible = true;
solid = true; solid = true;
} }
@Override @Override
public void update(Tile tile){ public void update(Tile tile){
//drills can only hold up to 10 items at a time if(tile.floor() == resource && Timers.get(tile, "drill", 60 * time) && tile.entity.totalItems() < capacity){
if(tile.floor() == resource && Timers.get(tile, "drill", 60 * time) && tile.entity.totalItems() < 10){
offloadNear(tile, result); offloadNear(tile, result);
Effects.effect("spark", tile.worldx(), tile.worldy()); Effects.effect("spark", tile.worldx(), tile.worldy());
} }