private void initPos() { pos_logic = new Position(); pos_logic.x = 14; pos_logic.y = 26; direction = 3; _pending_direction = 0; _state = 0; _change_direction = 0; is_alive = true; }
protected override void initPos() { _default_pos_graphic = new Position(181, 219); _default_tile = new Position(12, 17); pos_logic = new Position(12, 17); _direction = Constants.UP; _state = false; // mode = can_exit ? GhostMode.HouseExit : GhostMode.House; mode = GhostMode.HouseExit; _scatter_tile.x = 1; _scatter_tile.y = 0; }
protected override void initPos() { _default_pos_graphic = new Position(213, 171); _default_tile = new Position(13, 17); pos_logic = new Position(13, 14); _direction = Constants.LEFT; _state = false; // mode = can_exit ? GhostMode.HouseExit : GhostMode.House; mode = GhostMode.Scatter; _scatter_tile.x = 25; _scatter_tile.y = 0; }
private void Timer_Move(object sender, EventArgs e) { Thickness m = pacman_img.Margin; //pos tmp = pos_graphic; Position tmp = new Position(); tmp.x = (int)m.Left; tmp.y = (int)m.Top; Position tmp_logic = pos_logic; switch (direction) { case Constants.RIGHT: m.Left += 2; if (((m.Left - 5) % 16) == 6) { if (canMove(_pending_direction)) { direction = _pending_direction; _pacman_image[1] = Constants.pacmanImg[direction * 2 - 1]; _pacman_image[2] = Constants.pacmanImg[direction * 2]; _pacman_image[3] = Constants.pacmanImg[direction * 2 - 1]; } if (_change_direction-- <= 0) _pending_direction = 0; if (!canMove(direction)) { m.Left -= 2; _timer_animation.Stop(); } } pos_logic.x = ((int)m.Left - 4) / 16 + 1; break; case Constants.UP: m.Top -= 2; if (((m.Top - 5) % 16) == 6) { if (canMove(_pending_direction)) { direction = _pending_direction; _pacman_image[1] = Constants.pacmanImg[direction * 2 - 1]; _pacman_image[2] = Constants.pacmanImg[direction * 2]; _pacman_image[3] = Constants.pacmanImg[direction * 2 - 1]; } if (_change_direction-- <= 0) _pending_direction = 0; if (!canMove(direction)) { m.Top += 2; _timer_animation.Stop(); } } pos_logic.y = ((int)m.Top - 4 + 48) / 16 + 1; break; case Constants.LEFT: m.Left -= 2; if (((m.Left - 5) % 16) == 6) { if (canMove(_pending_direction)) { direction = _pending_direction; _pacman_image[1] = Constants.pacmanImg[direction * 2 - 1]; _pacman_image[2] = Constants.pacmanImg[direction * 2]; _pacman_image[3] = Constants.pacmanImg[direction * 2 - 1]; } if (_change_direction-- <= 0) _pending_direction = 0; if (!canMove(direction)) { m.Left += 2; _timer_animation.Stop(); } } pos_logic.x = ((int)m.Left - 4) / 16 + 1; break; case Constants.DOWN: m.Top += 2; if (((m.Top - 5) % 16) == 6) { if (canMove(_pending_direction)) { direction = _pending_direction; _pacman_image[1] = Constants.pacmanImg[direction * 2 - 1]; _pacman_image[2] = Constants.pacmanImg[direction * 2]; _pacman_image[3] = Constants.pacmanImg[direction * 2 - 1]; } if (_change_direction-- <= 0) _pending_direction = 0; if (!canMove(direction)) { m.Top -= 2; _timer_animation.Stop(); } } pos_logic.y = ((int)m.Top - 4 + 48) / 16 + 1; break; } // If graphical position changed then start animate if (m.Left != tmp.x || m.Top != tmp.y) { _timer_animation.Start(); SoundEffect.Instance.playBackground(); } else { SoundEffect.Instance.stopBackground(); SoundEffect.Instance.stopEatDot(); } // If logical position changed then notify observers if(pos_logic.x != tmp_logic.x || pos_logic.y != tmp_logic.y) { notifyObservers(); } // Tunel Telepoer if (pos_logic.x <= -1) { pos_logic.x = 28; m.Left = 443; } else if (pos_logic.x >= 29) { pos_logic.x = 0; m.Left = -5; } pacman_img.Margin = m; }
protected void Timer_Move(object sender, EventArgs e) { Thickness m = _ghost_img.Margin; Position tmp = new Position(); tmp.x = (int)m.Left; tmp.y = (int)m.Top; Position tmp_logic = pos_logic; switch (_direction) { case Constants.RIGHT: m.Left += 2; if (((m.Left - 5) % 16) == 6) { _direction = choseDirection(choseTarget()); if (mode != GhostMode.Frightened && mode != GhostMode.Eyes) { _ghost_image[0] = _image_urls[_direction * 2 - 2]; _ghost_image[1] = _image_urls[_direction * 2 - 1]; } else if(mode == GhostMode.Eyes){ _ghost_image[0] = Constants.eyesGhostImg[_direction * 2 - 2]; _ghost_image[1] = Constants.eyesGhostImg[_direction * 2 - 1]; } } pos_logic.x = ((int)m.Left - 4) / 16 + 1; break; case Constants.UP: m.Top -= 2; if (((m.Top - 5) % 16) == 6) { _direction = choseDirection(choseTarget()); if (mode != GhostMode.Frightened && mode != GhostMode.Eyes) { _ghost_image[0] = _image_urls[_direction * 2 - 2]; _ghost_image[1] = _image_urls[_direction * 2 - 1]; } else if (mode == GhostMode.Eyes) { _ghost_image[0] = Constants.eyesGhostImg[_direction * 2 - 2]; _ghost_image[1] = Constants.eyesGhostImg[_direction * 2 - 1]; } } pos_logic.y = ((int)m.Top - 4 + 48) / 16 + 1; break; case Constants.LEFT: m.Left -= 2; if (((m.Left - 5) % 16) == 6) { _direction = choseDirection(choseTarget()); if (mode != GhostMode.Frightened && mode != GhostMode.Eyes) { _ghost_image[0] = _image_urls[_direction * 2 - 2]; _ghost_image[1] = _image_urls[_direction * 2 - 1]; } else if (mode == GhostMode.Eyes) { _ghost_image[0] = Constants.eyesGhostImg[_direction * 2 - 2]; _ghost_image[1] = Constants.eyesGhostImg[_direction * 2 - 1]; } } pos_logic.x = ((int)m.Left - 4) / 16 + 1; break; case Constants.DOWN: m.Top += 2; if (((m.Top - 5) % 16) == 6) { _direction = choseDirection(choseTarget()); if (mode != GhostMode.Frightened && mode != GhostMode.Eyes) { _ghost_image[0] = _image_urls[_direction * 2 - 2]; _ghost_image[1] = _image_urls[_direction * 2 - 1]; } else if (mode == GhostMode.Eyes) { _ghost_image[0] = Constants.eyesGhostImg[_direction * 2 - 2]; _ghost_image[1] = Constants.eyesGhostImg[_direction * 2 - 1]; } } pos_logic.y = ((int)m.Top - 4 + 48) / 16 + 1; break; } // If graphical position changed then start animate if (m.Left != tmp.x || m.Top != tmp.y) { _timer_animation.Start(); } // If logical position changed then notify observers if (pos_logic.x != tmp_logic.x || pos_logic.y != tmp_logic.y) { notifyObservers(); } // Tunel Telepoer if (pos_logic.x <= -1) { pos_logic.x = 27; m.Left = 427; } else if (pos_logic.x >= 28) { pos_logic.x = 0; m.Left = -5; } _ghost_img.Margin = m; }
// Returns direction closest to target in straight line private int choseDirection(Position target) { int tmp_direction = _direction; double tmp_dist; double distance = 100000.0; if (_direction != Constants.LEFT && canMove(Constants.RIGHT) || mode == GhostMode.House) { tmp_dist = Math.Sqrt(Math.Pow(target.x - (pos_logic.x + 1), 2) + Math.Pow(target.y - pos_logic.y, 2)); if (distance > tmp_dist) { tmp_direction = Constants.RIGHT; distance = tmp_dist; } } if (_direction != Constants.DOWN && canMove(Constants.UP) || mode == GhostMode.House) { tmp_dist = Math.Sqrt(Math.Pow(target.x - pos_logic.x, 2) + Math.Pow(target.y - (pos_logic.y - 1), 2)); if (distance > tmp_dist) { // Special 4 tiles where ghosts can't go upwards // Frighten mode is exception if(!((pos_logic.x == 12 || pos_logic.x == 15) && (pos_logic.y == 14 || pos_logic.y == 26)) || mode == GhostMode.Frightened) { tmp_direction = Constants.UP; distance = tmp_dist; } } } if (_direction != Constants.RIGHT && canMove(Constants.LEFT) || mode == GhostMode.House) { tmp_dist = Math.Sqrt(Math.Pow(target.x - (pos_logic.x - 1), 2) + Math.Pow(target.y - pos_logic.y, 2)); if (distance > tmp_dist) { tmp_direction = Constants.LEFT; distance = tmp_dist; } } if (_direction != Constants.UP && canMove(Constants.DOWN) || mode == GhostMode.House) { tmp_dist = Math.Sqrt(Math.Pow(target.x - pos_logic.x, 2) + Math.Pow(target.y - (pos_logic.y + 1), 2)); if (distance > tmp_dist) { //Console.Write(distance + " - " + Math.Sqrt(Math.Pow(pacman.pos_logic.x - pos_logic.x, 2) //+ Math.Pow(pacman.pos_logic.y - (pos_logic.y + 1), 2)) + "\n"); tmp_direction = Constants.DOWN; distance = tmp_dist; } } return tmp_direction; }