private static Effect ParseMovementBehavior(XElement prop, Axis axis) { Effect action; float?mag = prop.TryAttribute <float?>("magnitude"); if (mag != 0) { XAttribute dirattr = prop.Attribute("direction"); string direction; direction = dirattr == null ? "Same" : dirattr.Value; switch (direction) { case "Up": action = entity => { MovementComponent mov = entity.GetComponent <MovementComponent>(); if (mov != null) { mov.VelocityY = -1 * (mag ?? Math.Abs(mov.VelocityY)); } }; break; case "Down": action = entity => { MovementComponent mov = entity.GetComponent <MovementComponent>(); if (mov != null) { mov.VelocityY = (mag ?? Math.Abs(mov.VelocityY)); } }; break; case "Left": action = entity => { MovementComponent mov = entity.GetComponent <MovementComponent>(); if (mov != null) { mov.VelocityX = -mag ?? -1 * Math.Abs(mov.VelocityX); } }; break; case "Right": action = entity => { MovementComponent mov = entity.GetComponent <MovementComponent>(); if (mov != null) { mov.VelocityX = mag ?? Math.Abs(mov.VelocityX); } }; break; case "Same": action = entity => { if (mag == null) { return; } float fmag = mag ?? 0; MovementComponent mov = entity.GetComponent <MovementComponent>(); if (mov == null) { return; } Direction dir = mov.Direction; if (axis != Axis.Y) { mov.VelocityX = (dir == Direction.Right) ? fmag : ((dir == Direction.Left) ? -fmag : 0); } if (axis != Axis.X) { mov.VelocityY = (dir == Direction.Down) ? fmag : ((dir == Direction.Up) ? -fmag : 0); } }; break; case "Reverse": action = entity => { if (mag == null) { return; } float fmag = mag ?? 0; MovementComponent mov = entity.GetComponent <MovementComponent>(); if (mov == null) { return; } Direction dir = mov.Direction; if (axis != Axis.Y) { mov.VelocityX = (dir == Direction.Left) ? fmag : ((dir == Direction.Right) ? -fmag : 0); } if (axis != Axis.X) { mov.VelocityY = (dir == Direction.Up) ? fmag : ((dir == Direction.Down) ? -fmag : 0); } }; break; case "Inherit": action = entity => { MovementComponent mov = entity.GetComponent <MovementComponent>(); if (mov == null) { return; } if (entity.Parent != null) { Direction dir = entity.Parent.Direction; if (axis != Axis.Y) { mov.VelocityX = (dir == Direction.Right) ? (mag ?? Math.Abs(mov.VelocityX)) : ((dir == Direction.Left) ? (-mag ?? -1 * Math.Abs(mov.VelocityX)) : 0); } if (axis != Axis.X) { mov.VelocityY = (dir == Direction.Down) ? (mag ?? Math.Abs(mov.VelocityY)) : ((dir == Direction.Up) ? (-mag ?? -1 * Math.Abs(mov.VelocityY)) : 0); } } else { mov.VelocityY = 0; } }; break; case "Input": action = entity => { MovementComponent mov = entity.GetComponent <MovementComponent>(); InputComponent input = entity.GetComponent <InputComponent>(); if (mov == null || input == null) { return; } if (axis != Axis.Y) { if (input.Left) { mov.VelocityX = -mag ?? -1 * Math.Abs(mov.VelocityX); } else if (input.Right) { mov.VelocityX = mag ?? Math.Abs(mov.VelocityX); } } if (axis != Axis.X) { if (input.Down) { mov.VelocityY = mag ?? Math.Abs(mov.VelocityY); } else if (input.Up) { mov.VelocityY = -mag ?? -1 * Math.Abs(mov.VelocityY); } else { mov.VelocityY = 0; } } }; break; case "Player": action = entity => { MovementComponent mov = entity.GetComponent <MovementComponent>(); PositionComponent pos = entity.GetComponent <PositionComponent>(); if (mov == null || pos == null) { return; } GameEntity player = entity.Entities.GetEntityById("Player"); PositionComponent playerPos = player.GetComponent <PositionComponent>(); if (axis == Axis.X) { if (pos.Position.X > playerPos.Position.X) { mov.VelocityX = -mag ?? -1 * Math.Abs(mov.VelocityX); } else if (pos.Position.X < playerPos.Position.X) { mov.VelocityX = mag ?? Math.Abs(mov.VelocityX); } } else if (axis == Axis.Y) { if (pos.Position.Y > playerPos.Position.Y) { mov.VelocityY = -mag ?? -1 * Math.Abs(mov.VelocityY); } else if (pos.Position.Y < playerPos.Position.Y) { mov.VelocityY = mag ?? Math.Abs(mov.VelocityY); } } else { float dx = playerPos.Position.X - pos.Position.X; float dy = playerPos.Position.Y - pos.Position.Y; double hyp = Math.Pow(dx, 2) + Math.Pow(dy, 2); hyp = Math.Pow(hyp, 0.5); mov.VelocityX = (float)(mag * dx / hyp); mov.VelocityY = (float)(mag * dy / hyp); } }; break; default: action = new Effect(entity => { }); break; } } else { if (axis == Axis.X) { action = entity => { MovementComponent mov = entity.GetComponent <MovementComponent>(); if (mov != null) { mov.VelocityX = 0; } } } ; else { action = entity => { MovementComponent mov = entity.GetComponent <MovementComponent>(); if (mov != null) { mov.VelocityY = 0; } } }; } return(action); } }
public static Effect ParsePositionBehavior(XElement prop, Axis axis) { Effect action = e => { }; XAttribute baseAttr = prop.Attribute("base"); if (baseAttr != null) { string base_str = baseAttr.Value; if (base_str == "Inherit") { action = entity => { PositionComponent pos = entity.GetComponent <PositionComponent>(); if (pos != null && entity.Parent != null) { PositionComponent parentPos = entity.Parent.GetComponent <PositionComponent>(); if (parentPos != null) { pos.SetPosition(axis == Axis.X ? new PointF(parentPos.Position.X, pos.Position.Y) : new PointF(pos.Position.X, parentPos.Position.Y)); } } }; } else { float baseVal = prop.TryAttribute <float>("base"); if (axis == Axis.X) { action = entity => { PositionComponent pos = entity.GetComponent <PositionComponent>(); if (pos != null) { pos.SetPosition(new PointF(baseVal, pos.Position.Y)); } } } ; else { action = entity => { PositionComponent pos = entity.GetComponent <PositionComponent>(); if (pos != null) { pos.SetPosition(new PointF(pos.Position.X, baseVal)); } } }; } } if (prop.Attribute("offset") != null) { float offset = prop.TryAttribute <float>("offset"); XAttribute offdirattr = prop.RequireAttribute("direction"); if (offdirattr.Value == "Inherit") { action += entity => { PositionComponent pos = entity.GetComponent <PositionComponent>(); if (pos != null && entity.Parent != null) { Direction offdir = entity.Parent.Direction; switch (offdir) { case Direction.Down: pos.SetPosition(new PointF(pos.Position.X, pos.Position.Y + offset)); break; case Direction.Up: pos.SetPosition(new PointF(pos.Position.X, pos.Position.Y - offset)); break; case Direction.Left: pos.SetPosition(new PointF(pos.Position.X - offset, pos.Position.Y)); break; case Direction.Right: pos.SetPosition(new PointF(pos.Position.X + offset, pos.Position.Y)); break; } } }; } else if (offdirattr.Value == "Input") { action += entity => { PositionComponent pos = entity.GetComponent <PositionComponent>(); InputComponent input = entity.GetComponent <InputComponent>(); if (input != null && pos != null) { if (axis == Axis.Y) { if (input.Down) { pos.SetPosition(new PointF(pos.Position.X, pos.Position.Y + offset)); } else if (input.Up) { pos.SetPosition(new PointF(pos.Position.X, pos.Position.Y - offset)); } } else { if (input.Left) { pos.SetPosition(new PointF(pos.Position.X - offset, pos.Position.Y)); } else if (input.Right || (!input.Up && !input.Down)) { pos.SetPosition(new PointF(pos.Position.X + offset, pos.Position.Y)); } } } }; } else { Direction offdir; try { offdir = (Direction)Enum.Parse(typeof(Direction), offdirattr.Value, true); } catch { throw new GameXmlException(offdirattr, "Position offset direction was not valid!"); } switch (offdir) { case Direction.Left: action += entity => { PositionComponent pos = entity.GetComponent <PositionComponent>(); if (pos != null) { pos.SetPosition(new PointF(pos.Position.X - offset, pos.Position.Y)); } }; break; case Direction.Right: action += entity => { PositionComponent pos = entity.GetComponent <PositionComponent>(); if (pos != null) { pos.SetPosition(new PointF(pos.Position.X + offset, pos.Position.Y)); } }; break; case Direction.Down: action += entity => { PositionComponent pos = entity.GetComponent <PositionComponent>(); if (pos != null) { pos.SetPosition(new PointF(pos.Position.X, pos.Position.Y + offset)); } }; break; case Direction.Up: action += entity => { PositionComponent pos = entity.GetComponent <PositionComponent>(); if (pos != null) { pos.SetPosition(new PointF(pos.Position.X, pos.Position.Y - offset)); } }; break; } } } return(action); } }