📄 actor.java
字号:
return world;
}
/**
* set spin rate in degrees per second
* @param i 0-360 degrees
*/
public final void setSpin(int i)
{
spinFP = MathFP.toFP(i);
}
/**
* Set an angle this actors wants to face; the actor will start spinning
* at its default spin rate towards the target angle - see cycle for
* the actual spin code.
*/
public final void setTargetDirection(int angle)
{
targetAngle = angle;
autoSpinning = false;
}
public final void setThrust(int i)
{
thrustFP = MathFP.div(i, MathFP.toFP(3));
}
public final int getThrust()
{
return MathFP.toInt(thrustFP);
}
public final boolean isThrusting()
{
return MathFP.toInt(thrustFP) != 0;
}
public final int getX()
{
return x;
}
public final int getY()
{
return y;
}
public final void setX(int xArg)
{
xFP = MathFP.toFP(xArg);
x = xArg;
}
public final void setY(int yArg)
{
yFP = MathFP.toFP(yArg);
y = yArg;
}
public final int getCenterX()
{
return x + (getWidth() / 2);
}
public final int getCenterY()
{
return getY() + (getHeight() / 2);
}
public final boolean isCollidingWith(int px, int py)
{
if (px >= x && px <= (x + getWidth()) &&
py >= y && py <= (y + getHeight()))
return true;
return false;
}
public final boolean isCollidingWith(int ax, int ay, int w, int h)
{
if (y + getHeight() < ay || y > ay + h ||
x + getWidth() < ax || x > ax + w)
return false;
return true;
}
public final boolean isCollidingWith(Actor another)
{
return isCollidingWith(another.getX(), another.getY(), another.getWidth(),
another.getHeight());
}
public void suicide()
{
}
private boolean teleported;
/**
* A cycle method that moves the Actor a distance relative to its current
* speed (the value of the speed int) and the amount of time that has passed
* since the last call to cycle (deltaMS). This code uses a fluff value in
* order to remember values too small to handle (below the tick level).
* @param deltaMS The number of milliseconds that have passed since the last
* call to cycle.
*/
public void cycle(long deltaMS)
{
int ticks = (int) (deltaMS + fluff) / 100;
// remember the bit we missed
fluff += (deltaMS - (ticks * 100));
if (ticks > 0)
{
int ticksFP = MathFP.toFP(ticks);
// move towards our target direction, if we have one
if (targetAngle != 0)
{
if (!autoSpinning)
{
// start spin in the dir of the target angle
setSpin(isClockwise(getDirection(), targetAngle) ? -maxSpinRate : maxSpinRate);
}
// and check if we've made it to the target direction
if (getAlignedDirection(targetAngle) == getDirection())
{
setSpin(0);
setTargetDirection(0);
autoSpinning = false;
}
}
// spin based on degrees per tick
if (spinFP != 0)
setDirection(getRealDirection() + MathFP.toInt(MathFP.mul(ticksFP, spinFP)));
// move based on our speed in pixels per ticks
if (thrustFP != 0)
{
//#ifdef debug
if (alignedDir > 359) System.out.println("bad aligneddir=" + alignedDir);
if (alignedDir < 0) System.out.println("bad aligneddir=" + alignedDir);
//#endif
xAccFP = MathFP.mul(thrustFP, lookupCosFP[alignedDir]);
yAccFP = MathFP.mul(thrustFP, -lookupSinFP[alignedDir]);
xVelFP = MathFP.add(xVelFP, xAccFP);
yVelFP = MathFP.add(yVelFP, yAccFP);
}
//System.out.println("accX=" + MathFP.toString(xAccFP) + " accY=" + MathFP.toString(yAccFP) +
// " velX=" + MathFP.toString(xVelFP) + " velY=" + MathFP.toString(yVelFP));
// If you change this remember to change the setVel code
if (xVelFP > maxVelFP)
xVelFP = maxVelFP;
else if (xVelFP < -maxVelFP) xVelFP = -maxVelFP;
if (yVelFP > maxVelFP)
yVelFP = maxVelFP;
else if (yVelFP < -maxVelFP) yVelFP = -maxVelFP;
lastXFP = xFP;
lastYFP = yFP;
// adjust X
xFP = MathFP.add(xFP, MathFP.mul(xVelFP, ticksFP));
x = MathFP.toInt(xFP);
teleported = false;
// now check if we collided with anything (we test X first)
if (collidable)
{
if (world.checkCollision(this, x, y, getWidth(), getHeight()))
{
xVelFP = MathFP.mul(xVelFP, bounceVelFP);
xFP = MathFP.add(lastXFP, xVelFP);
x = MathFP.toInt(xFP);
}
if (lastYFP != yFP) teleported = true;
}
// adjust Y
// we also handle a special case where the x collision may have
// caused the ship to move (teleport, gateway). In this case our
// lastYFP is invalid and we should abort the collision test.
if (!teleported)
{
yFP = MathFP.add(yFP, MathFP.mul(yVelFP, ticksFP));
y = MathFP.toInt(yFP);
// now check if we collided with anything in Y movement
if (collidable)
{
if (world.checkCollision(this, x, y, getWidth(), getHeight()))
{
yVelFP = MathFP.mul(yVelFP, bounceVelFP);
yFP = MathFP.add(lastYFP, yVelFP);
y = MathFP.toInt(yFP);
}
}
}
}
}
public void onCollision(Actor another)
{
}
/******************************* 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);
}
/**
* returns the opposite of an angle (ie. 0=180, 90=270)
* @param angle
*/
public final static int getOppositeAngle(int angle)
{
if (angle < 180) return angle + 180;
return angle - 180;
}
/**
* Project a point along a vector
* @param x starting x position
* @param y starting y position
* @param angle angle to project along
* @param distance distance to project
* @return int[] containing x and y int positions
*/
public final static int[] getProjectedPos(int x, int y, int angle,
int distance)
{
int dx = lookupCosFP[angle];
int dy = -lookupSinFP[angle];
int xFP = MathFP.toFP(x);
int yFP = MathFP.toFP(y);
int distanceFP = MathFP.toFP(distance);
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;
}
public final static int distance(int x1, int y1, int x2, int y2)
{
int dx = (x2 - x1) * (x2 - x1);
int dy = (y2 - y1) * (y2 - y1);
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 + -