protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); Globals.content = Content; Globals.LoadContent(GraphicsDevice); Textures.LoadTextures(); Cell.CreateGrid(new Point(-2048, -2048), 400, 400); Rail.LoadContent(); Level.LoadContent(); Input.LoadInput(); Globals.LoadLevel(); Globals.level.SpawnMapRails(); Globals.player = new Player(); GUI.LoadGUI(); }
public void Teleport(Portal toPortal) { position = toPortal.ToVector2(); previousRail.CartStopApproaching(); previousRail = (Rail)cell.item; cell = Cell.GetCell(position); moveDirection = MoveDirection.None; CalculateTravelDirection(); SetFutureCell(); if (cell.item is Rail rl) { rl.CartStartApproaching(); } futureRail.CartStartApproaching(); //previousRail.CartStopApproaching(); }
public Rail Connect(Node other) { float x = Location.X - other.Location.X; x *= x; float y = Location.Y - other.Location.Y; y *= y; Rail rail = new Rail(this, other, (float)Math.Sqrt(x + y)); rails.Add(rail); other.rails.Add(rail); nodes.Add(other); other.nodes.Add(this); return(rail); }
public List <Stack <Rail> > Find(Node goal, List <Stack <Rail> > results = null, Stack <Rail> path = null, HashSet <Guid> visited = null) { //prevent null exception on new path path ??= new Stack <Rail>(); visited ??= new HashSet <Guid>(); results ??= new List <Stack <Rail> >(); if (visited.Contains(Guid)) { return(results); } visited.Add(Guid); //check if goal reached if (goal.Guid == Guid) { //copy current path int len = path.Count; Rail[] _path = new Rail[len]; Array.Copy(path.ToArray(), _path, len); //save current path as solution results.Add(new Stack <Rail>(_path)); } else { //recursive search on all connected rails for (int i = 0; i < nodes.Count; i++) { if (visited.Contains(rails[i].Guid)) { continue; } visited.Add(rails[i].Guid); path.Push(rails[i]); nodes[i].Find(goal, results, path, visited); visited.Remove(rails[i].Guid); path.Pop(); } } visited.Remove(Guid); return(results); }
public void Move(GameTime gameTime) { move = Vector2.Zero; if (moveDirection == MoveDirection.None) { Destroy(); } if (futureRail != null) // move { position = Vector2.Lerp(cell.ToVector2(), futureRail.cell.ToVector2(), t); //velocity = MathHelper.Lerp(startVel, endVel, acceleration * -t); velocity += acceleration * (float)gameTime.ElapsedGameTime.TotalSeconds * 0.1f; //velocity *= (float)gameTime.ElapsedGameTime.TotalSeconds; if (velocity <= endVel) // the cart is no longer travelling { velocity = endVel; } t += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds * 0.1f; if (t >= 1) { t = 0; switch (moveDirection) { case MoveDirection.None: break; case MoveDirection.Up: rotation = MathHelper.Pi; break; case MoveDirection.Down: rotation = 0; break; case MoveDirection.Right: rotation = -MathHelper.PiOver2; break; case MoveDirection.Left: rotation = MathHelper.PiOver2; break; default: break; } previousRail = (Rail)cell.item; cell = Cell.GetCell(position); position = futureRail.cell.ToVector2(); if (cell.item is Rail rail) { rail.CartPassOver(this); } SetFutureCell(); if (previousRail != null) { previousRail.CartStopApproaching(); } if (futureRail != null) { futureRail.CartStartApproaching(); } if (cell.item is Rail rl) { rl.CartStartApproaching(); } if (futureRail == null) { CalculateTravelDirection(); SetFutureCell(); if (futureRail != null) { futureRail.CartStartApproaching(); } } } } else { CalculateTravelDirection(); SetFutureCell(); if (futureRail == null) { Destroy(); } } }
public void CalculateTravelDirection() { if (cell.item is StraightRail rl) { Cell searchCell = Cell.GetCell(cell, 1, 0); if (searchCell.item is Rail) // track found to the right { Rail rail = (Rail)searchCell.item; if (moveDirection == MoveDirection.None && rail is StraightRail && ((StraightRail)rail).orientation == StraightRail.Orientation.Horizontal && rl.orientation == StraightRail.Orientation.Horizontal) { moveDirection = MoveDirection.Right; if (Globals.debugMessages) { Console.WriteLine("Rail found at: Right"); } return; } else if (rail is TurnRail) { if (((TurnRail)rail).turnDir == TurnRail.TurnDirection.LeftUp || ((TurnRail)rail).turnDir == TurnRail.TurnDirection.DownLeft) { moveDirection = MoveDirection.Right; return; } } } searchCell = Cell.GetCell(cell, -1, 0); if (searchCell.item is Rail) // track found to the left { Rail rail = (Rail)searchCell.item; if (moveDirection == MoveDirection.None && rail is StraightRail && ((StraightRail)rail).orientation == StraightRail.Orientation.Horizontal && rl.orientation == StraightRail.Orientation.Horizontal) { moveDirection = MoveDirection.Left; if (Globals.debugMessages) { Console.WriteLine("Rail found at: Left"); } return; } else if (rail is TurnRail) { if (((TurnRail)rail).turnDir == TurnRail.TurnDirection.RightUp || ((TurnRail)rail).turnDir == TurnRail.TurnDirection.DownRight) { moveDirection = MoveDirection.Left; return; } } } searchCell = Cell.GetCell(cell, 0, -1); if (searchCell.item is Rail) // track found up top { Rail rail = (Rail)searchCell.item; if (moveDirection == MoveDirection.None && rail is StraightRail && ((StraightRail)rail).orientation == StraightRail.Orientation.Vertical && rl.orientation == StraightRail.Orientation.Vertical) { moveDirection = MoveDirection.Up; if (Globals.debugMessages) { Console.WriteLine("Rail found at: Top"); } return; } else if (rail is TurnRail) { if (((TurnRail)rail).turnDir == TurnRail.TurnDirection.DownRight || ((TurnRail)rail).turnDir == TurnRail.TurnDirection.DownLeft) { moveDirection = MoveDirection.Up; return; } } } searchCell = Cell.GetCell(cell, 0, 1); if (searchCell.item is Rail) // track found at bottom { Rail rail = (Rail)searchCell.item; if (moveDirection == MoveDirection.None && rail is StraightRail && ((StraightRail)rail).orientation == StraightRail.Orientation.Vertical && rl.orientation == StraightRail.Orientation.Vertical) { moveDirection = MoveDirection.Down; if (Globals.debugMessages) { Console.WriteLine("Rail found at: Bottom"); } return; } else if (rail is TurnRail) { if (((TurnRail)rail).turnDir == TurnRail.TurnDirection.LeftUp || ((TurnRail)rail).turnDir == TurnRail.TurnDirection.RightUp) { moveDirection = MoveDirection.Down; return; } } } moveDirection = MoveDirection.None; if (Globals.debugMessages) { Console.WriteLine("Rail not found"); } } }
public void SetFutureCell() { Cell newCell = null; switch (moveDirection) { case MoveDirection.None: break; case MoveDirection.Up: newCell = Cell.GetCell(cell, 0, -1); if (newCell.item is Rail) // useless? { if (newCell.item is TurnRail rl) // the track is a turn { switch (rl.turnDir) { case TurnRail.TurnDirection.DownRight: moveDirection = MoveDirection.Right; break; case TurnRail.TurnDirection.DownLeft: moveDirection = MoveDirection.Left; break; default: break; } } else if (((StraightRail)newCell.item).orientation != StraightRail.Orientation.Vertical) { newCell = null; } } break; case MoveDirection.Down: newCell = Cell.GetCell(cell, 0, 1); if (newCell.item is Rail) { if (newCell.item is TurnRail rl) { switch (rl.turnDir) { case TurnRail.TurnDirection.RightUp: moveDirection = MoveDirection.Right; break; case TurnRail.TurnDirection.LeftUp: moveDirection = MoveDirection.Left; break; default: break; } } else if (((StraightRail)newCell.item).orientation != StraightRail.Orientation.Vertical) { newCell = null; } } break; case MoveDirection.Right: newCell = Cell.GetCell(cell, 1, 0); if (newCell.item is Rail) { if (newCell.item is TurnRail rl) { switch (rl.turnDir) { case TurnRail.TurnDirection.LeftUp: moveDirection = MoveDirection.Up; break; case TurnRail.TurnDirection.DownLeft: moveDirection = MoveDirection.Down; break; default: break; } } else if (((StraightRail)newCell.item).orientation != StraightRail.Orientation.Horizontal) { newCell = null; } } break; case MoveDirection.Left: newCell = Cell.GetCell(cell, -1, 0); if (newCell.item is Rail) { if (newCell.item is TurnRail rl) { switch (rl.turnDir) { case TurnRail.TurnDirection.RightUp: moveDirection = MoveDirection.Up; break; case TurnRail.TurnDirection.DownRight: moveDirection = MoveDirection.Down; break; default: break; } } else if (((StraightRail)newCell.item).orientation != StraightRail.Orientation.Horizontal) { newCell = null; } } break; default: break; } //if (newCell == null) //{ // futureRail = null; // return; //} futureRail = newCell != null && newCell.item != null && newCell.item is Rail ? (Rail)newCell.item : null; }
public void Connect(Node a, Node b) { Rail rail = a.Connect(b); Rails.Add(rail.Guid, rail); }