/// <summary> /// Handles a move request by a player and signal it to the other processes /// </summary> /// <param name="moveRequest"></param> /// <param name="sender"></param> private void HandleMovePlayer(MoveRequest moveRequest, ActorProcess sender) { // Pessimistically assume the move is going to be denied MoveSignal moveSignal = new MoveSignal { InitialTile = sender.Position, FinalTile = sender.Position }; // Try to move and update the final position MoveEffect effect = map.CheckForMoveEffects(sender.Position, moveRequest.DesiredTile); if (effect != MoveEffect.InvalidMove) { logger.LogMove(sender.Rank, true, sender.Position, moveRequest.DesiredTile); switch (effect) { case MoveEffect.RatCaptured: ActorProcess rat = GetActorProcessByCoordinates(moveRequest.DesiredTile); logger.LogCaptureRat(rat.Rank, sender.Rank); HandleRatRemoval(rat); break; case MoveEffect.RatEscaped: logger.LogExitRat(sender.Rank, moveRequest.DesiredTile); HandleRatRemoval(GetActorProcessByCoordinates(sender.Position)); break; case MoveEffect.CheeseEaten: logger.LogCheeseConsumption(sender.Rank, moveRequest.DesiredTile); break; } // Move is valid and the destination tile no longer has important information map.ApplyMove(sender.Position, moveRequest.DesiredTile); sender.Position = moveRequest.DesiredTile; moveSignal.FinalTile = moveRequest.DesiredTile; } else { logger.LogMove(sender.Rank, false, sender.Position, moveRequest.DesiredTile); } moveCount++; if (moveCount % MAP_LOG_INTERVAL == 0) { logger.LogMap(map.ToString()); } if (IsGameOver()) { EndGame(); } else { // Broadcast the result of the move Broadcast(moveSignal); } }
private void Update() { //If game is not in the play, //there is not need for collecting input if (isGamePlaying) { //Debug.Log("Update"); //Save time if space bar is pressed //And return to do not change camera angle if (Input.GetKey(KeyCode.Space)) { //Debug.Log("KeyDown"); m_shotTime += Time.deltaTime; ShotPower.Dispatch(m_shotTime); return; } else if (Input.GetKeyUp(KeyCode.Space)) { //Debug.Log("KeyMove"); m_shotTime += Time.deltaTime; ShotSignal.Dispatch(m_shotTime); m_shotTime = 0; return; } } if (Input.GetMouseButtonDown(0)) { //Debug.Log("MouseDown :"+ Input.mousePosition.x+" "+ Input.mousePosition.y); //Get move values and send them to listener at camera hanle m_moveHorizontal = Input.mousePosition.x; m_moveVertical = Input.mousePosition.y; return; } if (Input.GetMouseButton(0)) { //Debug.Log("MouseHeld :" + Input.mousePosition.x + " " + Input.mousePosition.y); //Debug.Log("MouseMoveHeld :" + (Input.mousePosition.x - m_moveHorizontal) + " " + (Input.mousePosition.y - m_moveVertical)); //Get move values and send them to listener at camera hanle m_moveHorizontal = Input.mousePosition.x - m_moveHorizontal; m_moveVertical = Input.mousePosition.y - m_moveVertical; MoveSignal.Dispatch(m_moveHorizontal, m_moveVertical); m_moveHorizontal = Input.mousePosition.x; m_moveVertical = Input.mousePosition.y; } }
/// <summary> /// Handle the message and call the corresponding methods. Return if the message was our response. /// </summary> /// <param name="message">Message to handle.</param> /// <returns>True if the message was targeted at the current actor.</returns> private bool HandleMessage(Message message) { MoveSignal moveSignal = message as MoveSignal; if (moveSignal != null) { // Update the map based on the move received. map.ApplyMove(moveSignal.InitialTile, moveSignal.FinalTile); if (moveSignal.InitialTile.Equals(currentTile.Position)) { currentTile = map.Tiles[moveSignal.FinalTile.Y, moveSignal.FinalTile.X]; return(true); } return(false); } MeowSignal meowSignal = message as MeowSignal; if (meowSignal != null) { // We notif that a cat Meow. ListenMeow(map.Tiles[meowSignal.MeowLocation.Y, meowSignal.MeowLocation.X]); return(false); } KillSignal killSignal = message as KillSignal; if (killSignal != null) { // The map said that we need to die. shouldDie = true; return(true); } return(false); }
public void MoveTo(Vector2 destination) { MoveSignal.Dispatch(destination); }
private void Move(MoveSignal signal) { _rb.position = (Vector2)transform.position + signal.moveInput * Time.fixedDeltaTime * _playerSpeed; }