using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace SemiColinGames { class NPC { private Point position; private const int spriteWidth = 96; private const int spriteHeight = 81; private const int spriteCenterYOffset = 3; public NPC(Point position) { this.position = position; } public int Facing { get; private set; } = 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 (!foundBox) { Facing *= -1; } position.X = desiredX; } public void Draw(SpriteBatch spriteBatch) { Rectangle textureSource = Sprites.Executioner.GetTextureSource( "run", (int) Clock.ModelTime.TotalMilliseconds); // TODO: move this into Sprite metadata. Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset); SpriteEffects effect = Facing == 1 ? SpriteEffects.None : SpriteEffects.FlipHorizontally; Color color = Color.White; spriteBatch.Draw(Textures.Executioner.Get, position.ToVector2(), textureSource, color, 0f, spriteCenter, Vector2.One, effect, 0f); } } }