enemies now in tiled map layer - enemies now move in a certain area when not focussing the player

master
GammelJAN 5 years ago
parent 5101c5931c
commit 7e6781fb55

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="40" height="50" tilewidth="32" tileheight="32" infinite="0" nextlayerid="10" nextobjectid="103">
<map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="40" height="50" tilewidth="32" tileheight="32" infinite="0" nextlayerid="11" nextobjectid="116">
<tileset firstgid="1" source="../tilesets/terrain.tsx"/>
<tileset firstgid="2049" source="../tilesets/village.tsx"/>
<tileset firstgid="3849" source="../tilesets/crops.tsx"/>
@ -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 @@
<object id="85" x="32" y="640" width="64" height="32"/>
<object id="86" x="96" y="672" width="64" height="32"/>
<object id="96" x="832" y="256" width="128" height="32"/>
<object id="103" x="0" y="640" width="32" height="32"/>
<object id="104" x="0" y="672" width="96" height="32"/>
<object id="105" x="0" y="704" width="416" height="192"/>
<object id="106" x="416" y="736" width="160" height="160"/>
<object id="107" x="576" y="768" width="32" height="128"/>
<object id="108" x="768" y="768" width="32" height="128"/>
<object id="109" x="800" y="768" width="64" height="160"/>
<object id="110" x="864" y="800" width="64" height="128"/>
<object id="111" x="928" y="800" width="352" height="160"/>
<object id="112" x="1024" y="960" width="256" height="32"/>
<object id="113" x="1056" y="992" width="224" height="32"/>
<object id="114" x="1088" y="1024" width="192" height="576"/>
</objectgroup>
<objectgroup id="7" name="DoorRects">
<object id="88" x="150.875" y="191.75" width="52.625" height="43.625">
@ -391,19 +403,19 @@
<object id="92" x="608" y="960" width="32" height="32">
<properties>
<property name="id" type="int" value="1"/>
<property name="texture" value="sign.png"/>
<property name="texture" value="-"/>
</properties>
</object>
<object id="93" x="128" y="1024" width="32" height="32">
<properties>
<property name="id" type="int" value="0"/>
<property name="texture" value="sign.png"/>
<property name="texture" value="-"/>
</properties>
</object>
<object id="95" x="672" y="1472" width="32" height="32">
<properties>
<property name="id" type="int" value="2"/>
<property name="texture" value="sign.png"/>
<property name="texture" value="-"/>
</properties>
</object>
</objectgroup>
@ -421,4 +433,18 @@
</properties>
</object>
</objectgroup>
<objectgroup id="10" name="EnemyRects">
<object id="115" x="736" y="1216" width="160" height="96">
<properties>
<property name="atk" type="int" value="2"/>
<property name="def" type="int" value="3"/>
<property name="hp" type="int" value="20"/>
<property name="id" type="int" value="0"/>
<property name="init" type="int" value="2"/>
<property name="isMelee" type="bool" value="true"/>
<property name="level" type="int" value="1"/>
<property name="texture" value="sprite.png"/>
</properties>
</object>
</objectgroup>
</map>

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="12" height="10" tilewidth="32" tileheight="32" infinite="0" nextlayerid="10" nextobjectid="20">
<map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="12" height="10" tilewidth="32" tileheight="32" infinite="0" nextlayerid="11" nextobjectid="20">
<tileset firstgid="1" source="../tilesets/village.tsx"/>
<tileset firstgid="1801" source="../tilesets/worldborder.tsx"/>
<tileset firstgid="1809" source="../tilesets/furniture.tsx"/>
@ -122,4 +122,5 @@
</properties>
</object>
</objectgroup>
<objectgroup id="10" name="EnemyRects"/>
</map>

@ -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

@ -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){

@ -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
*/

Loading…
Cancel
Save