/* * public Game.Direction GetDirection(int col, int row) { * return directions[map.GetIndex(col, row)]; * } * * public float GetCostToGoal(int col, int row) { * return costs[map.GetIndex(col, row)]; * }*/ public void TileChanged(Vector2Int square) { float bestCost = Mathf.Infinity; Game.Direction bestDirection = Game.Direction.left; for (int i = 0, len = Game.Direction.directions.Length; i < len; ++i) { Game.Direction direction = Game.Direction.directions[i]; Vector2Int newSq = square + direction.deltaPosition; if (!map.IsInBounds(newSq)) { continue; } float costInDirection = nodes[newSq.y, newSq.x].cost; if (costInDirection < bestCost) { bestCost = costInDirection; bestDirection = direction; } } Debug.Log("Best cost from " + square + " is " + bestCost + " in direction " + bestDirection); Node node = nodes[square.y, square.x]; node.cost = bestCost + map.GetCost(square); node.direction = bestDirection.GetOpposite(); node.generation = currentGeneration; nodes[square.y, square.x] = node; UpdateAroundTile(square, node.cost); }
/// <summary> /// Returns the appropriate animation for the specified direction of a moving player /// </summary> public static Animation GetWalkAnimation(Game.Direction walkDirection, double animationDuration) { // Get pre-generated sprites IReadOnlyList <SharpDX.Direct2D1.Bitmap> spriteSequence = null; switch (walkDirection) { case Game.Direction.North: spriteSequence = up; break; case Game.Direction.South: spriteSequence = down; break; case Game.Direction.West: spriteSequence = left; break; case Game.Direction.East: spriteSequence = right; break; default: throw new Exception("Unknown walking direction: " + walkDirection); } return(new Animation(spriteSequence, animationDuration, true)); }
// Start is called before the first frame update void Start() { game = GameObject.FindGameObjectWithTag("Game").GetComponent <GameManager>(); tilesToPass = 1; anim = GetComponent <Animator>(); current = Game.Direction.None; Vector3 PosToWalk = Vector3.zero; }
public FloatingLocationResolver(Tile initialTile) { Console.WriteLine("New resolver"); CurrentTile = initialTile; Direction = Game.Direction.None; this.bounds = new RectangleF(initialTile.Bounds.Location, Game.boxSize); this.centerPosition = new PointF(Bounds.Left + Bounds.Width / 2, Bounds.Top + Bounds.Height / 2); }
static public PlayerMovement Decode_PlayerMovement(byte[] data) { int peerID = BitConverter.ToInt32(data, 4); Game.Direction dir = (Game.Direction)BitConverter.ToInt32(data, 8); int x = BitConverter.ToInt32(data, 12); int y = BitConverter.ToInt32(data, 16); return(new PlayerMovement(peerID, dir, new PointF(x, y))); }
/// <summary> /// Player movement replication, /// contains sender peer ID and movement directon /// </summary> static public byte[] Build_PlayerMovement(int myID, Game.Direction direction, PointF location) { List <byte> data = new List <byte>(); data.AddRange(BitConverter.GetBytes((int)MessageType.PlayerMovement)); data.AddRange(BitConverter.GetBytes(myID)); data.AddRange(BitConverter.GetBytes((int)direction)); data.AddRange(BitConverter.GetBytes(location.X)); data.AddRange(BitConverter.GetBytes(location.Y)); return(data.ToArray()); }
public Tile GetNeighbour(Tile tile, Game.Direction direction) { foreach (KeyValuePair <Game.Direction, Tile> pair in Tiles[tile]) { if (pair.Key == direction) { return(pair.Value); } } return(null); }
public void PacmanMovesAfterTick(Game.Direction direction, int nextX, int nextY) { var grid = new Grid(4, 8); var originPosition = new Position(0, 0); var positionNextToActual = new Position(nextX, nextY); grid.PlacePacManAt(originPosition); var game = new Game(grid); game.PacmanDirection = direction; game.Tick(); Assert.Equal(Grid.PacMan, grid.At(positionNextToActual)); }
public void SetDefaultDirection(int i) { if (!DefaultDirectionSeted) { if (i == 1) { prevoius = Game.Direction.Up; } else if (i == 2) { prevoius = Game.Direction.Down; } } }
private static void TryToMove(Game.Direction direction) { if (IsReadyToMove == 0) { if (Direction != direction) { Direction = direction; } else { Moved = true; } IsReadyToMove = MoveFrequency; } }
protected bool DamageEnemy(Game game, Point playerLocation, Game.Direction direction, int distance, int damage, Random rnd) { foreach (Enemy enemy in game.Enemies) { if (enemy.Nearby(playerLocation, distance, direction)) { enemy.TakeHit(damage, rnd); } return(true); } game.MovePlayer(direction); return(false); }
public Tile GetNextTileInDirection(Game.Direction direction) { switch (direction) { case Game.Direction.None: return(this); case Game.Direction.North: return(North); case Game.Direction.South: return(South); case Game.Direction.West: return(West); case Game.Direction.East: return(East); } return(null); }
public void Attack(Game.Direction direction, Random random) { if (fEquippedWeapon == null) { return; } else { fEquippedWeapon.Attack(direction, random); if (fEquippedWeapon is IPotion) { // might need to make sure some gui stuff gets done after this. fEquippedWeapon = null; } } }
public void NewMovementDirection(Game.Direction newDirection, double currentTime) { if (newDirection == this.movement.Direction) { // No need to update when direction is the same return; } // Update direction this.movement.Direction = newDirection; // Update velocity switch (newDirection) { case Game.Direction.North: this.movement.Velocity = new PointF(0, -225); break; case Game.Direction.South: this.movement.Velocity = new PointF(0, 225); break; case Game.Direction.West: this.movement.Velocity = new PointF(-225, 0); break; case Game.Direction.East: this.movement.Velocity = new PointF(225, 0); break; case Game.Direction.None: this.movement.Velocity = new PointF(0, 0); break; } // Update animation if (newDirection == Game.Direction.None) { StopAnimating(); } else { StartAnimating(newDirection, currentTime); } }
public override void Move(Random rand, Rectangle boundaries, Point playerLocation) { // only move if it has one or more hit points if (this.HitPoints > 0) { // move in random direction with probability 1/2 if (rand.NextDouble() < 0.5) { Game.Direction direction = (Game.Direction)rand.Next(3); this.Move(direction, boundaries); } else { // else move towards player. 1st decide direction. this.MoveTowardsPlayer(boundaries, playerLocation); } } }
private void UpdateAroundTile(Vector2Int square, float tileCost) { for (int i = 0, len = Game.Direction.directions.Length; i < len; ++i) { Game.Direction direction = Game.Direction.directions[i]; Vector2Int newSq = square + direction.deltaPosition; if (!map.IsInBounds(newSq)) { continue; } float newCost = tileCost + map.GetCost(newSq); Node newNode = nodes[newSq.y, newSq.x]; if (newCost < maxCost && (newNode.cost > newCost || newNode.generation < currentGeneration)) { PathItem newItem = new PathItem(newSq, direction, newCost); path.Enqueue(newItem); } } }
private void TurnLeft() { if (transform.parent.transform.position.x > PosToWalk.x) { transform.parent.Translate(Vector3.forward * Time.deltaTime * speed); } else { if (anim != null) { anim.SetFloat("Walk", 0.0f); } PosToWalk = Vector3.zero; current = Game.Direction.None; game.GetClickedTile().SetCurrentStepingGameTool(null); game.GetTileToWalk().SetCurrentStepingGameTool(GetComponent <GameTool>()); transform.parent.transform.position = game.GetTileToWalk().transform.position; game.PassTurn(); } }
public static Vector2Int GetNextCell(Vector2Int currentCell, Game.Direction direction) { Vector2Int indexOffset = default(Vector2Int); int oddColumn = currentCell.x & 1; switch (direction) { case Game.Direction.NW: indexOffset.x = -1; indexOffset.y = 1 - oddColumn; break; case Game.Direction.North: indexOffset.x = 0; indexOffset.y = 1; break; case Game.Direction.NE: indexOffset.x = 1; indexOffset.y = 1 - oddColumn; break; case Game.Direction.SW: indexOffset.x = -1; indexOffset.y = -oddColumn; break; case Game.Direction.South: indexOffset.x = 0; indexOffset.y = -1; break; case Game.Direction.SE: indexOffset.x = 1; indexOffset.y = -oddColumn; break; } return(currentCell + indexOffset); }
public void Kicked(Game.Direction kickedFromSide) { System.Console.WriteLine("Kicked from direction: " + kickedFromSide); if (!(this.LocationResolver is FloatingLocationResolver)) { this.LocationResolver = new FloatingLocationResolver(this.Tile); } var resolver = this.LocationResolver as FloatingLocationResolver; switch (kickedFromSide) { case Game.Direction.East: resolver.Direction = Game.Direction.East; resolver.Velocity = new PointF(300, 0); break; case Game.Direction.North: resolver.Direction = Game.Direction.North; resolver.Velocity = new PointF(0, -300); break; case Game.Direction.South: resolver.Direction = Game.Direction.South; resolver.Velocity = new PointF(0, 300); break; case Game.Direction.West: resolver.Direction = Game.Direction.West; resolver.Velocity = new PointF(-300, 0); break; } Tile.Object = null; FloatingObjectRegistry.Register(this); }
public virtual void Move(Game.Direction direction, Rectangle boundaries) { Point newLocation = this.Location; switch (direction) { case Game.Direction.Up: if (newLocation.Y - MoveInterval >= boundaries.Top) { newLocation.Y -= MoveInterval; } break; case Game.Direction.Down: if (newLocation.Y + MoveInterval <= boundaries.Bottom) { newLocation.Y += MoveInterval; } break; case Game.Direction.Left: if (newLocation.X - MoveInterval >= boundaries.Left) { newLocation.X -= MoveInterval; } break; case Game.Direction.Right: if (newLocation.X + MoveInterval <= boundaries.Right) { newLocation.X += MoveInterval; } break; } this.Location = newLocation; }
public bool Nearby(Point locationToCheck, int distance, Game.Direction direction) { return(true); }
private void ExplodeInDirection(Game.Direction direction, FireType fireType, double currentTime) { FireType connectionType = (fireType == FireType.Up || fireType == FireType.Down) ? FireType.Vertical : FireType.Horizontal; var remainingRange = this.range; var tile = this.Tile; while (remainingRange > 0) { tile = tile.GetNextTileInDirection(direction); remainingRange--; if (tile == Tile.OutOfBounds) { break; } foreach (var item in FloatingObjectRegistry.GetMovingObjects()) { if (item.IsDescructible) { if (item.Tile == tile) { item.Destroy(currentTime); } } } var elem = tile.Object; if (elem == null) { if (remainingRange == 0) { tile.Object = new Fire(fireType, currentTime); } else { tile.Object = new Fire(connectionType, currentTime); } } else if (elem.IsDescructible) { elem.Destroy(currentTime); if (tile.Object == null) { tile.Object = new Fire(fireType, currentTime); } //if (tile.Object?.IsDescructible ?? true) { // tile.Object = new Fire(fireType, currentTime); //} break; } else if (elem is Fire) { var elemStart = (elem as Fire).GetStartTime(); if (elemStart < currentTime) { elem.Destroy(currentTime); tile.Object = new Fire(remainingRange == 0 ? fireType : connectionType, currentTime); } else { break; } } else { break; } } }
public void StartAnimating(Game.Direction direction, double currentTime) { this.spriteAnimation = PlayerAnimations.GetWalkAnimation(direction, 0.6); this.spriteAnimation.Start(currentTime); }
public PathItem(Vector2Int square, Game.Direction fromDirection, float cost) { this.square = square; this.fromDirection = fromDirection; this.cost = cost; }
public void SetDirection(Game.Direction direction) { Facing = direction; }
public void MoveTo(Game.Direction direction, int tilesToPass) { this.tilesToPass = tilesToPass; if (direction == Game.Direction.Right) { PosToWalk = transform.parent.transform.position + tileSize * tilesToPass * Vector3.right; current = Game.Direction.Right; if (prevoius == Game.Direction.Down) { this.transform.parent.transform.Rotate(0, -90, 0); prevoius = Game.Direction.Right; } else if (prevoius == Game.Direction.Left) { this.transform.parent.transform.Rotate(0, 180, 0); prevoius = Game.Direction.Right; } else if (prevoius == Game.Direction.Up) { this.transform.parent.transform.Rotate(0, 90, 0); prevoius = Game.Direction.Right; } if (anim != null) { anim.SetFloat("Walk", 1.0f); } } else if (direction == Game.Direction.Left) { PosToWalk = transform.parent.transform.position + tileSize * tilesToPass * Vector3.left; current = Game.Direction.Left; if (prevoius == Game.Direction.Down) { this.transform.parent.transform.Rotate(0, 90, 0); prevoius = Game.Direction.Left; } else if (prevoius == Game.Direction.Up) { this.transform.parent.transform.Rotate(0, -90, 0); prevoius = Game.Direction.Left; } else if (prevoius == Game.Direction.Right) { this.transform.parent.transform.Rotate(0, 180, 0); prevoius = Game.Direction.Left; } if (anim != null) { anim.SetFloat("Walk", 1.0f); } } else if (direction == Game.Direction.Up) { PosToWalk = transform.parent.transform.position + tileSize * tilesToPass * Vector3.forward; current = Game.Direction.Up; if (prevoius == Game.Direction.Down) { this.transform.parent.transform.Rotate(0, 180, 0); prevoius = Game.Direction.Up; ; } else if (prevoius == Game.Direction.Left) { this.transform.parent.transform.Rotate(0, 90, 0); prevoius = Game.Direction.Up; } else if (prevoius == Game.Direction.Right) { this.transform.parent.transform.Rotate(0, -90, 0); prevoius = Game.Direction.Up; } if (anim != null) { anim.SetFloat("Walk", 1.0f); } } else if (direction == Game.Direction.Down) { PosToWalk = transform.parent.transform.position + tileSize * tilesToPass * Vector3.back; current = Game.Direction.Down; if (prevoius == Game.Direction.Up) { this.transform.parent.transform.Rotate(0, 180, 0); prevoius = Game.Direction.Down; } else if (prevoius == Game.Direction.Left) { this.transform.parent.transform.Rotate(0, -90, 0); prevoius = Game.Direction.Down; } else if (prevoius == Game.Direction.Right) { this.transform.parent.transform.Rotate(0, 90, 0); prevoius = Game.Direction.Down; } if (anim != null) { anim.SetFloat("Walk", 1.0f); } } else { Debug.Log("Invalid direction"); } }
public Stack <Vector2Int> FindSolution(int maxMoves) { int bestDist = int.MaxValue; System.Array.Copy(map.CurrentState, virtualState, virtualState.Length); int heuristic = GetDistance(); List <Node> nodeList = new List <Node>(); nodeList.Add(new Node(playerCell, 0, heuristic, null)); //A* with break cost int nodeListIndex; for (nodeListIndex = 0; nodeListIndex < nodeList.Count; ++nodeListIndex) { bestDist = nodeList[nodeListIndex].dist + nodeList[nodeListIndex].heuristic; int minDistNode = nodeListIndex; // Find node with minimum estimated cost for (int li = nodeListIndex + 1; li < nodeList.Count; li++) { int dist = nodeList[li].dist + nodeList[li].heuristic; if (dist < bestDist) { bestDist = dist; minDistNode = li; } } //Swap positions Node node = nodeList[minDistNode]; nodeList[minDistNode] = nodeList[nodeListIndex]; nodeList[nodeListIndex] = node; if (bestDist > maxMoves) { return(null); //Too far } //TODO check path complete and break? if (node.heuristic == 0) { break; } SetVirtualState(node); Vector2Int currentCell = node.cell; // Find more nodes for (int i = 0; i < 6; ++i) { Game.Direction direction = (Game.Direction)i; Vector2Int neighbourCell = Map.GetNextCell(currentCell, direction); //Check legal move if (!CheckLegalMove(neighbourCell)) { continue; } int totalDist = node.dist + 1; //TODO dont add nodes with high heuristic? heuristic = node.heuristic + GetStepDistanceChange(neighbourCell); nodeList.Add(new Node(neighbourCell, totalDist, heuristic, node)); } } if (nodeListIndex == nodeList.Count) { return(null); } else { Node node = nodeList[nodeListIndex]; int nodeDist = node.dist; Stack <Vector2Int> path = new Stack <Vector2Int>(nodeDist); while (node != null && nodeDist > 0) { path.Push(node.cell); nodeDist--; node = node.prev; } return(path); } }
public PlayerMovement(int player, Game.Direction moveDirection, PointF location) { this.peerID = player; this.moveDirection = moveDirection; this.Location = location; }
public Snake() { Facing = Game.Direction.Up; Location[0] = 10; Location[1] = 3; }
public abstract void Attack(Game.Direction direction, Random rnd);