/android/LGame-Android-0.2.96/src/org/loon/framework/android/game/action/FireTo.java

http://loon-simple.googlecode.com/ · Java · 89 lines · 55 code · 14 blank · 20 comment · 14 complexity · 5aba342f3399cc30d2bf25983ed6ebd4 MD5 · raw file

  1. package org.loon.framework.android.game.action;
  2. import org.loon.framework.android.game.core.graphics.window.actor.ActorLayer;
  3. /**
  4. * Copyright 2008 - 2011
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. * use this file except in compliance with the License. You may obtain a copy of
  8. * the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. * License for the specific language governing permissions and limitations under
  16. * the License.
  17. *
  18. * @project loonframework
  19. * @author chenpeng
  20. * @email??šceponline@yahoo.com.cn
  21. * @version 0.1
  22. */
  23. public class FireTo extends ActionEvent {
  24. private double direction;
  25. private int x, y;
  26. private int vx, vy;
  27. private int endX, endY;
  28. private double speed;
  29. public FireTo(int endX, int endY, double speed) {
  30. this.endX = endX;
  31. this.endY = endY;
  32. this.speed = speed;
  33. }
  34. public boolean isComplete() {
  35. return isComplete;
  36. }
  37. public void onLoad() {
  38. this.x = original.getX();
  39. this.y = original.getY();
  40. this.direction = Math.atan2(endY - y, endX - x);
  41. this.vx = (int) (Math.cos(direction) * this.speed);
  42. this.vy = (int) (Math.sin(direction) * this.speed);
  43. }
  44. public void update(long elapsedTime) {
  45. this.x += this.vx;
  46. this.y += this.vy;
  47. if (x == 0 && y == 0) {
  48. isComplete = true;
  49. return;
  50. }
  51. ActorLayer layer = original.getLayer();
  52. if (layer.isBounded()) {
  53. if (layer.contains(x, y, original.getWidth(), original.getHeight())) {
  54. original.setLocation(x, y);
  55. } else {
  56. isComplete = true;
  57. }
  58. } else {
  59. if (x + original.getWidth() < 0) {
  60. isComplete = true;
  61. } else if (x > layer.getWidth() + original.getWidth()) {
  62. isComplete = true;
  63. }
  64. if (y + original.getHeight() < 0) {
  65. isComplete = true;
  66. } else if (y > layer.getHeight() + original.getHeight()) {
  67. isComplete = true;
  68. }
  69. original.setLocation(x, y);
  70. }
  71. }
  72. public double getDirection() {
  73. return direction;
  74. }
  75. }