Original author: Pierre Sarrazin. Preserve upstream GPLv3-or-later and file-specific notices. Source: http://sarrazip.com/dev/coco-planes-0.1.0.tar.gz Archive SHA-256: 29191e6ba225965caa637ee726e1189639dc7fcdf2b68310fb93fbffd56c9c52
172 lines
4.9 KiB
C
172 lines
4.9 KiB
C
#include "BiplaneMover.h"
|
|
|
|
#include "Sprite.h"
|
|
#include "PreShiftedBitmap.h"
|
|
#include "util.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
void
|
|
BiplaneMover_init(BiplaneMover *self,
|
|
Sprite *biplane,
|
|
const PreShiftedBitmap *biplaneBitmapArray[],
|
|
byte climbIncr,
|
|
byte diveIncr,
|
|
BiplaneDirectionFunctor directionFunctor)
|
|
{
|
|
self->biplane = biplane;
|
|
self->speed = 0;
|
|
self->numFramesBeforeSpawn = 0;
|
|
self->hasStarted = FALSE;
|
|
self->isStalled = FALSE;
|
|
self->biplaneBitmapArray = biplaneBitmapArray;
|
|
self->climbIncr = climbIncr;
|
|
self->diveIncr = diveIncr;
|
|
self->directionFunctor = directionFunctor;
|
|
}
|
|
|
|
|
|
// Table that represents the gravity that is applied to a plane
|
|
// that is pointing up 45, 67.5, 90, 112.5 or 135 degrees from
|
|
// the positive x axis.
|
|
// The constants are 256 * sin(angle) * 0.075.
|
|
//
|
|
static const sword gravityTable[5] =
|
|
{
|
|
16, 21, 23, 21, 16,
|
|
};
|
|
|
|
|
|
// Returns the contribution of gravity to the speed, in 256ths of a pixel.
|
|
// The angle of the plane is given by biplane->bitmapIndex.
|
|
//
|
|
static sword
|
|
BiplaneMover_computeGravity(Sprite *biplane)
|
|
{
|
|
byte bi = biplane->bitmapIndex;
|
|
if (bi < 2 || bi > 6) // if not 45..135 degrees
|
|
return 0;
|
|
return gravityTable[bi - 2];
|
|
}
|
|
|
|
|
|
static sword
|
|
patchHorizSpeed(sword dx)
|
|
{
|
|
if (dx > 0 && (byte) dx & 1)
|
|
return dx + 1;
|
|
if (dx < 0 && (byte) dx & 1)
|
|
return dx - 1;
|
|
return dx;
|
|
}
|
|
|
|
|
|
BOOL
|
|
BiplaneMover_move(BiplaneMover *self)
|
|
{
|
|
assert(self);
|
|
if (!self->hasStarted)
|
|
return FALSE;
|
|
|
|
Sprite *biplane = self->biplane;
|
|
assert(biplane);
|
|
if (!biplane->active)
|
|
return FALSE;
|
|
|
|
BOOL explosionRequested = FALSE;
|
|
byte *bitmapIndex = &biplane->bitmapIndex;
|
|
assert(*bitmapIndex < NUM_BIPLANE_BITMAPS);
|
|
|
|
byte direction = (*self->directionFunctor)();
|
|
|
|
assert(direction == DIR_FORWARD || direction == DIR_CLIMB || direction == DIR_DIVE);
|
|
if (direction == DIR_CLIMB)
|
|
{
|
|
*bitmapIndex = (*bitmapIndex + self->climbIncr) % NUM_BIPLANE_BITMAPS;
|
|
biplane->bm = self->biplaneBitmapArray[*bitmapIndex];
|
|
}
|
|
else if (direction == DIR_DIVE)
|
|
{
|
|
*bitmapIndex = (*bitmapIndex + self->diveIncr) % NUM_BIPLANE_BITMAPS;
|
|
biplane->bm = self->biplaneBitmapArray[*bitmapIndex];
|
|
}
|
|
assert(*bitmapIndex < NUM_BIPLANE_BITMAPS);
|
|
assert(biplane->bm);
|
|
assert(biplane->bm->height == BIPLANE_HEIGHT);
|
|
|
|
// *bitmapIndex gives the orientation of the plane.
|
|
// Get a speed vector from it.
|
|
// Add the speed to the sprite position, with edge detection, clamping
|
|
// and horizontal wraparound.
|
|
|
|
sword dx, dy; // change of position to be applied to plane, in whole pixels
|
|
|
|
if (! self->isStalled)
|
|
{
|
|
sword g = BiplaneMover_computeGravity(biplane);
|
|
assert(g >= 0);
|
|
self->speed -= g;
|
|
self->speed += ACCELERATION_256THS;
|
|
if (self->speed > MAX_SPEED_256THS)
|
|
self->speed = MAX_SPEED_256THS;
|
|
else if (self->speed < MIN_SPEED_256THS)
|
|
{
|
|
// Stall b/c speed dropped too low from climbing.
|
|
self->speed = MIN_SPEED_256THS;
|
|
self->isStalled = TRUE;
|
|
}
|
|
|
|
sword c, s;
|
|
cosSin256ths(*bitmapIndex, &c, &s);
|
|
dx = mul256thsToByte(self->speed, c);
|
|
dx = patchHorizSpeed(dx);
|
|
dy = - mul256thsToByte(self->speed, s); // negation b/c y axis points down
|
|
}
|
|
else // stalled:
|
|
{
|
|
// Plane must nose-dive to recover.
|
|
|
|
if (*bitmapIndex >= 10 && *bitmapIndex <= 14) // if 45..135 deg. down from pos. x axis
|
|
{
|
|
self->speed += 100; // build momentum in the dive
|
|
|
|
if (self->speed >= MIN_SPEED_256THS)
|
|
self->isStalled = FALSE; // airflow restored over wings
|
|
}
|
|
else
|
|
self->speed = 2 * 256; // heavy drag while tumbling stall
|
|
|
|
sword c = cos256ths(*bitmapIndex);
|
|
dx = mul256thsToByte(self->speed, c);
|
|
dx = patchHorizSpeed(dx);
|
|
|
|
dy = 3; // plane drops heavily due to loss of lift
|
|
}
|
|
assert(dx >= -20 && dx <= 20); // sanity check
|
|
assert(dy >= -20 && dy <= 20); // sanity check
|
|
|
|
// Change the position of 'biplane' by (dx, dy).
|
|
//
|
|
sword newX = biplane->x + dx;
|
|
if (newX > COLS_PER_SCREEN - 2 - SPRITE_WIDTH) // if past rightmost allowed pos
|
|
newX = 2; // reposition at leftmost allowed pos
|
|
else if (newX < 2)
|
|
newX = COLS_PER_SCREEN - 2 - SPRITE_WIDTH;
|
|
biplane->x = (byte) newX;
|
|
|
|
if (dy < 0 && biplane->y < (byte) - dy) // if too high
|
|
biplane->y = 0;
|
|
else if (dy > 0 // if too low
|
|
&& biplane->y + BIPLANE_HEIGHT + (byte) dy > ROWS_PER_SCREEN)
|
|
{
|
|
biplane->y = ROWS_PER_SCREEN - BIPLANE_HEIGHT;
|
|
biplane->active = FALSE;
|
|
explosionRequested = TRUE;
|
|
}
|
|
else
|
|
biplane->y += (sbyte) dy;
|
|
|
|
return explosionRequested;
|
|
}
|