Merge branch 'ai'

This commit is contained in:
Jeasonfire 2015-05-18 00:02:46 +03:00
commit 010d19b414
8 changed files with 202 additions and 20 deletions

View File

@ -32,7 +32,8 @@ public class EntityFactory {
player.add(createPlayerRenderedObject()); player.add(createPlayerRenderedObject());
// Physics // Physics
player.add(new CPhysics().setSize(1.5f, 3.299f).setPosition(pos.x, pos.y).setDirection(initialDirection)); player.add(new CPhysics().setSize(1.5f, 3.299f).setPosition(pos.x, pos.y)
.setDirection(initialDirection).setMoveSpeed(15f));
// Combat // Combat
player.add(new CCombat().setBaseDamage(100).setHealth(1000) player.add(new CCombat().setBaseDamage(100).setHealth(1000)
@ -44,28 +45,33 @@ public class EntityFactory {
return player; return player;
} }
public Entity createDummy(Vector2 pos) { public Entity createEnemy(Vector2 pos) {
return createDummy(pos, Direction.RIGHT); return createEnemy(pos, Direction.RIGHT);
} }
public Entity createDummy(Vector2 pos, Direction initialDirection) { public Entity createEnemy(Vector2 pos, Direction initialDirection) {
Entity dummy = new Entity(); return createEnemy(pos, initialDirection, new CAI().setReactDistance(5).setAIListener(new DummyAI()));
dummy.flags |= Global.FLAG_ALIVE; }
public Entity createEnemy(Vector2 pos, Direction initialDirection, CAI cai) {
Entity enemy = new Entity();
enemy.flags |= Global.FLAG_ALIVE;
// Graphics // Graphics
dummy.add(createPlayerRenderedObject()); enemy.add(createPlayerRenderedObject());
// Physics // Physics
dummy.add(new CPhysics().setSize(1.5f, 3.299f).setPosition(pos.x, pos.y).setDirection(initialDirection)); enemy.add(new CPhysics().setSize(1.5f, 3.299f).setPosition(pos.x, pos.y)
.setDirection(initialDirection).setMoveSpeed(14f));
// Combat // Combat
dummy.add(new CCombat().setBaseDamage(100).setHealth(1000).setSwingCD(.5f) enemy.add(new CCombat().setBaseDamage(100).setHealth(1000).setSwingCD(.5f)
.setCombatListener(new BasicDeathListener())); .setCombatListener(new BasicDeathListener()));
dummy.add(new CAI().setReactDistance(5).setAIListener(new DummyAI())); enemy.add(cai);
AppUtil.engine.addEntity(dummy); AppUtil.engine.addEntity(enemy);
return dummy; return enemy;
} }
private CRenderedObject createPlayerRenderedObject() { private CRenderedObject createPlayerRenderedObject() {
@ -87,11 +93,11 @@ public class EntityFactory {
renderedObject.addChannel("torso"); renderedObject.addChannel("torso");
// Idle animations // Idle animations
SpriteSequence torsoIdleRightSequence = new SpriteSequence(IDLE_ANIMATION_SPEED).addSprite(playerSprites[0][1][0]); SpriteSequence torsoIdleRightSequence = new SpriteSequence(IDLE_ANIMATION_SPEED).addSprite(playerSprites[0][0][0]).addSprite(playerSprites[0][1][0]);
renderedObject.addSequence("Torso-Idle-Right", torsoIdleRightSequence); renderedObject.addSequence("Torso-Idle-Right", torsoIdleRightSequence);
SpriteSequence legsIdleRightSquence = new SpriteSequence(IDLE_ANIMATION_SPEED).addSprite(playerSprites[1][0][0]).addSprite(playerSprites[1][1][0]); SpriteSequence legsIdleRightSquence = new SpriteSequence(IDLE_ANIMATION_SPEED).addSprite(playerSprites[1][0][0]).addSprite(playerSprites[1][1][0]);
renderedObject.addSequence("Legs-Idle-Right", legsIdleRightSquence); renderedObject.addSequence("Legs-Idle-Right", legsIdleRightSquence);
SpriteSequence torsoIdleLeftSequence = new SpriteSequence(IDLE_ANIMATION_SPEED).addSprite(playerSprites[0][1][1]); SpriteSequence torsoIdleLeftSequence = new SpriteSequence(IDLE_ANIMATION_SPEED).addSprite(playerSprites[0][0][1]).addSprite(playerSprites[0][1][1]);
renderedObject.addSequence("Torso-Idle-Left", torsoIdleLeftSequence); renderedObject.addSequence("Torso-Idle-Left", torsoIdleLeftSequence);
SpriteSequence legsIdleLeftSquence = new SpriteSequence(IDLE_ANIMATION_SPEED).addSprite(playerSprites[1][0][1]).addSprite(playerSprites[1][1][1]); SpriteSequence legsIdleLeftSquence = new SpriteSequence(IDLE_ANIMATION_SPEED).addSprite(playerSprites[1][0][1]).addSprite(playerSprites[1][1][1]);
renderedObject.addSequence("Legs-Idle-Left", legsIdleLeftSquence); renderedObject.addSequence("Legs-Idle-Left", legsIdleLeftSquence);

View File

@ -2,7 +2,9 @@ package com.saltosion.gladiator.level.premade;
import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.saltosion.gladiator.components.CAI;
import com.saltosion.gladiator.level.Level; import com.saltosion.gladiator.level.Level;
import com.saltosion.gladiator.listeners.ai.PanicAI;
import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.AppUtil;
import com.saltosion.gladiator.util.Direction; import com.saltosion.gladiator.util.Direction;
import com.saltosion.gladiator.util.Global; import com.saltosion.gladiator.util.Global;
@ -37,7 +39,8 @@ public class Round1Level implements Level {
public void generate() { public void generate() {
AppUtil.levelFactory.createLevelBase(); AppUtil.levelFactory.createLevelBase();
player = AppUtil.entityFactory.createPlayer(new Vector2(-10, 2), Direction.RIGHT); player = AppUtil.entityFactory.createPlayer(new Vector2(-10, 2), Direction.RIGHT);
enemies.add(AppUtil.entityFactory.createDummy(new Vector2(10, 2), Direction.LEFT)); enemies.add(AppUtil.entityFactory.createEnemy(new Vector2(10, 2), Direction.LEFT,
new CAI().setReactDistance(12f).setAIListener(new PanicAI())));
} }
} }

View File

@ -2,7 +2,10 @@ package com.saltosion.gladiator.level.premade;
import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.saltosion.gladiator.components.CAI;
import com.saltosion.gladiator.level.Level; import com.saltosion.gladiator.level.Level;
import com.saltosion.gladiator.listeners.ai.HunterAI;
import com.saltosion.gladiator.listeners.ai.ScaredAI;
import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.AppUtil;
import com.saltosion.gladiator.util.Direction; import com.saltosion.gladiator.util.Direction;
import com.saltosion.gladiator.util.Global; import com.saltosion.gladiator.util.Global;
@ -37,8 +40,10 @@ public class Round2Level implements Level {
public void generate() { public void generate() {
AppUtil.levelFactory.createLevelBase(); AppUtil.levelFactory.createLevelBase();
player = AppUtil.entityFactory.createPlayer(new Vector2(0, 2), Direction.RIGHT); player = AppUtil.entityFactory.createPlayer(new Vector2(0, 2), Direction.RIGHT);
enemies.add(AppUtil.entityFactory.createDummy(new Vector2(10, 2), Direction.LEFT)); enemies.add(AppUtil.entityFactory.createEnemy(new Vector2(10, 2), Direction.LEFT,
enemies.add(AppUtil.entityFactory.createDummy(new Vector2(-10, 2), Direction.RIGHT)); new CAI().setReactDistance(10f).setAIListener(new ScaredAI())));
enemies.add(AppUtil.entityFactory.createEnemy(new Vector2(-10, 2), Direction.RIGHT,
new CAI().setReactDistance(10f).setAIListener(new HunterAI())));
} }
} }

View File

@ -0,0 +1,60 @@
package com.saltosion.gladiator.listeners.ai;
import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.Gdx;
import com.saltosion.gladiator.components.CCombat;
import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.listeners.AIListener;
import com.saltosion.gladiator.util.Direction;
import java.util.ArrayList;
public class HunterAI implements AIListener {
private static final ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
private static final ComponentMapper<CCombat> cm = ComponentMapper.getFor(CCombat.class);
private float lastRush = 0;
private final float rushInterval;
public HunterAI() {
this.rushInterval = 1f;
}
public HunterAI(float rushInterval) {
this.rushInterval = rushInterval;
}
@Override
public void react(ArrayList<Entity> closeEntities, Entity host) {
lastRush += Gdx.graphics.getDeltaTime();
if (lastRush > rushInterval) {
lastRush = 0;
} else {
return;
}
CCombat c0 = cm.get(host);
CPhysics p0 = pm.get(host);
p0.movingLeft = false;
p0.movingRight = false;
c0.inputs.put(Direction.UP, false);
c0.inputs.put(Direction.DOWN, false);
c0.inputs.put(Direction.LEFT, false);
c0.inputs.put(Direction.RIGHT, false);
for (Entity other : closeEntities) {
CPhysics p1 = pm.get(other);
if (p0.getPosition().x + p0.getSize().x / 3 < p1.getPosition().x - p1.getSize().x / 3) {
p0.movingRight = true;
c0.inputs.put(Direction.RIGHT, true);
} else if (p0.getPosition().x - p0.getSize().x / 3 > p1.getPosition().x + p1.getSize().x / 3) {
p0.movingLeft = true;
c0.inputs.put(Direction.LEFT, true);
}
}
}
}

View File

@ -0,0 +1,46 @@
package com.saltosion.gladiator.listeners.ai;
import com.badlogic.ashley.core.Entity;
import com.saltosion.gladiator.components.CCombat;
import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.listeners.AIListener;
import com.saltosion.gladiator.util.Direction;
import java.util.ArrayList;
public class PanicAI implements AIListener {
@Override
public void react(ArrayList<Entity> closeEntities, Entity host) {
CPhysics po = host.getComponent(CPhysics.class);
CCombat co = host.getComponent(CCombat.class);
co.inputs.put(Direction.UP, false);
co.inputs.put(Direction.DOWN, false);
co.inputs.put(Direction.RIGHT, false);
co.inputs.put(Direction.LEFT, false);
if (closeEntities.isEmpty()) {
po.jumping = false;
return;
}
if (po.isGrounded()) {
po.jumping = true;
}
switch ((int) Math.ceil(Math.random() * 4)) {
default:
case 1:
co.inputs.put(Direction.LEFT, true);
break;
case 2:
co.inputs.put(Direction.RIGHT, true);
break;
case 3:
co.inputs.put(Direction.DOWN, true);
break;
case 4:
co.inputs.put(Direction.UP, true);
break;
}
}
}

View File

@ -0,0 +1,47 @@
package com.saltosion.gladiator.listeners.ai;
import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.Gdx;
import com.saltosion.gladiator.components.CCombat;
import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.listeners.AIListener;
import com.saltosion.gladiator.util.Direction;
import java.util.ArrayList;
public class ScaredAI implements AIListener {
private static final ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
private static final ComponentMapper<CCombat> cm = ComponentMapper.getFor(CCombat.class);
@Override
public void react(ArrayList<Entity> closeEntities, Entity host) {
CCombat c0 = cm.get(host);
CPhysics p0 = pm.get(host);
p0.movingLeft = false;
p0.movingRight = false;
c0.inputs.put(Direction.UP, false);
c0.inputs.put(Direction.DOWN, false);
c0.inputs.put(Direction.LEFT, false);
c0.inputs.put(Direction.RIGHT, false);
Direction dir = Direction.DOWN;
if (Math.random() < 0.5) {
dir = Direction.UP;
}
if (Math.random() < 2 * Gdx.graphics.getDeltaTime()) {
c0.inputs.put(dir, true);
}
for (Entity other : closeEntities) {
CPhysics p1 = pm.get(other);
if (p0.getPosition().x + p0.getSize().x / 3 < p1.getPosition().x - p1.getSize().x / 3) {
p0.movingLeft = true;
} else if (p0.getPosition().x - p0.getSize().x / 3 > p1.getPosition().x + p1.getSize().x / 3) {
p0.movingRight = true;
}
}
}
}

View File

@ -9,6 +9,7 @@ import com.badlogic.ashley.utils.ImmutableArray;
import com.saltosion.gladiator.components.CAI; import com.saltosion.gladiator.components.CAI;
import com.saltosion.gladiator.components.CCombat; import com.saltosion.gladiator.components.CCombat;
import com.saltosion.gladiator.components.CPhysics; import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.listeners.AIListener;
import java.util.ArrayList; import java.util.ArrayList;
public class AISystem extends EntitySystem { public class AISystem extends EntitySystem {
@ -49,7 +50,10 @@ public class AISystem extends EntitySystem {
reactEntities.add(entities.get(j)); reactEntities.add(entities.get(j));
} }
} }
cai.getAIListener().react(reactEntities, entities.get(i)); AIListener listener = cai.getAIListener();
if (listener != null) {
listener.react(reactEntities, entities.get(i));
}
} }
} }

View File

@ -18,6 +18,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.saltosion.gladiator.components.CAI;
import com.saltosion.gladiator.components.CCombat; import com.saltosion.gladiator.components.CCombat;
import com.saltosion.gladiator.components.CParticle; import com.saltosion.gladiator.components.CParticle;
import com.saltosion.gladiator.components.CPhysics; import com.saltosion.gladiator.components.CPhysics;
@ -28,7 +29,6 @@ import com.saltosion.gladiator.gui.nodes.TextNode;
import com.saltosion.gladiator.gui.properties.TextProperty; import com.saltosion.gladiator.gui.properties.TextProperty;
import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.AppUtil;
import com.saltosion.gladiator.util.Global; import com.saltosion.gladiator.util.Global;
import com.saltosion.gladiator.util.Log;
import com.saltosion.gladiator.util.Name; import com.saltosion.gladiator.util.Name;
import com.saltosion.gladiator.util.SpriteLoader; import com.saltosion.gladiator.util.SpriteLoader;
import com.saltosion.gladiator.util.SpriteSequence; import com.saltosion.gladiator.util.SpriteSequence;
@ -40,6 +40,7 @@ public class RenderingSystem extends EntitySystem {
private final ComponentMapper<CRenderedObject> rom = ComponentMapper.getFor(CRenderedObject.class); private final ComponentMapper<CRenderedObject> rom = ComponentMapper.getFor(CRenderedObject.class);
private final ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class); private final ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
private final ComponentMapper<CCombat> cm = ComponentMapper.getFor(CCombat.class); private final ComponentMapper<CCombat> cm = ComponentMapper.getFor(CCombat.class);
private final ComponentMapper<CAI> aim = ComponentMapper.getFor(CAI.class);
private final ComponentMapper<CParticle> pam = ComponentMapper.getFor(CParticle.class); private final ComponentMapper<CParticle> pam = ComponentMapper.getFor(CParticle.class);
private ImmutableArray<Entity> entities; private ImmutableArray<Entity> entities;
@ -53,7 +54,7 @@ public class RenderingSystem extends EntitySystem {
public int screenWidth = 0; public int screenWidth = 0;
private boolean debug = false; private boolean debug = false;
private final Color debugColor = new Color(0, 1, 0, 1); private final Color debugColor = new Color(0, 1, 0, 1), debugAIColor = new Color(1, 0, 0, 1);
private float deltaDelay = 0; private float deltaDelay = 0;
private double deltaAvgSum; private double deltaAvgSum;
@ -326,6 +327,16 @@ public class RenderingSystem extends EntitySystem {
debugRenderer.line(x1, y0, x1, y1); debugRenderer.line(x1, y0, x1, y1);
debugRenderer.line(x1, y1, x0, y1); debugRenderer.line(x1, y1, x0, y1);
debugRenderer.line(x0, y1, x0, y0); debugRenderer.line(x0, y1, x0, y0);
CAI ai = aim.get(entities.get(i));
if (ai == null) {
continue;
}
float x = physics.getPosition().x + getCameraOffset(playerPhys, physics).x;
float y = physics.getPosition().y + getCameraOffset(playerPhys, physics).y;
debugRenderer.setColor(debugAIColor);
debugRenderer.circle(x, y, ai.getReactDistance());
} }
debugRenderer.end(); debugRenderer.end();
} }