include multiple NPCs, have them turn around based on platforms.

This commit is contained in:
Colin McMillen 2020-03-05 16:17:57 -05:00
parent 5b82f4d646
commit 53c6d8483f
3 changed files with 25 additions and 11 deletions

View File

@ -15,14 +15,24 @@ namespace SemiColinGames {
public int Facing { get; private set; } = 1;
public void Update(float modelTime) {
if (Facing == 1 && position.X > 16 * 39) {
Facing = -1;
public void Update(float modelTime, AABB[] collisionTargets) {
int moveSpeed = 120;
int desiredX = position.X + (int) (moveSpeed * Facing * modelTime);
// TODO: define the box modularly & correctly.
AABB npcBox = new AABB(new Vector2(desiredX, position.Y), new Vector2(11, 33));
Debug.AddRect(npcBox, Color.Cyan);
bool foundBox = false;
foreach (AABB box in collisionTargets) {
if (box.Intersect(npcBox) != null) {
foundBox = true;
break;
}
if (Facing == -1 && position.X < 16 * 24) {
Facing = 1;
}
position.X += (int) (120 * Facing * modelTime);
if (!foundBox) {
Facing *= -1;
}
position.X = desiredX;
}
public void Draw(SpriteBatch spriteBatch) {

View File

@ -48,7 +48,7 @@ namespace SemiColinGames {
public Point Position { get { return position; } }
public void Update(float modelTime, History<Input> input, AABB[] collisionTargets) {
public void Update(float modelTime, AABB[] collisionTargets, History<Input> input) {
AABB BoxOffset(Point position, int yOffset) {
return new AABB(new Vector2(position.X, position.Y + yOffset), halfSize);
}

View File

@ -90,7 +90,7 @@ namespace SemiColinGames {
public const int TileSize = 16;
readonly Tile[] tiles;
readonly Tile[] decorations;
readonly NPC[] npcs = new NPC[1];
readonly NPC[] npcs = new NPC[4];
// Size of World in terms of tile grid.
private readonly int tileWidth;
@ -109,7 +109,11 @@ namespace SemiColinGames {
public World(string levelSpecification) {
Player = new Player();
npcs[0] = new NPC(new Point(16 * 38, 16 * 12));
npcs[0] = new NPC(new Point(16 * 8, 16 * 6));
npcs[1] = new NPC(new Point(16 * 28, 16 * 6));
npcs[2] = new NPC(new Point(16 * 36, 16 * 6));
npcs[3] = new NPC(new Point(16 * 36, 16 * 12));
var tilesList = new List<Tile>();
var decorationsList = new List<Tile>();
string[] worldDesc = levelSpecification.Substring(1).Split('\n');
@ -158,9 +162,9 @@ namespace SemiColinGames {
}
public void Update(float modelTime, History<Input> input) {
Player.Update(modelTime, input, CollisionTargets);
Player.Update(modelTime, CollisionTargets, input);
foreach (NPC npc in npcs) {
npc.Update(modelTime);
npc.Update(modelTime, CollisionTargets);
}
if (Player.Health <= 0) {
Reset();