⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 actor.java

📁 一个j2me的游戏的实现.需要添加一个mathfp类来支持浮点数.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         if (world.checkCollision(this))
         {
            xVelFP = MathFP.mul(xVelFP, bounceVelFP);
            xFP = MathFP.add(lastXFP, xVelFP);
         }

         // Adjust Y. This code also handles a special case where the x
         // collision may have caused the Actor to move (such as colliding with
         // a teleport or level gateway). In this case our lastYFP is invalid
         // and we should abort the collision test. Therefore we only do the
         // y test now if the lastYFP STILL equals the yFP (which it should
         // unless a teleport occured).
         if (lastYFP == yFP)
         {
            yFP = MathFP.add(yFP, MathFP.mul(yVelFP, ticksFP));

            // Check if we collided with anything in Y movement
            if (world.checkCollision(this))
            {
               yVelFP = MathFP.mul(yVelFP, bounceVelFP);
               yFP = MathFP.add(lastYFP, yVelFP);
            }
         }
      }
   }

   /**
    * @return The current x position of the actor.
    */
   public int getX()
   {
      return MathFP.toInt(xFP);
   }

   /**
    * @return The current y position of the actor.
    */
   public int getY()
   {
      return MathFP.toInt(yFP);
   }

   /**
    * Change the Actor's current direction.
    * @param newDirection The new direction to face (in degrees).
    */
   public void setDirection(int newDirection)
   {
      realDir = newDirection;
      if (realDir < 0) realDir = (359 + (realDir));   // add the neg value
      if (realDir > 359) realDir = (newDirection - 360);

      // set the facing direction to be the closest alignment
      if (wantAlignment)
         alignedDir = getAlignedDirection(realDir);
      else
         alignedDir = realDir;
   }

   /**
    * Gets the closest aligned direction to the passed in direction (in
    * degrees).
    * @param dir The direction to align (in degrees).
    * @return A direction which aligns to the number of divisions the Actor
    * supports.
    */
   public final int getAlignedDirection(int dir)
   {
      int fp = MathFP.toInt(MathFP.div(MathFP.toFP(dir), alignedDivDegreesFP));
      int ad = MathFP.toInt(MathFP.mul(MathFP.toFP(fp), alignedDivDegreesFP));
      if (ad < 0) ad = 0;
      if (ad > 359) ad = 0;
      return ad;
   }

   /**
    * @return The current aligned direction.
    */
   public final int getDirection()
   {
      return alignedDir;
   }

   /**
    * @return The current real direction (not aligned).
    */
   public final int getRealDirection()
   {
      return realDir;
   }

   /**
    * Uses the distance method to calculate the distance between this Actor and
    * another one.
    * @param anotherActor The Actor to calculate the distance to.
    * @return The distance to anotherActor.
    */
   public int distanceTo(Actor anotherActor)
   {
      return distance(this.getX(), this.getY(), anotherActor.getX(),
                      anotherActor.getY());
   }

   public void setThrust(int newThrust)
   {
      thrustFP = MathFP.toFP(newThrust);
   }

   /**
    * Abstract method to return the width of the Actor.
    */
   abstract public int getWidth();

   /**
    * Abstract method to return the height of the Actor.
    */
   abstract public int getHeight();



   /******************************* STATICS **********************************/

   /**
    * Returns the shortest turning direction from one angle to another
    */
   public final static boolean isClockwise(int angleA, int angleB)
   {
      if (angleA > angleB)
         return (Math.abs(angleA - angleB)) < (angleB + (360 - angleA));
      else
         return (angleA + (360 - angleB)) < (Math.abs(angleB - angleA));
   }

	/**
	 * Returns a game relative angle between two points
	 */
	public final static int getFacingAngle(int x, int y, int ax, int ay)
	{
		// figure the two sides of our right angle triangle
		int a = MathFP.toFP(Math.abs(ax - x));
		int b = MathFP.toFP(Math.abs(ay - y));

		if (a == 0) a = FP_ONE;
		if (b == 0) b = FP_ONE;

		int bovera = MathFP.div(b, a);
		int angleInRadians = MathFP.atan(bovera);
		int angle = getAngleFromRadians(angleInRadians);

		// The tan result from this calculation is between 0 and 90 degrees so now
      // we adjust the result for the actual quadrant we're in relative to the
      // other Actor.
		if (ax < x) // left side
		{
			if (ay < y)
				return angle + 90;   // top
			return angle + 180;     // bottom
		}
		else // right side
		{
			if (ay < y)
				return angle;        // top
			return angle + 270;     // bottom
		}
	}

   public final static int getAngleFromRadians(int radiansFP)
   {
      return MathFP.toInt(MathFP.mul(radiansFP, FP_DEGREES_PER_RAD));
   }

   public final static int getRadiansFromAngle(int angle)
   {
      return MathFP.div(MathFP.toFP(angle), FP_DEGREES_PER_RAD);
   }

   /**
    * Projects a point starting at x, y outwards at an angle for the specified
    * distance. The result is an array of two integer with the x and y point
    * of the projected point.
    * @param startX The starting x position.
    * @param startY The starting y position.
    * @param angleToProjectAt The angle to project along.
    * @param distanceToGo The distance to go.
    * @return An array of 2 integers with the x and y position of the projected
    * point.
    */
   public final static int[] getProjectedPos(int startX, int startY,
                                             int angleToProjectAt,
                                             int distanceToGo)
   {
      int angleInRadians = MathFP.div(MathFP.toFP(angleToProjectAt),
                                      FP_DEGREES_PER_RAD);

      int dx = MathFP.cos(angleInRadians);
      int dy = -MathFP.sin(angleInRadians);

      int xFP = MathFP.toFP(startX);
      int yFP = MathFP.toFP(startY);
      int distanceFP = MathFP.toFP(distanceToGo);

      xFP = MathFP.add(xFP, MathFP.mul(dx, distanceFP));
      yFP = MathFP.add(yFP, MathFP.mul(dy, distanceFP));

      int[] result = {MathFP.toInt(xFP), MathFP.toInt(yFP)};
      return result;
   }

   /**
    * Calculates the distance from one point to another.
    * @param fromX The first x position.
    * @param fromY The first y position.
    * @param toX The second x position.
    * @param toY The second y position.
    * @return The distance between the two points.
    */
   public final static int distance(int fromX, int fromY, int toX, int toY)
   {
      int dx = (toX - fromX) * (toX - fromX);
      int dy = (toY - fromY) * (toY - fromY);
      if (dx == 0 || dy == 0) return 0;

      try
      {
         return MathFP.toInt(MathFP.sqrt(MathFP.toFP(dx + dy)));
      }

      catch (ArithmeticException ae)
      {
         return 0;
      }
   }


}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -