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

View File

@ -79,7 +79,7 @@ project(":core") {
apply plugin: "java"
dependencies {
compile 'com.github.anuken:ucore:91e4e11010'
compile 'com.github.anuken:ucore:c498e5920a'
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
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

View File

@ -2,6 +2,7 @@ package io.anuke.mindustry.entities.enemies;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import io.anuke.mindustry.Shaders;
@ -15,6 +16,7 @@ import io.anuke.mindustry.world.World;
import io.anuke.ucore.core.*;
import io.anuke.ucore.entities.*;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Tmp;
public class Enemy extends DestructibleEntity{
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);
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());
if(Timers.get(this, "target", 15)){
@ -66,9 +84,7 @@ public class Enemy extends DestructibleEntity{
//no tile found
if(target == null){
target = Entities.getClosest(x, y, range, e->{
return e instanceof Player;
});
target = Entities.getClosest(entities, x, y, range, e -> e instanceof Player);
}
}

View File

@ -35,7 +35,7 @@ public class LevelDialog extends FloatingDialog{
for(int i = 0; i < Map.values().length; i ++){
Map map = Map.values()[i];
if(!map.visible) continue;
if(!map.visible && !Vars.debug) continue;
if(i % maxwidth == 0){
maps.row();
@ -48,18 +48,18 @@ public class LevelDialog extends FloatingDialog{
.pad(3f).units(Unit.dp);
inset.pack();
float images = Unit.dp.inPixels(154);
float images = 154f;
ImageButton image = new ImageButton(new TextureRegion(World.getTexture(map)), "togglemap");
image.row();
image.add(inset).width(images+6);
image.add(inset).width(images+6).units(Unit.dp);
image.clicked(()->{
selectedMap = map;
hide();
Vars.control.playMap(selectedMap);
});
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);

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
//TODO enable
if(destructible && !update && !GameState.is(State.paused)){
// tile.entity.update();
tile.entity.update();
}
}

View File

@ -2,6 +2,7 @@ package io.anuke.mindustry.world;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ObjectMap;
import io.anuke.mindustry.Vars;
@ -94,9 +95,9 @@ public class Generator{
}
//preformance debugging
//if(Vector2.dst(pixmap.getWidth()/2, pixmap.getHeight()/2, x, y) < 40){
// block = DefenseBlocks.stonewall;
//}
if(Vector2.dst(pixmap.getWidth()/2, pixmap.getHeight()/2, x, y) < 30){
// block = Mathf.choose(ProductionBlocks.stonedrill, DistributionBlocks.conveyor);
}
World.tile(x, y).setBlock(block);
World.tile(x, y).setFloor(floor);

View File

@ -12,18 +12,20 @@ public class Drill extends Block{
protected Block resource;
protected Item result;
protected int time = 5;
protected int capacity = 5;
public Drill(String name) {
super(name);
update = true;
//update = false;
//destructible = true;
solid = true;
}
@Override
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() < 10){
if(tile.floor() == resource && Timers.get(tile, "drill", 60 * time) && tile.entity.totalItems() < capacity){
offloadNear(tile, result);
Effects.effect("spark", tile.worldx(), tile.worldy());
}