From 7e6781fb557b2f003ce97157a61c956c905c358e Mon Sep 17 00:00:00 2001 From: GammelJAN Date: Sun, 24 Jan 2021 19:51:05 +0100 Subject: [PATCH] enemies now in tiled map layer - enemies now move in a certain area when not focussing the player --- core/assets/tiledmapData/maps/map1.tmx | 40 +++++-- core/assets/tiledmapData/maps/map2.tmx | 3 +- core/src/com/trs/main/Main.java | 11 ++ core/src/com/trs/main/MapContainer.java | 32 +++++- .../com/trs/main/worldobjects/Hostile.java | 104 ++++++++++++++++-- 5 files changed, 173 insertions(+), 17 deletions(-) diff --git a/core/assets/tiledmapData/maps/map1.tmx b/core/assets/tiledmapData/maps/map1.tmx index 845c9f2..64c5cbd 100644 --- a/core/assets/tiledmapData/maps/map1.tmx +++ b/core/assets/tiledmapData/maps/map1.tmx @@ -1,5 +1,5 @@ - + @@ -94,9 +94,9 @@ 613,614,614,613,612,549,549,612,633,567,635,614,612,613,612,614,549,612,573,550,0,0,0,548,570,633,634,612,567,633,613,635,573,570,633,613,567,612,635,549, 581,581,581,581,581,581,581,581,581,581,581,581,581,581,581,581,581,581,581,582,0,0,0,580,454,633,633,635,573,634,573,549,634,612,614,613,633,573,549,612, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,68,69,70,0,580,581,581,581,454,614,614,633,612,633,633,570,570,573,612,549, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,101,102,0,0,0,0,0,580,581,581,454,613,614,613,567,613,570,612,549, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7434,100,101,102,0,0,0,0,0,580,581,581,454,613,614,613,567,613,570,612,549, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,101,102,0,0,0,0,0,0,0,0,580,454,612,549,573,613,613,634,614, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,101,102,0,0,0,0,0,0,0,0,0,580,454,612,567,613,613,570,634, +0,0,0,0,7434,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,101,102,0,0,0,0,0,0,0,0,0,580,454,612,567,613,613,570,634, 69,69,69,69,69,69,69,69,69,69,69,69,69,69,69,69,69,69,69,69,38,101,37,69,70,0,0,0,0,0,0,0,0,548,613,634,634,614,613,573, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,37,69,70,0,0,0,0,0,0,548,612,612,612,612,573,614, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,37,70,0,0,0,0,0,548,612,633,549,613,614,549, @@ -110,7 +110,7 @@ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,102,0,0,0,548,549,614,613,573,573,573, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,102,0,0,0,548,635,567,549,549,613,614, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,102,0,0,0,548,614,567,612,613,549,613, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,102,0,0,0,548,614,614,549,573,635,613, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7434,100,0,0,0,0,0,0,102,0,0,0,548,614,614,549,573,635,613, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,102,0,0,0,548,613,567,634,633,614,613, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,102,0,0,0,548,612,635,634,612,635,549, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,102,0,0,0,548,633,570,570,612,573,635 @@ -352,6 +352,18 @@ + + + + + + + + + + + + @@ -391,19 +403,19 @@ - + - + - + @@ -421,4 +433,18 @@ + + + + + + + + + + + + + + diff --git a/core/assets/tiledmapData/maps/map2.tmx b/core/assets/tiledmapData/maps/map2.tmx index 95e4760..4d4c574 100644 --- a/core/assets/tiledmapData/maps/map2.tmx +++ b/core/assets/tiledmapData/maps/map2.tmx @@ -1,5 +1,5 @@ - + @@ -122,4 +122,5 @@ + diff --git a/core/src/com/trs/main/Main.java b/core/src/com/trs/main/Main.java index 20ab33b..e5c5fda 100644 --- a/core/src/com/trs/main/Main.java +++ b/core/src/com/trs/main/Main.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.trs.main.view.screens.GameScreen; import com.trs.main.view.screens.InventoryScreen; import com.trs.main.view.screens.MenuScreen; @@ -82,6 +83,16 @@ public class Main extends Game{ } screen.render(Gdx.graphics.getDeltaTime()); + /* + ShapeRenderer renderer = new ShapeRenderer(); + Gdx.gl.glEnable(GL20.GL_BLEND); + Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); + renderer.begin(ShapeRenderer.ShapeType.Filled); + renderer.setColor(51f/255f, 26f/255f, 0f/255f, 0.3f); + renderer.rect(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + renderer.end(); + Gdx.gl.glDisable(GL20.GL_BLEND); + */ } @Override diff --git a/core/src/com/trs/main/MapContainer.java b/core/src/com/trs/main/MapContainer.java index f365546..6121595 100644 --- a/core/src/com/trs/main/MapContainer.java +++ b/core/src/com/trs/main/MapContainer.java @@ -45,6 +45,15 @@ import com.badlogic.gdx.utils.viewport.FitViewport; * Layer 8: NpcRects * id * texture + * Layer 9: EnemyRects + * id + * texture + * isMelee + * level + * hp + * def + * atk + * init * * @author Jan */ @@ -137,6 +146,27 @@ public class MapContainer { stage.addActor(new MapCollisionObject((int) rect.x, (int) rect.y, (int) rect.width, (int) rect.height)); } + // adding the InteractionObjects + for(MapObject object : map.getLayers().get(9).getObjects().getByType(RectangleMapObject.class)){ + Rectangle rect = ((RectangleMapObject) object).getRectangle(); + MapProperties props = object.getProperties(); + + int id = props.get("id", Integer.class); + String texture = props.get("texture", String.class); + + Stats stats = new Stats(); + stats.setLevel(props.get("level", Integer.class)); + stats.setHp(props.get("hp", Integer.class)); + stats.setDef(props.get("def", Integer.class)); + stats.setAtk(props.get("atk", Integer.class)); + stats.setInit(props.get("init", Integer.class)); + + boolean isMelee = props.get("isMelee", Boolean.class); + + Hostile h = new Hostile(rect, rect.getX() + (float)(Math.random()*(rect.getWidth()-64)), rect.getY()+(float)(Math.random()*(rect.getHeight()-64)), id, stats, texture, isMelee); + stage.addActor(h); + } + doors = new Door[tempDoors.size()]; for(int i = 0; i < doors.length; i++) { doors[i] = tempDoors.get(i); @@ -165,8 +195,6 @@ public class MapContainer { stage.addActor(p); - stage.addActor(new Hostile(200, 200, 0, new Stats(), "sprite.png", false)); - stage.addActor(new Hostile(265, 200, 1, new Stats(), "sprite.png", true)); } public void render(float f){ diff --git a/core/src/com/trs/main/worldobjects/Hostile.java b/core/src/com/trs/main/worldobjects/Hostile.java index 7e6aba8..bcfa4b8 100644 --- a/core/src/com/trs/main/worldobjects/Hostile.java +++ b/core/src/com/trs/main/worldobjects/Hostile.java @@ -18,18 +18,25 @@ public class Hostile extends Actor { private Stats stats; private AnimatedSprite sprite; private Rectangle collisionRect; + private Rectangle area; private Circle attackCircle; private Circle attentionCircle; private boolean isMelee; - + private Vector2 POI; + private int facing; + private float speed = 2; + float movementX; + float movementY; + // 0: normal movement, 1: locked onto Player, 2: attacking private int movementState; - public Hostile(float xPos, float yPos, int id, Stats stats, String texture, boolean isMelee) { + public Hostile(Rectangle movementRect, float xPos, float yPos, int id, Stats stats, String texture, boolean isMelee) { this.id = id; this.stats = stats; this.isMelee = isMelee; + this.area = movementRect; Texture tx = new Texture(Gdx.files.internal("textureData/sprites/" + texture)); sprite = new AnimatedSprite(tx, 64, 64, true); @@ -56,9 +63,72 @@ public class Hostile extends Actor { } } } - } + 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(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; + } + + if(StaticMath.calculateDistance(getX(), getY(), POI.x, POI.y) < 10f) { + movementX = 0; + movementY = 0; + } + else { + movementX = movement.x; + movementY = movement.y; + } + + if(movementX == 0 && movementY == 0){ + + } + else if(movementX == 0 && movementY != 0){ + setY(getY()+movementY); + if(collidingWithMapCollisionObject()){ + setY(getY()-movementY); + } + } + else if(movementY == 0 && movementX != 0){ + setX(getX()+movementX); + if(collidingWithMapCollisionObject()){ + setX(getX()-movementX); + } + } + else if(movementX != 0 && movementY != 0){ + setX(getX()+ (movementX)); + if(collidingWithMapCollisionObject()){ + setX(getX() - (movementX)); + } + + setY(getY() + (movementY)); + if(collidingWithMapCollisionObject()){ + setY(getY()- (movementY)); + } + } + + int animationRow = 0; + if(movementX != 0 || movementY != 0) { + animationRow = 8; + } + sprite.setRow(animationRow + facing); + + movementX = 0; + movementY = 0; + } - if(getMovementState() == 1) { + if(getMovementState() == 1 && Main.gamestate != 1) { for(Actor a : getStage().getActors()) { if(a instanceof Player) { if(Intersector.overlaps(getAttackCircle(), ((Player) a).getCollisionRect())) { @@ -69,12 +139,10 @@ public class Hostile extends Actor { } - Vector2 POI = new Vector2(a.getX(), a.getY()); - float speed = 2; + POI = new Vector2(a.getX(), a.getY()); Vector2 movement = new Vector2(speed,0); movement.setAngleRad(StaticMath.calculateAngle(getX(), getY(), POI.x, POI.y)); - int facing; if(movement.angleDeg() < 135 && movement.angleDeg() >= 45) { facing = 0; } @@ -126,6 +194,28 @@ public class Hostile extends Actor { super.positionChanged(); //To change body of generated methods, choose Tools | Templates. } + public boolean collidingWithMapCollisionObject(){ + for(Actor a : getStage().getActors()){ + if(a instanceof MapCollisionObject){ + Rectangle o = new Rectangle(a.getX(), a.getY(), a.getWidth(), a.getHeight()); + if(Intersector.overlaps(collisionRect, o)){ + return true; + } + } + else if(a instanceof MovingNpc && a != this){ + if(Intersector.overlaps(collisionRect, ((MovingNpc)a).collisionRect)){ + return true; + } + } + else if(a instanceof Player){ + if(Intersector.overlaps(collisionRect, ((Player)a).getCollisionRect())){ + return true; + } + } + } + return false; + } + /** * @return the id */