1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-24 01:09:23 +01:00

added a direction2 class

This commit is contained in:
Ellpeck 2019-09-04 13:05:44 +02:00
parent cec3151ed0
commit f4658aede0
3 changed files with 106 additions and 24 deletions

View file

@ -3,6 +3,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MLEM.Animations;
using MLEM.Misc;
using MLEM.Startup;
using MLEM.Textures;
@ -10,7 +11,7 @@ namespace Demos {
public class AnimationDemo : Demo {
private SpriteAnimationGroup group;
private int facing;
private Direction2 facing = Direction2.Down;
public AnimationDemo(MlemGame game) : base(game) {
}
@ -31,16 +32,16 @@ namespace Demos {
// using a group isn't necessary, but highly recommended for things like character animations as it makes
// it very easy to have different animations play at different times
this.group = new SpriteAnimationGroup();
// for example, the down animation should only play when we're facing down (0 in this case)
this.group.Add(downAnim, () => this.facing == 0);
this.group.Add(upAnim, () => this.facing == 1);
this.group.Add(leftAnim, () => this.facing == 2);
this.group.Add(rightAnim, () => this.facing == 3);
// for example, the down animation should only play when we're facing down
this.group.Add(downAnim, () => this.facing == Direction2.Down);
this.group.Add(upAnim, () => this.facing == Direction2.Up);
this.group.Add(leftAnim, () => this.facing == Direction2.Left);
this.group.Add(rightAnim, () => this.facing == Direction2.Right);
// you can also add a priority to an animation in the group (10 in this case, which is higher than the default of 0)
// if two animations' playing conditions are both true, then the one with the higher priority will be picked to play
// in this instance, a standing "animation" is displayed when we're facing down and also holding the space key
this.group.Add(new SpriteAnimation(1F, tex, new Rectangle(0, 0, 8, 8)) {Name = "DownStanding"}, () => this.facing == 0 && this.InputHandler.IsKeyDown(Keys.Space), 10);
this.group.Add(new SpriteAnimation(1F, tex, new Rectangle(0, 0, 8, 8)) {Name = "DownStanding"}, () => this.facing == Direction2.Down && this.InputHandler.IsKeyDown(Keys.Space), 10);
// you can also add a callback to see when the animation used changes
this.group.OnAnimationChanged += (anim, newAnim) => {
@ -52,13 +53,13 @@ namespace Demos {
base.Update(gameTime);
if (this.InputHandler.IsKeyDown(Keys.Down))
this.facing = 0;
this.facing = Direction2.Down;
else if (this.InputHandler.IsKeyDown(Keys.Up))
this.facing = 1;
this.facing = Direction2.Up;
else if (this.InputHandler.IsKeyDown(Keys.Left))
this.facing = 2;
this.facing = Direction2.Left;
else if (this.InputHandler.IsKeyDown(Keys.Right))
this.facing = 3;
this.facing = Direction2.Right;
// update the animation group
// if not using a group, just update the animation itself here

91
MLEM/Misc/Direction2.cs Normal file
View file

@ -0,0 +1,91 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
namespace MLEM.Misc {
public enum Direction2 {
Up,
Right,
Down,
Left,
UpRight,
DownRight,
DownLeft,
UpLeft,
None
}
public static class Direction2Helper {
public static readonly Direction2[] All = EnumHelper.GetValues<Direction2>().ToArray();
public static readonly Direction2[] Adjacent = All.Where(IsAdjacent).ToArray();
public static readonly Direction2[] Diagonals = All.Where(IsDiagonal).ToArray();
public static readonly Direction2[] AllExceptNone = All.Where(dir => dir != Direction2.None).ToArray();
public static bool IsAdjacent(this Direction2 dir) {
return dir <= Direction2.Left;
}
public static bool IsDiagonal(this Direction2 dir) {
return dir >= Direction2.UpRight && dir <= Direction2.UpLeft;
}
public static Point Offset(this Direction2 dir) {
switch (dir) {
case Direction2.Up:
return new Point(0, -1);
case Direction2.Right:
return new Point(1, 0);
case Direction2.Down:
return new Point(0, 1);
case Direction2.Left:
return new Point(-1, 0);
case Direction2.UpRight:
return new Point(1, -1);
case Direction2.DownRight:
return new Point(1, 1);
case Direction2.DownLeft:
return new Point(-1, 1);
case Direction2.UpLeft:
return new Point(-1, 1);
default:
return Point.Zero;
}
}
public static IEnumerable<Point> Offsets(this IEnumerable<Direction2> directions) {
foreach (var dir in directions)
yield return dir.Offset();
}
public static Direction2 Opposite(this Direction2 dir) {
switch (dir) {
case Direction2.Up:
return Direction2.Down;
case Direction2.Right:
return Direction2.Left;
case Direction2.Down:
return Direction2.Up;
case Direction2.Left:
return Direction2.Right;
case Direction2.UpRight:
return Direction2.DownLeft;
case Direction2.DownRight:
return Direction2.UpLeft;
case Direction2.DownLeft:
return Direction2.UpRight;
case Direction2.UpLeft:
return Direction2.DownRight;
default:
return Direction2.None;
}
}
}
}

View file

@ -1,23 +1,13 @@
using System;
using System.Linq;
using Microsoft.Xna.Framework;
using MLEM.Misc;
namespace MLEM.Pathfinding {
public class AStar2 : AStar<Point> {
private static readonly Point[] AdjacentDirs = {
new Point(1, 0),
new Point(-1, 0),
new Point(0, 1),
new Point(0, -1)
};
private static readonly Point[] AllDirs = AdjacentDirs.Concat(new[] {
new Point(1, 1),
new Point(-1, 1),
new Point(1, -1),
new Point(-1, -1)
}).ToArray();
private static readonly Point[] AdjacentDirs = Direction2Helper.Adjacent.Offsets().ToArray();
private static readonly Point[] AllDirs = Direction2Helper.All.Offsets().ToArray();
public AStar2(GetCost defaultCostFunction, bool defaultAllowDiagonals, float defaultCost = 1, int defaultMaxTries = 10000) :
base(AllDirs, AdjacentDirs, defaultCostFunction, defaultAllowDiagonals, defaultCost, defaultMaxTries) {