anarch/main.c

4012 lines
106 KiB
C
Raw Normal View History

2019-10-03 18:04:14 -04:00
/**
@file main.c
Main source file of the game that puts together all the pieces. main game
logic is implemented here.
by Miloslav Ciz (drummyfish), 2019
Released under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)
plus a waiver of all other intellectual property. The goal of this work is
be and remain completely in the public domain forever, available for any use
whatsoever.
*/
2019-09-25 09:51:19 -04:00
#include <stdint.h>
2019-10-02 08:42:30 -04:00
/*
The following keys are mandatory to be implemented on any platform in order
for the game to be playable.
*/
2019-09-25 09:51:19 -04:00
#define SFG_KEY_UP 0
#define SFG_KEY_RIGHT 1
#define SFG_KEY_DOWN 2
#define SFG_KEY_LEFT 3
#define SFG_KEY_A 4
#define SFG_KEY_B 5
#define SFG_KEY_C 6
2019-10-14 09:56:26 -04:00
2019-10-02 08:42:30 -04:00
/*
The following keys are optional for a platform to implement. They just make
the controls more comfortable.
*/
#define SFG_KEY_JUMP 7
#define SFG_KEY_STRAFE_LEFT 8
#define SFG_KEY_STRAFE_RIGHT 9
2019-10-08 07:46:12 -04:00
#define SFG_KEY_MAP 10
2020-01-18 06:27:20 -05:00
#define SFG_KEY_TOGGLE_FREELOOK 11
#define SFG_KEY_NEXT_WEAPON 12
#define SFG_KEY_PREVIOUS_WEAPON 13
2020-02-16 07:20:22 -05:00
#define SFG_KEY_MENU 14
2019-09-25 09:51:19 -04:00
2020-02-16 07:20:22 -05:00
#define SFG_KEY_COUNT 15 ///< Number of keys.
2019-10-14 09:56:26 -04:00
2019-09-25 09:51:19 -04:00
/* ============================= PORTING =================================== */
2019-09-27 06:46:44 -04:00
/* When porting, do the following:
- implement the following functions in your platform_*.h.
- Call SFG_init() from your platform initialization code.
- Call SFG_mainLoopBody() from within your platform's main loop.
- include "settings.h" in your platform_*.h and optionally hard-override
(redefine) some settings in platform_*.h, according to the platform's
needs.
*/
2019-09-25 09:51:19 -04:00
2019-09-29 07:50:40 -04:00
#define SFG_LOG(str) ; ///< Can be redefined to log messages for better debug.
2019-10-02 14:31:27 -04:00
#define SFG_PROGRAM_MEMORY static const /**< Can be redefined to platform's
specifier of program meory. */
2020-01-02 07:16:30 -05:00
/**
Returns 1 (0) if given key is pressed (not pressed). At least the mandatory
2019-10-02 08:42:30 -04:00
keys have to be implemented, the optional keys don't have to ever return 1.
2020-01-02 07:16:30 -05:00
See the key contant definitions to see which ones are mandatory.
*/
2019-09-25 09:51:19 -04:00
int8_t SFG_keyPressed(uint8_t key);
2020-01-18 05:10:05 -05:00
/**
Optinal function for mouse/analog controls, gets mouse x and y offset in
pixels from the game screen center (to achieve classic FPS mouse controls the
platform should center the mouse at the end). If the platform isn't using a
mouse, this function should simply return [0,0] offets at each call.
*/
void SFG_getMouseOffset(int16_t *x, int16_t *y);
2020-01-02 07:16:30 -05:00
/**
Returns time in ms sice program start.
*/
2019-09-25 09:51:19 -04:00
uint32_t SFG_getTimeMs();
2020-01-02 07:16:30 -05:00
/**
Sleep (yield CPU) for specified amount of ms. This is used to relieve CPU
2019-09-25 09:51:19 -04:00
usage. If your platform doesn't need this or handles it in other way, this
2020-01-02 07:16:30 -05:00
function can do nothing.
*/
2019-09-25 09:51:19 -04:00
void SFG_sleepMs(uint16_t timeMs);
2020-01-02 07:16:30 -05:00
/**
Set specified screen pixel. The function doesn't have to check whether
the coordinates are within screen.
*/
2019-09-25 09:51:19 -04:00
static inline void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex);
2020-02-04 11:17:00 -05:00
/**
2020-02-06 02:30:12 -05:00
Play given sound effect (SFX). This function may or may not use the sound
samples provided in sounds.h, and it may or may not ignore the volume
parameter (which is 0 to 255). Depending on the platform the function can play
completely different samples or even e.g. just beeps. If the platform can't
2020-02-06 10:39:25 -05:00
play sounds, this function implementation can simply be left empty. This
function doesn't have to implement safety measures, the back end takes cares
of them.
2020-02-04 11:17:00 -05:00
*/
void SFG_playSound(uint8_t soundIndex, uint8_t volume);
2019-09-25 09:51:19 -04:00
/* ========================================================================= */
/**
Game main loop body, call this inside the platform's specific main loop.
*/
void SFG_mainLoopBody();
/**
Initializes the whole program, call this in the platform initialization.
*/
void SFG_init();
2019-09-27 10:38:55 -04:00
#ifdef SFG_PLATFORM_POKITTO
#include "platform_pokitto.h"
#else
#include "platform_sdl.h"
#endif
2019-09-25 09:51:19 -04:00
2019-10-04 15:09:10 -04:00
#define RCL_PIXEL_FUNCTION SFG_pixelFunc
#define RCL_TEXTURE_VERTICAL_STRETCH 0
#define RCL_CAMERA_COLL_HEIGHT_BELOW 800
2020-02-08 14:53:10 -05:00
#define RCL_CAMERA_COLL_HEIGHT_ABOVE 200
2019-10-04 15:09:10 -04:00
2020-03-10 14:56:11 -04:00
#if SFG_TEXTURE_DISTANCE == 0
#define RCL_COMPUTE_WALL_TEXCOORDS 0
#endif
2019-10-04 15:09:10 -04:00
#include "raycastlib.h"
2019-10-02 14:31:27 -04:00
2020-02-12 06:41:23 -05:00
#include "texts.h"
2020-02-04 11:17:00 -05:00
#include "images.h"
2019-12-30 14:44:14 -05:00
#include "levels.h"
#include "palette.h"
#include "settings.h" // will include if not included by platform
#include "constants.h"
2019-10-01 14:25:21 -04:00
2019-10-02 14:31:27 -04:00
typedef struct
{
uint8_t coords[2];
uint8_t state; /**< door state in format:
2019-10-04 11:18:46 -04:00
MSB ccbaaaaa LSB
2019-10-02 14:31:27 -04:00
aaaaa: current door height (how much they're open)
b: whether currently going up (0) or down (1)
2020-02-08 15:41:05 -05:00
cc: by which card (key) the door is unlocked, 00
means no card (unlocked), 1 means card 0 etc.
2019-10-02 14:31:27 -04:00
*/
2019-10-04 11:18:46 -04:00
} SFG_DoorRecord;
2019-10-02 14:31:27 -04:00
2020-01-02 07:16:30 -05:00
#define SFG_SPRITE_SIZE(size0to3) \
(((size0to3 + 3) * SFG_BASE_SPRITE_SIZE) / 4)
2020-02-27 10:13:19 -05:00
#define SFG_SPRITE_SIZE_PIXELS(size0to3) \
((SFG_SPRITE_SIZE(size0to3) * SFG_GAME_RESOLUTION_Y) / RCL_UNITS_PER_SQUARE)
2020-02-27 06:07:27 -05:00
#define SFG_SPRITE_SIZE_TO_HEIGHT_ABOVE_GROUND(size0to3) \
2020-02-27 10:13:19 -05:00
(SFG_SPRITE_SIZE(size0to3) / 2)
2020-01-02 07:16:30 -05:00
2019-10-06 10:16:59 -04:00
/**
Holds information about one instance of a level item (a type of level element,
e.g. pickable items, decorations etc.). The format is following:
MSB abbbbbbb LSB
a: active flag, 1 means the item is nearby to player and is active
bbbbbbb: index to elements array of the current level, pointing to element
representing this item
*/
typedef uint8_t SFG_ItemRecord;
2019-12-30 14:44:14 -05:00
#define SFG_ITEM_RECORD_ACTIVE_MASK 0x80
2020-01-07 15:26:38 -05:00
#define SFG_ITEM_RECORD_LEVEL_ELEMENT(itemRecord) \
(SFG_currentLevel.levelPointer->elements[itemRecord & \
~SFG_ITEM_RECORD_ACTIVE_MASK])
2019-10-17 15:19:32 -04:00
typedef struct
{
uint8_t stateType; /**< Holds state (lower 4 bits) and type of monster
(upper 4 bits). */
2019-10-21 17:50:19 -04:00
uint8_t coords[2]; /**< Monster position, in 1/4s of a square */
2019-10-17 15:19:32 -04:00
uint8_t health;
} SFG_MonsterRecord;
2019-12-26 09:28:23 -05:00
#define SFG_MR_STATE(mr) ((mr).stateType & SFG_MONSTER_MASK_STATE)
2020-03-28 15:48:16 -04:00
#define SFG_MR_TYPE(mr) \
(SFG_MONSTER_INDEX_TO_TYPE(((mr).stateType & SFG_MONSTER_MASK_TYPE) >> 4))
2019-12-26 09:28:23 -05:00
2019-10-21 17:50:19 -04:00
#define SFG_MONSTER_COORD_TO_RCL_UNITS(c) (c * 256)
2019-10-21 19:34:40 -04:00
#define SFG_MONSTER_COORD_TO_SQUARES(c) (c / 4)
2019-10-21 17:50:19 -04:00
2019-10-22 06:13:10 -04:00
#define SFG_ELEMENT_COORD_TO_RCL_UNITS(c) \
(c * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2)
2019-10-18 09:25:07 -04:00
#define SFG_MONSTER_MASK_STATE 0x0f
#define SFG_MONSTER_MASK_TYPE 0xf0
2019-10-17 15:19:32 -04:00
#define SFG_MONSTER_STATE_INACTIVE 0 ///< Not nearby, not actively updated.
#define SFG_MONSTER_STATE_IDLE 1
#define SFG_MONSTER_STATE_ATTACKING 2
#define SFG_MONSTER_STATE_HURTING 3
#define SFG_MONSTER_STATE_DYING 4
#define SFG_MONSTER_STATE_GOING_N 5
#define SFG_MONSTER_STATE_GOING_NE 6
2019-10-18 09:25:07 -04:00
#define SFG_MONSTER_STATE_GOING_E 7
2019-10-18 10:34:51 -04:00
#define SFG_MONSTER_STATE_GOING_SE 8
2019-10-18 09:25:07 -04:00
#define SFG_MONSTER_STATE_GOING_S 9
#define SFG_MONSTER_STATE_GOING_SW 10
#define SFG_MONSTER_STATE_GOING_W 11
#define SFG_MONSTER_STATE_GOING_NW 12
2019-10-17 15:19:32 -04:00
2019-10-20 17:04:28 -04:00
typedef struct
{
uint8_t type;
2019-10-21 13:58:11 -04:00
uint8_t doubleFramesToLive; /**< This number times two (because 256 could be
too little at high FPS) says after how many
frames the projectile is destroyed. */
2019-10-20 17:04:28 -04:00
uint16_t position[3]; /**< Current position, stored as u16 to save space, as
that is exactly enough to store position on 64x64
map. */
int16_t direction[3]; /**< Added to position each game step. */
2019-10-21 08:54:32 -04:00
} SFG_ProjectileRecord;
2020-02-12 02:18:38 -05:00
#define SFG_GAME_STATE_MENU 0
#define SFG_GAME_STATE_PLAYING 1
2020-02-13 02:23:44 -05:00
#define SFG_GAME_STATE_WIN 2
#define SFG_GAME_STATE_LOSE 3
#define SFG_GAME_STATE_INTRO 4
#define SFG_GAME_STATE_OUTRO 5
2020-02-16 07:20:22 -05:00
#define SFG_GAME_STATE_MAP 6
2020-02-12 02:18:38 -05:00
2020-02-12 06:41:23 -05:00
#define SFG_MENU_ITEM_CONTINUE 0
#define SFG_MENU_ITEM_MAP 1
#define SFG_MENU_ITEM_PLAY 2
#define SFG_MENU_ITEM_LOAD 3
#define SFG_MENU_ITEM_SOUND 4
#define SFG_MENU_ITEM_EXIT 5
#define SFG_MENU_ITEM_NONE 255
2019-10-14 14:21:18 -04:00
/*
GLOBAL VARIABLES
===============================================================================
*/
2019-10-06 10:47:47 -04:00
2020-02-10 12:10:06 -05:00
/**
Groups global variables related to the game as such in a single struct. There
are still other global structs for player, level etc.
*/
struct
{
2020-02-12 02:18:38 -05:00
uint8_t state;
2020-02-13 02:23:44 -05:00
uint16_t stateChangeTime; ///< Time in ms at which the state was changed.
2020-02-12 02:18:38 -05:00
2020-02-10 12:10:06 -05:00
uint8_t currentRandom; ///< for RNG
uint8_t spriteAnimationFrame;
2020-04-01 06:20:18 -04:00
uint8_t soundsPlayedThisFrame; /**< Each bit says whether given sound was
played this frame, prevents playing too many
sounds at once. */
2020-03-30 12:52:54 -04:00
2020-02-10 12:10:06 -05:00
RCL_RayConstraints rayConstraints;
2020-02-12 02:18:38 -05:00
uint8_t keyStates[SFG_KEY_COUNT]; /**< Pressed states of keys, each value
stores the number of frames for which the
key has been held. */
2020-02-10 12:10:06 -05:00
uint8_t zBuffer[SFG_Z_BUFFER_SIZE];
2020-03-10 14:56:11 -04:00
uint8_t textureAverageColors[SFG_WALL_TEXTURE_COUNT]; /**< Contains average
color for each wall texture. */
2020-02-10 12:10:06 -05:00
int8_t backgroundScaleMap[SFG_GAME_RESOLUTION_Y];
uint16_t backgroundScroll;
uint8_t spriteSamplingPoints[SFG_MAX_SPRITE_SIZE]; /**< Helper for
precomputing sprite
sampling positions for
drawing. */
uint32_t frameTime; ///< Keeps a constant time (in ms) during a frame
uint32_t frame;
uint32_t lastFrameTimeMs;
2020-02-12 02:18:38 -05:00
uint8_t selectedMenuItem;
2020-02-12 08:44:51 -05:00
uint8_t selectedLevel; ///< Level to play selected in the main menu.
2020-03-28 12:34:09 -04:00
uint8_t antiSpam; ///< Prevents log message spamming.
2020-02-10 12:10:06 -05:00
} SFG_game;
2019-10-22 10:48:27 -04:00
2020-02-10 12:10:06 -05:00
/**
Stores player state.
*/
2019-10-14 14:21:18 -04:00
struct
{
RCL_Camera camera;
int8_t squarePosition[2];
RCL_Vector2D direction;
RCL_Unit verticalSpeed;
RCL_Unit previousVerticalSpeed; /**< Vertical speed in previous frame, needed
for determining whether player is in the
air. */
uint16_t headBobFrame;
2020-01-13 12:59:36 -05:00
uint8_t weapon; ///< currently selected weapon
2019-10-22 15:02:39 -04:00
2020-01-13 12:59:36 -05:00
uint8_t health;
2019-10-25 16:41:52 -04:00
2020-01-01 11:06:11 -05:00
uint32_t weaponCooldownStartFrame; /**< frame from which weapon cooldown is
counted */
2020-01-12 10:48:42 -05:00
uint32_t lastHurtFrame;
uint32_t lastItemTakenFrame;
2020-01-13 12:59:36 -05:00
uint8_t ammo[SFG_AMMO_TOTAL];
2020-01-18 06:27:20 -05:00
uint8_t freeLook; /**< If on, the vertical looking (shear) does
not automatically shear back. This is mainly
for mouse control. */
2020-02-08 14:53:10 -05:00
uint8_t cards; /**< Lowest bits say which access cards have
been taken. */
2020-02-09 09:03:15 -05:00
uint8_t justTeleported;
2019-10-14 14:21:18 -04:00
} SFG_player;
2019-09-26 15:15:07 -04:00
/**
Stores the current level and helper precomputed vaues for better performance.
*/
struct
{
const SFG_Level *levelPointer;
const uint8_t* textures[7];
2019-10-06 10:16:59 -04:00
2019-09-26 21:34:49 -04:00
uint32_t timeStart;
2019-10-04 11:42:54 -04:00
uint32_t frameStart;
2019-10-06 10:16:59 -04:00
2019-09-29 14:26:53 -04:00
uint8_t floorColor;
uint8_t ceilingColor;
2019-10-06 10:16:59 -04:00
SFG_DoorRecord doorRecords[SFG_MAX_DOORS];
2019-10-02 14:31:27 -04:00
uint8_t doorRecordCount;
2019-10-04 11:42:54 -04:00
uint8_t checkedDoorIndex; ///< Says which door are currently being checked.
2019-10-06 10:16:59 -04:00
2019-10-17 15:19:32 -04:00
SFG_ItemRecord itemRecords[SFG_MAX_ITEMS]; ///< Holds level items
2019-10-06 10:16:59 -04:00
uint8_t itemRecordCount;
2019-10-06 10:47:47 -04:00
uint8_t checkedItemIndex; ///< Same as checkedDoorIndex, but for items.
2019-10-17 15:19:32 -04:00
SFG_MonsterRecord monsterRecords[SFG_MAX_MONSTERS];
uint8_t monsterRecordCount;
uint8_t checkedMonsterIndex;
2019-10-21 08:54:32 -04:00
SFG_ProjectileRecord projectileRecords[SFG_MAX_PROJECTILES];
uint8_t projectileRecordCount;
2020-01-02 18:34:50 -05:00
uint8_t backgroundImage;
2020-02-09 09:03:15 -05:00
uint8_t teleportCount;
2020-02-21 15:21:09 -05:00
uint16_t mapRevealMask; /**< Bits say which parts of the map have been
revealed. */
2020-02-22 17:36:15 -05:00
uint8_t itemCollisionMap[(SFG_MAP_SIZE * SFG_MAP_SIZE) / 8];
/**< Bit array, for each map square says whether there
is a colliding item or not. */
2019-09-26 15:15:07 -04:00
} SFG_currentLevel;
2020-02-23 05:51:56 -05:00
/**
Helper function for accessing the itemCollisionMap bits.
*/
2020-02-22 17:36:15 -05:00
void SFG_getItemCollisionMapIndex(
uint8_t x, uint8_t y, uint16_t *byte, uint8_t *bit)
{
uint16_t index = y * SFG_MAP_SIZE + x;
*byte = index / 8;
*bit = index % 8;
}
void SFG_setItemCollisionMapBit(uint8_t x, uint8_t y, uint8_t value)
{
uint16_t byte;
uint8_t bit;
SFG_getItemCollisionMapIndex(x,y,&byte,&bit);
SFG_currentLevel.itemCollisionMap[byte] &= ~(0x01 << bit);
SFG_currentLevel.itemCollisionMap[byte] |= (value & 0x01) << bit;
}
uint8_t SFG_getItemCollisionMapBit(uint8_t x, uint8_t y)
{
uint16_t byte;
uint8_t bit;
SFG_getItemCollisionMapIndex(x,y,&byte,&bit);
return (SFG_currentLevel.itemCollisionMap[byte] >> bit) & 0x01;
}
2019-10-03 17:37:52 -04:00
#if SFG_DITHERED_SHADOW
SFG_PROGRAM_MEMORY uint8_t SFG_ditheringPatterns[] =
{
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,1,0,0,
0,0,0,0,
0,1,0,1,
1,0,1,0,
0,1,0,0,
1,0,1,0,
0,1,0,1,
1,0,1,0,
0,1,1,1,
1,1,1,1,
0,1,0,1,
1,1,1,1,
0,1,1,1,
1,1,1,1,
1,1,1,1
};
#endif
2019-10-14 14:21:18 -04:00
/*
FUNCTIONS
===============================================================================
*/
2019-10-18 10:34:51 -04:00
/**
Returns a pseudorandom byte. This is a congrent generator, its parameters
have been chosen so that each number (0-255) is included in the output
exactly once!
*/
uint8_t SFG_random()
{
2020-02-10 12:10:06 -05:00
SFG_game.currentRandom *= 13;
SFG_game.currentRandom += 7;
2019-10-18 10:34:51 -04:00
2020-02-10 12:10:06 -05:00
return SFG_game.currentRandom;
2019-10-18 10:34:51 -04:00
}
2020-02-06 10:39:25 -05:00
void SFG_playSoundSafe(uint8_t soundIndex, uint8_t volume)
{
2020-04-01 06:20:18 -04:00
uint8_t mask = 0x01 << soundIndex;
if (!(SFG_game.soundsPlayedThisFrame & mask))
2020-03-30 12:52:54 -04:00
{
2020-02-06 10:39:25 -05:00
SFG_playSound(soundIndex,volume);
2020-04-01 06:20:18 -04:00
SFG_game.soundsPlayedThisFrame |= mask;
}
2020-02-06 10:39:25 -05:00
}
2020-01-02 11:22:00 -05:00
/**
Returns a damage value for specific attack type (SFG_WEAPON_FIRE_TYPE_...),
with added randomness (so the values will differe). For explosion pass
SFG_WEAPON_FIRE_TYPE_FIREBALL.
*/
uint8_t SFG_getDamageValue(uint8_t attackType)
{
if (attackType >= SFG_WEAPON_FIRE_TYPES_TOTAL)
return 0;
int32_t value = SFG_attackDamageTable[attackType]; // has to be signed
int32_t maxAdd = (value * SFG_DAMAGE_RANDOMNESS) / 256;
2020-01-11 13:22:14 -05:00
value = value + (maxAdd / 2) - (SFG_random() * maxAdd / 256);
2020-01-02 11:22:00 -05:00
if (value < 0)
value = 0;
return value;
}
2020-01-13 12:59:36 -05:00
/**
Returns ammo type for given weapon.
*/
uint8_t SFG_weaponAmmo(uint8_t weapon)
{
if (weapon == SFG_WEAPON_KNIFE)
return SFG_AMMO_NONE;
if (weapon == SFG_WEAPON_MACHINE_GUN ||
weapon == SFG_WEAPON_SHOTGUN)
return SFG_AMMO_BULLETS;
else if (weapon == SFG_WEAPON_ROCKET_LAUNCHER)
return SFG_AMMO_ROCKETS;
else
return SFG_AMMO_PLASMA;
}
2020-01-07 15:26:38 -05:00
RCL_Unit SFG_taxicabDistance(
RCL_Unit x0, RCL_Unit y0, RCL_Unit z0, RCL_Unit x1, RCL_Unit y1, RCL_Unit z1)
2019-10-21 19:34:40 -04:00
{
2020-01-07 15:26:38 -05:00
return (RCL_absVal(x0 - x1) + RCL_absVal(y0 - y1) + RCL_absVal(z0 - z1));
2019-10-21 19:34:40 -04:00
}
2020-01-11 12:05:27 -05:00
uint8_t SFG_isInActiveDistanceFromPlayer(RCL_Unit x, RCL_Unit y, RCL_Unit z)
{
return SFG_taxicabDistance(
x,y,z,SFG_player.camera.position.x,SFG_player.camera.position.y,
SFG_player.camera.height) <= SFG_LEVEL_ELEMENT_ACTIVE_DISTANCE;
}
2019-10-22 17:59:10 -04:00
static inline uint8_t SFG_RCLUnitToZBuffer(RCL_Unit x)
2019-10-18 11:06:38 -04:00
{
2020-02-25 11:03:40 -05:00
x /= (RCL_UNITS_PER_SQUARE / 8);
2019-10-18 11:06:38 -04:00
2020-02-25 11:03:40 -05:00
uint8_t okay = x < 256;
2019-10-18 11:06:38 -04:00
return okay * (x + 1) - 1;
2019-10-18 11:41:55 -04:00
}
2019-10-18 11:06:38 -04:00
2019-10-22 18:11:26 -04:00
const uint8_t *SFG_getMonsterSprite(
uint8_t monsterType, uint8_t state, uint8_t frame)
{
2019-12-29 09:06:56 -05:00
uint8_t index = 17; // makes the code smaller compared to returning pointers
if (state != SFG_MONSTER_STATE_DYING)
switch (monsterType)
{
case SFG_LEVEL_ELEMENT_MONSTER_SPIDER:
switch (state)
{
case SFG_MONSTER_STATE_ATTACKING: index = 1; break;
case SFG_MONSTER_STATE_IDLE: index = 0; break;
default: index = frame ? 0 : 2; break;
}
break;
case SFG_LEVEL_ELEMENT_MONSTER_WARRIOR:
index = state != SFG_MONSTER_STATE_ATTACKING ? 6 : 7;
break;
case SFG_LEVEL_ELEMENT_MONSTER_DESTROYER:
switch (state)
{
case SFG_MONSTER_STATE_ATTACKING: index = 4; break;
case SFG_MONSTER_STATE_IDLE: index = 3; break;
default: index = frame ? 3 : 5; break;
}
break;
case SFG_LEVEL_ELEMENT_MONSTER_PLASMABOT:
index = state != SFG_MONSTER_STATE_ATTACKING ? 8 : 9;
break;
case SFG_LEVEL_ELEMENT_MONSTER_ENDER:
switch (state)
{
case SFG_MONSTER_STATE_ATTACKING: index = 12; break;
case SFG_MONSTER_STATE_IDLE: index = 10; break;
default: index = frame ? 10 : 11; break;
}
break;
case SFG_LEVEL_ELEMENT_MONSTER_TURRET:
switch (state)
{
case SFG_MONSTER_STATE_ATTACKING: index = 15; break;
case SFG_MONSTER_STATE_IDLE: index = 13; break;
default: index = frame ? 13 : 14; break;
}
break;
case SFG_LEVEL_ELEMENT_MONSTER_EXPLODER:
default:
index = 16;
break;
}
2019-12-27 11:17:14 -05:00
return SFG_monsterSprites[index];
2019-10-22 18:11:26 -04:00
}
2019-10-14 14:21:18 -04:00
/**
Says whether given key is currently pressed (down). This should be preferred
to SFG_keyPressed().
*/
uint8_t SFG_keyIsDown(uint8_t key)
{
2020-02-12 02:18:38 -05:00
return SFG_game.keyStates[key] != 0;
2019-10-14 14:21:18 -04:00
}
/**
Says whether given key has been pressed in the current frame.
*/
uint8_t SFG_keyJustPressed(uint8_t key)
{
2020-02-12 02:18:38 -05:00
return (SFG_game.keyStates[key]) == 1;
2019-10-14 14:21:18 -04:00
}
2020-02-12 06:41:23 -05:00
/**
Says whether a key is being repeated after being held for certain time.
*/
uint8_t SFG_keyRepeated(uint8_t key)
{
2020-02-12 08:44:51 -05:00
return
((SFG_game.keyStates[key] >= SFG_KEY_REPEAT_DELAY_FRAMES) ||
(SFG_game.keyStates[key] == 255)) &&
(SFG_game.frame % SFG_KEY_REPEAT_PERIOD_FRAMES == 0);
}
2020-02-12 06:41:23 -05:00
2020-02-12 08:44:51 -05:00
uint16_t SFG_keyRegisters(uint8_t key)
{
return SFG_keyJustPressed(key) || SFG_keyRepeated(key);
2020-02-12 06:41:23 -05:00
}
2019-10-14 14:21:18 -04:00
#if SFG_RESOLUTION_SCALEDOWN == 1
#define SFG_setGamePixel SFG_setPixel
#else
/**
Sets the game pixel (a pixel that can potentially be bigger than the screen
pixel).
*/
static inline void SFG_setGamePixel(uint16_t x, uint16_t y, uint8_t colorIndex)
{
uint16_t screenY = y * SFG_RESOLUTION_SCALEDOWN;
uint16_t screenX = x * SFG_RESOLUTION_SCALEDOWN;
for (uint16_t j = screenY; j < screenY + SFG_RESOLUTION_SCALEDOWN; ++j)
for (uint16_t i = screenX; i < screenX + SFG_RESOLUTION_SCALEDOWN; ++i)
SFG_setPixel(i,j,colorIndex);
}
#endif
2020-03-22 14:51:51 -04:00
void SFG_recomputePLayerDirection()
2019-10-14 14:21:18 -04:00
{
2020-02-12 10:52:18 -05:00
SFG_player.camera.direction =
RCL_wrap(SFG_player.camera.direction,RCL_UNITS_PER_SQUARE);
2019-10-14 14:21:18 -04:00
SFG_player.direction = RCL_angleToDirection(SFG_player.camera.direction);
SFG_player.direction.x =
(SFG_player.direction.x * SFG_PLAYER_MOVE_UNITS_PER_FRAME)
/ RCL_UNITS_PER_SQUARE;
SFG_player.direction.y =
(SFG_player.direction.y * SFG_PLAYER_MOVE_UNITS_PER_FRAME)
/ RCL_UNITS_PER_SQUARE;
2020-02-10 12:10:06 -05:00
SFG_game.backgroundScroll =
2019-10-14 14:21:18 -04:00
((SFG_player.camera.direction * 8) * SFG_GAME_RESOLUTION_Y)
/ RCL_UNITS_PER_SQUARE;
}
2019-12-29 15:09:50 -05:00
#if SFG_BACKGROUND_BLUR != 0
uint8_t SFG_backgroundBlurIndex = 0;
SFG_PROGRAM_MEMORY int8_t SFG_backgroundBlurOffsets[9] =
2020-02-12 06:42:30 -05:00
{
0 * SFG_BACKGROUND_BLUR,
16 * SFG_BACKGROUND_BLUR,
7 * SFG_BACKGROUND_BLUR,
17 * SFG_BACKGROUND_BLUR,
1 * SFG_BACKGROUND_BLUR,
4 * SFG_BACKGROUND_BLUR,
15 * SFG_BACKGROUND_BLUR,
9 * SFG_BACKGROUND_BLUR,
7 * SFG_BACKGROUND_BLUR
2019-12-29 15:09:50 -05:00
};
#endif
2020-02-22 03:23:11 -05:00
static inline uint8_t SFG_fogValueDiminish(RCL_Unit depth)
2020-02-01 17:17:22 -05:00
{
return depth / SFG_FOG_DIMINISH_STEP;
}
2020-03-10 14:56:11 -04:00
static inline uint8_t SFG_getTexelFull(uint8_t textureIndex,RCL_Unit u, RCL_Unit v)
{
return
SFG_getTexel(
textureIndex != 255 ?
SFG_currentLevel.textures[textureIndex] :
SFG_wallTextures[SFG_currentLevel.levelPointer->doorTextureIndex],
u / 32,
v / 32);
}
static inline uint8_t SFG_getTexelAverage(uint8_t textureIndex)
{
return
textureIndex != 255 ?
SFG_game.textureAverageColors[
SFG_currentLevel.levelPointer->textureIndices[textureIndex]]
:
(
SFG_game.textureAverageColors[
SFG_currentLevel.levelPointer->doorTextureIndex]
+ 1 // to distinguish from normal walls
);
}
2019-09-25 09:51:19 -04:00
void SFG_pixelFunc(RCL_PixelInfo *pixel)
{
uint8_t color;
uint8_t shadow = 0;
2019-09-29 10:08:38 -04:00
if (pixel->isHorizon && pixel->depth > RCL_UNITS_PER_SQUARE * 16)
{
color = SFG_TRANSPARENT_COLOR;
}
else if (pixel->isWall)
2019-09-25 09:51:19 -04:00
{
2019-09-26 15:15:07 -04:00
uint8_t textureIndex =
pixel->isFloor ?
2019-09-27 13:04:49 -04:00
(
((pixel->hit.type & SFG_TILE_PROPERTY_MASK) != SFG_TILE_PROPERTY_DOOR) ?
(pixel->hit.type & 0x7)
:
(
2019-09-28 15:12:16 -04:00
(pixel->texCoords.y > RCL_UNITS_PER_SQUARE) ?
2019-09-29 10:08:38 -04:00
(pixel->hit.type & 0x7) : 255
2019-09-27 13:04:49 -04:00
)
):
2019-09-26 15:15:07 -04:00
((pixel->hit.type & 0x38) >> 3);
2019-09-29 14:17:31 -04:00
2020-03-10 14:56:11 -04:00
#if SFG_TEXTURE_DISTANCE != 0
2019-09-27 20:08:28 -04:00
RCL_Unit textureV = pixel->texCoords.y;
2019-09-26 22:41:45 -04:00
2019-09-27 13:04:49 -04:00
if ((pixel->hit.type & SFG_TILE_PROPERTY_MASK) ==
SFG_TILE_PROPERTY_SQUEEZER)
2019-09-27 09:24:36 -04:00
textureV += pixel->wallHeight;
2020-03-10 14:56:11 -04:00
#endif
2019-09-26 22:41:45 -04:00
2019-09-25 19:34:57 -04:00
color =
2019-09-26 15:15:07 -04:00
textureIndex != SFG_TILE_TEXTURE_TRANSPARENT ?
2020-03-10 14:56:11 -04:00
(
#if SFG_TEXTURE_DISTANCE >= 65535
SFG_getTexelFull(textureIndex,pixel->texCoords.x,textureV)
#elif SFG_TEXTURE_DISTANCE == 0
SFG_getTexelAverage(textureIndex)
#else
pixel->depth <= SFG_TEXTURE_DISTANCE ?
SFG_getTexelFull(textureIndex,pixel->texCoords.x,textureV) :
SFG_getTexelAverage(textureIndex)
#endif
)
:
2019-09-25 19:34:57 -04:00
SFG_TRANSPARENT_COLOR;
2019-09-25 09:51:19 -04:00
shadow = pixel->hit.direction >> 1;
}
else
{
2019-09-29 14:26:53 -04:00
color = pixel->isFloor ?
2019-09-30 13:39:21 -04:00
(SFG_currentLevel.floorColor) :
(pixel->height < SFG_CEILING_MAX_HEIGHT ?
SFG_currentLevel.ceilingColor : SFG_TRANSPARENT_COLOR);
2019-09-25 09:51:19 -04:00
}
2019-09-25 19:34:57 -04:00
if (color != SFG_TRANSPARENT_COLOR)
{
2019-09-26 19:25:22 -04:00
#if SFG_DITHERED_SHADOW
2020-02-01 17:17:22 -05:00
uint8_t fogShadow = (pixel->depth * 8) / SFG_FOG_DIMINISH_STEP;
2019-10-03 17:37:52 -04:00
uint8_t fogShadowPart = fogShadow & 0x07;
2019-09-26 19:25:22 -04:00
2019-10-03 17:37:52 -04:00
fogShadow /= 8;
2019-09-26 19:25:22 -04:00
2019-10-03 17:37:52 -04:00
uint8_t xMod4 = pixel->position.x & 0x03;
uint8_t yMod2 = pixel->position.y & 0x01;
2019-09-26 19:25:22 -04:00
2019-10-03 17:37:52 -04:00
shadow +=
fogShadow + SFG_ditheringPatterns[fogShadowPart * 8 + yMod2 * 4 + xMod4];
2019-09-26 19:25:22 -04:00
#else
2020-02-01 17:17:22 -05:00
shadow += SFG_fogValueDiminish(pixel->depth);
2019-09-26 19:25:22 -04:00
#endif
2019-10-04 16:40:41 -04:00
#if SFG_ENABLE_FOG
2019-09-25 19:34:57 -04:00
color = palette_minusValue(color,shadow);
2019-10-04 16:40:41 -04:00
#endif
2019-09-25 19:34:57 -04:00
}
else
{
2019-12-29 15:09:50 -05:00
color = SFG_getTexel(
2020-01-02 18:34:50 -05:00
SFG_backgroundImages[SFG_currentLevel.backgroundImage],
2020-02-10 12:10:06 -05:00
SFG_game.backgroundScaleMap[((pixel->position.x
2019-12-29 15:09:50 -05:00
#if SFG_BACKGROUND_BLUR != 0
+ SFG_backgroundBlurOffsets[SFG_backgroundBlurIndex]
#endif
2020-02-11 15:24:50 -05:00
) * SFG_RAYCASTING_SUBSAMPLE + SFG_game.backgroundScroll) % SFG_GAME_RESOLUTION_Y],
2020-02-10 12:10:06 -05:00
(SFG_game.backgroundScaleMap[(pixel->position.y // ^ TODO: get rid of mod?
2019-12-29 15:09:50 -05:00
#if SFG_BACKGROUND_BLUR != 0
+ SFG_backgroundBlurOffsets[SFG_backgroundBlurIndex + 1]
#endif
) % SFG_GAME_RESOLUTION_Y ])
);
#if SFG_BACKGROUND_BLUR != 0
SFG_backgroundBlurIndex = (SFG_backgroundBlurIndex + 1) % 0x07;
#endif
2019-09-25 19:34:57 -04:00
}
2019-09-25 09:51:19 -04:00
2019-09-28 15:42:02 -04:00
RCL_Unit screenX = pixel->position.x * SFG_RAYCASTING_SUBSAMPLE;
for (uint8_t i = 0; i < SFG_RAYCASTING_SUBSAMPLE; ++i)
{
2019-10-04 10:32:24 -04:00
SFG_setGamePixel(screenX,pixel->position.y,color);
2019-09-28 15:42:02 -04:00
screenX++;
}
2019-09-25 09:51:19 -04:00
}
2019-10-13 19:47:41 -04:00
/**
Draws image on screen, with transparency. This is faster than sprite drawing.
For performance sake drawing near screen edges is not pixel perfect.
*/
void SFG_blitImage(
const uint8_t *image,
int16_t posX,
int16_t posY,
uint8_t scale)
{
if (scale == 0)
return;
uint16_t x0 = posX,
x1,
y0 = posY,
y1;
uint8_t u0 = 0, v0 = 0;
if (posX < 0)
{
x0 = 0;
u0 = (-1 * posX) / scale;
}
posX += scale * SFG_TEXTURE_SIZE;
uint16_t limitX = SFG_GAME_RESOLUTION_X - scale;
uint16_t limitY = SFG_GAME_RESOLUTION_Y - scale;
x1 = posX >= 0 ?
(posX <= limitX ? posX : limitX)
: 0;
if (x1 >= SFG_GAME_RESOLUTION_X)
x1 = SFG_GAME_RESOLUTION_X - 1;
if (posY < 0)
{
y0 = 0;
v0 = (-1 * posY) / scale;
}
posY += scale * SFG_TEXTURE_SIZE;
y1 = posY >= 0 ?
(posY <= limitY ? posY : limitY)
: 0;
if (y1 >= SFG_GAME_RESOLUTION_Y)
y1 = SFG_GAME_RESOLUTION_Y - 1;
uint8_t u,v;
v = v0;
for (uint16_t y = y0; y < y1; y += scale)
{
u = u0;
for (uint16_t x = x0; x < x1; x += scale)
{
uint8_t color = SFG_getTexel(image,u,v);
if (color != SFG_TRANSPARENT_COLOR)
{
uint16_t sY = y;
for (uint8_t j = 0; j < scale; ++j)
{
uint16_t sX = x;
for (uint8_t i = 0; i < scale; ++i)
{
SFG_setGamePixel(sX,sY,color);
sX++;
}
sY++;
}
}
u++;
}
v++;
}
}
2019-10-06 19:18:42 -04:00
void SFG_drawScaledSprite(
2019-10-01 19:31:59 -04:00
const uint8_t *image,
int16_t centerX,
int16_t centerY,
2019-10-06 11:12:12 -04:00
int16_t size,
2019-10-06 19:18:42 -04:00
uint8_t minusValue,
RCL_Unit distance)
2019-10-01 19:31:59 -04:00
{
if ((size > SFG_MAX_SPRITE_SIZE) || (size == 0))
return;
uint16_t halfSize = size / 2;
int16_t topLeftX = centerX - halfSize;
int16_t topLeftY = centerY - halfSize;
int16_t x0, u0;
if (topLeftX < 0)
{
u0 = -1 * topLeftX;
x0 = 0;
}
else
{
u0 = 0;
x0 = topLeftX;
}
int16_t x1 = topLeftX + size - 1;
2019-10-04 10:32:24 -04:00
if (x1 >= SFG_GAME_RESOLUTION_X)
x1 = SFG_GAME_RESOLUTION_X - 1;
2019-10-01 19:31:59 -04:00
int16_t y0, v0;
if (topLeftY < 0)
{
v0 = -1 * topLeftY;
y0 = 0;
}
else
{
v0 = 0;
y0 = topLeftY;
}
int16_t y1 = topLeftY + size - 1;
2019-10-04 10:32:24 -04:00
if (y1 >= SFG_GAME_RESOLUTION_Y)
y1 = SFG_GAME_RESOLUTION_Y - 1;
2019-10-01 19:31:59 -04:00
2020-02-09 05:17:13 -05:00
if ((x0 > x1) || (y0 > y1) || (u0 >= size) || (v0 >= size)) // outside screen?
return;
2019-10-01 19:31:59 -04:00
int16_t u1 = u0 + (x1 - x0);
int16_t v1 = v0 + (y1 - y0);
// precompute sampling positions:
int16_t uMin = RCL_min(u0,u1);
int16_t vMin = RCL_min(v0,v1);
int16_t uMax = RCL_max(u0,u1);
int16_t vMax = RCL_max(v0,v1);
int16_t precompFrom = RCL_min(uMin,vMin);
int16_t precompTo = RCL_max(uMax,vMax);
2019-10-06 16:04:08 -04:00
precompFrom = RCL_max(0,precompFrom);
precompTo = RCL_min(SFG_MAX_SPRITE_SIZE - 1,precompTo);
2020-03-22 15:12:06 -04:00
#define PRECOMP_SCALE 512
2019-10-01 19:31:59 -04:00
2020-03-22 15:12:06 -04:00
int16_t precompStepScaled = ((SFG_TEXTURE_SIZE) * PRECOMP_SCALE) / size;
2019-10-01 19:31:59 -04:00
int16_t precompPosScaled = precompFrom * precompStepScaled;
for (int16_t i = precompFrom; i <= precompTo; ++i)
{
2020-02-10 12:10:06 -05:00
SFG_game.spriteSamplingPoints[i] = precompPosScaled / PRECOMP_SCALE;
2019-10-01 19:31:59 -04:00
precompPosScaled += precompStepScaled;
}
#undef PRECOMP_SCALE
2019-10-22 17:59:10 -04:00
uint8_t zDistance = SFG_RCLUnitToZBuffer(distance);
2019-10-07 16:19:45 -04:00
2019-10-01 19:31:59 -04:00
for (int16_t x = x0, u = u0; x <= x1; ++x, ++u)
2019-10-06 19:18:42 -04:00
{
2020-02-10 12:10:06 -05:00
if (SFG_game.zBuffer[x] >= zDistance)
2019-10-06 11:05:26 -04:00
{
2019-10-06 19:18:42 -04:00
int8_t columnTransparent = 1;
2019-10-06 11:05:26 -04:00
2019-10-06 19:18:42 -04:00
for (int16_t y = y0, v = v0; y <= y1; ++y, ++v)
2019-10-06 11:12:12 -04:00
{
2019-10-06 19:18:42 -04:00
uint8_t color =
2020-02-10 12:10:06 -05:00
SFG_getTexel(image,SFG_game.spriteSamplingPoints[u],
SFG_game.spriteSamplingPoints[v]);
2019-10-06 19:18:42 -04:00
if (color != SFG_TRANSPARENT_COLOR)
{
2019-10-07 16:19:45 -04:00
#if SFG_DIMINISH_SPRITES
2019-10-06 19:18:42 -04:00
color = palette_minusValue(color,minusValue);
2019-10-22 17:59:10 -04:00
#endif
2019-10-06 19:18:42 -04:00
columnTransparent = 0;
2019-10-06 11:12:12 -04:00
2019-10-06 19:18:42 -04:00
SFG_setGamePixel(x,y,color);
}
2019-10-06 11:12:12 -04:00
}
2019-10-06 19:18:42 -04:00
if (!columnTransparent)
2020-02-10 12:10:06 -05:00
SFG_game.zBuffer[x] = zDistance;
2019-10-06 11:05:26 -04:00
}
2019-10-06 19:18:42 -04:00
}
2019-10-01 19:31:59 -04:00
}
2019-09-26 15:15:07 -04:00
RCL_Unit SFG_texturesAt(int16_t x, int16_t y)
2019-09-25 09:51:19 -04:00
{
2019-09-26 22:41:45 -04:00
uint8_t p;
2019-09-26 21:34:49 -04:00
2020-02-12 10:52:18 -05:00
SFG_TileDefinition tile =
SFG_getMapTile(SFG_currentLevel.levelPointer,x,y,&p);
2019-09-26 22:41:45 -04:00
return
SFG_TILE_FLOOR_TEXTURE(tile) | (SFG_TILE_CEILING_TEXTURE(tile) << 3) | p;
// ^ store both textures (floor and ceiling) and properties in one number
2019-09-25 09:51:19 -04:00
}
2019-09-26 21:34:49 -04:00
RCL_Unit SFG_movingWallHeight
(
RCL_Unit low,
RCL_Unit high,
uint32_t time
)
{
RCL_Unit height = high - low;
RCL_Unit halfHeight = height / 2;
RCL_Unit sinArg =
(time * ((SFG_MOVING_WALL_SPEED * RCL_UNITS_PER_SQUARE) / 1000)) / height;
return
low + halfHeight + (RCL_sinInt(sinArg) * halfHeight) / RCL_UNITS_PER_SQUARE;
}
2019-09-25 09:51:19 -04:00
RCL_Unit SFG_floorHeightAt(int16_t x, int16_t y)
{
2019-09-26 21:34:49 -04:00
uint8_t properties;
2019-10-04 15:09:10 -04:00
SFG_TileDefinition tile =
SFG_getMapTile(SFG_currentLevel.levelPointer,x,y,&properties);
2019-09-25 09:51:19 -04:00
2020-03-09 11:20:40 -04:00
RCL_Unit doorHeight = 0;
2019-10-04 15:09:10 -04:00
if (properties == SFG_TILE_PROPERTY_DOOR)
{
for (uint8_t i = 0; i < SFG_currentLevel.doorRecordCount; ++i)
{
2019-10-06 10:16:59 -04:00
SFG_DoorRecord *door = &(SFG_currentLevel.doorRecords[i]);
2019-10-04 15:09:10 -04:00
if ((door->coords[0] == x) && (door->coords[1] == y))
{
doorHeight = door->state & SFG_DOOR_VERTICAL_POSITION_MASK;
2020-03-09 11:20:40 -04:00
doorHeight = doorHeight != (0xff & SFG_DOOR_VERTICAL_POSITION_MASK) ?
doorHeight * SFG_DOOR_HEIGHT_STEP : RCL_UNITS_PER_SQUARE;
2019-10-04 15:09:10 -04:00
break;
}
}
}
else if (properties == SFG_TILE_PROPERTY_ELEVATOR)
{
2020-03-02 15:47:28 -05:00
RCL_Unit height =
SFG_TILE_FLOOR_HEIGHT(tile) * SFG_WALL_HEIGHT_STEP;
2019-10-04 15:09:10 -04:00
return SFG_movingWallHeight(
2020-03-02 15:47:28 -05:00
height,
height + SFG_TILE_CEILING_HEIGHT(tile) * SFG_WALL_HEIGHT_STEP,
2020-02-10 12:10:06 -05:00
SFG_game.frameTime - SFG_currentLevel.timeStart);
2019-10-04 15:09:10 -04:00
}
2020-03-09 11:20:40 -04:00
return SFG_TILE_FLOOR_HEIGHT(tile) * SFG_WALL_HEIGHT_STEP - doorHeight;
2019-09-25 09:51:19 -04:00
}
2020-02-23 05:51:56 -05:00
/**
Like SFG_floorCollisionHeightAt, but takes into account colliding items on
the map, so the squares that have these items are higher. The former function
is for rendering, this one is for collision checking.
*/
2020-02-22 17:36:15 -05:00
RCL_Unit SFG_floorCollisionHeightAt(int16_t x, int16_t y)
{
return SFG_floorHeightAt(x,y) +
SFG_getItemCollisionMapBit(x,y) * RCL_UNITS_PER_SQUARE;
}
2020-01-05 18:00:44 -05:00
void SFG_initPlayer()
{
RCL_initCamera(&SFG_player.camera);
SFG_player.camera.resolution.x =
SFG_GAME_RESOLUTION_X / SFG_RAYCASTING_SUBSAMPLE;
SFG_player.camera.resolution.y = SFG_GAME_RESOLUTION_Y - SFG_HUD_BAR_HEIGHT;
SFG_player.camera.position.x = RCL_UNITS_PER_SQUARE / 2 +
SFG_currentLevel.levelPointer->playerStart[0] * RCL_UNITS_PER_SQUARE;
SFG_player.camera.position.y = RCL_UNITS_PER_SQUARE / 2 +
SFG_currentLevel.levelPointer->playerStart[1] * RCL_UNITS_PER_SQUARE;
SFG_player.camera.height =
SFG_floorHeightAt(
SFG_currentLevel.levelPointer->playerStart[0],
SFG_currentLevel.levelPointer->playerStart[1]);
SFG_player.camera.direction =
2020-02-24 10:26:02 -05:00
SFG_currentLevel.levelPointer->playerStart[2] *
(RCL_UNITS_PER_SQUARE / 256);
2020-01-05 18:00:44 -05:00
2020-03-22 14:51:51 -04:00
SFG_recomputePLayerDirection();
2020-01-05 18:00:44 -05:00
SFG_player.previousVerticalSpeed = 0;
SFG_player.headBobFrame = 0;
SFG_player.weapon = 2;
2020-02-10 12:10:06 -05:00
SFG_player.weaponCooldownStartFrame = SFG_game.frame;
SFG_player.lastHurtFrame = SFG_game.frame;
SFG_player.lastItemTakenFrame = SFG_game.frame;
2020-01-05 18:00:44 -05:00
2020-03-22 06:31:02 -04:00
SFG_player.health = SFG_PLAYER_START_HEALTH;
2020-01-13 12:59:36 -05:00
2020-02-08 14:53:10 -05:00
SFG_player.cards = 0;
2020-02-09 09:03:15 -05:00
SFG_player.justTeleported = 0;
2020-01-13 12:59:36 -05:00
for (uint8_t i = 0; i < SFG_AMMO_TOTAL; ++i)
SFG_player.ammo[i] = 0;
2020-01-05 18:00:44 -05:00
}
2019-09-25 09:51:19 -04:00
RCL_Unit SFG_ceilingHeightAt(int16_t x, int16_t y)
{
2019-09-26 21:34:49 -04:00
uint8_t properties;
2019-10-08 07:46:12 -04:00
SFG_TileDefinition tile =
SFG_getMapTile(SFG_currentLevel.levelPointer,x,y,&properties);
2019-09-26 21:34:49 -04:00
if (properties == SFG_TILE_PROPERTY_ELEVATOR)
return SFG_CEILING_MAX_HEIGHT;
2019-09-25 20:40:35 -04:00
uint8_t height = SFG_TILE_CEILING_HEIGHT(tile);
2019-09-27 09:24:36 -04:00
return properties != SFG_TILE_PROPERTY_SQUEEZER ?
(
height != SFG_TILE_CEILING_MAX_HEIGHT ?
((SFG_TILE_FLOOR_HEIGHT(tile) + height) * SFG_WALL_HEIGHT_STEP) :
SFG_CEILING_MAX_HEIGHT
) :
SFG_movingWallHeight(
SFG_TILE_FLOOR_HEIGHT(tile) * SFG_WALL_HEIGHT_STEP,
2019-09-30 15:35:49 -04:00
(SFG_TILE_CEILING_HEIGHT(tile) + SFG_TILE_FLOOR_HEIGHT(tile))
* SFG_WALL_HEIGHT_STEP,
2020-02-10 12:10:06 -05:00
SFG_game.frameTime - SFG_currentLevel.timeStart);
2019-09-25 09:51:19 -04:00
}
2020-02-23 05:45:32 -05:00
/**
Gets sprite (image and sprite size) for given item.
*/
void SFG_getItemSprite(
2020-02-24 02:26:35 -05:00
uint8_t elementType, const uint8_t **sprite, uint8_t *spriteSize)
2020-02-22 17:36:15 -05:00
{
*spriteSize = 0;
2020-02-24 02:26:35 -05:00
*sprite = (const uint8_t *) &(SFG_itemSprites[elementType - 1]);
2020-02-22 17:36:15 -05:00
switch (elementType)
{
case SFG_LEVEL_ELEMENT_TREE:
2020-03-01 08:37:54 -05:00
case SFG_LEVEL_ELEMENT_RUIN:
2020-03-22 15:12:06 -04:00
case SFG_LEVEL_ELEMENT_LAMP:
2020-02-22 17:36:15 -05:00
*spriteSize = 2;
break;
case SFG_LEVEL_ELEMENT_TERMINAL:
*spriteSize = 1;
break;
case SFG_LEVEL_ELEMENT_TELEPORT:
case SFG_LEVEL_ELEMENT_FINISH:
2020-02-25 11:03:40 -05:00
case SFG_LEVEL_ELEMENT_COLUMN:
2020-02-22 17:36:15 -05:00
*spriteSize = 3;
break;
case SFG_LEVEL_ELEMENT_CARD0:
case SFG_LEVEL_ELEMENT_CARD1:
case SFG_LEVEL_ELEMENT_CARD2:
2020-02-24 02:26:35 -05:00
*sprite =
(const uint8_t *) &(SFG_itemSprites[SFG_LEVEL_ELEMENT_CARD0 - 1]);
break;
case SFG_LEVEL_ELEMENT_BLOCKER:
*sprite = 0;
2020-02-22 17:36:15 -05:00
break;
default:
break;
}
}
2020-02-23 05:45:32 -05:00
/**
Says whether given item type collides, i.e. stops player from moving.
*/
uint8_t SFG_itemCollides(uint8_t elementType)
2020-02-22 17:36:15 -05:00
{
return
elementType == SFG_LEVEL_ELEMENT_BARREL ||
elementType == SFG_LEVEL_ELEMENT_TREE ||
2020-02-24 02:26:35 -05:00
elementType == SFG_LEVEL_ELEMENT_TERMINAL ||
2020-02-24 06:53:49 -05:00
elementType == SFG_LEVEL_ELEMENT_COLUMN ||
2020-03-01 08:37:54 -05:00
elementType == SFG_LEVEL_ELEMENT_RUIN ||
2020-03-22 12:52:23 -04:00
elementType == SFG_LEVEL_ELEMENT_BLOCKER ||
elementType == SFG_LEVEL_ELEMENT_LAMP;
2020-02-22 17:36:15 -05:00
}
2019-10-02 08:42:30 -04:00
void SFG_setAndInitLevel(const SFG_Level *level)
2019-09-26 21:34:49 -04:00
{
2019-09-29 07:50:40 -04:00
SFG_LOG("setting and initializing level");
2020-02-10 12:10:06 -05:00
SFG_game.currentRandom = 0;
2019-10-18 10:34:51 -04:00
2020-01-02 18:34:50 -05:00
SFG_currentLevel.backgroundImage = level->backgroundImage;
2019-09-26 21:34:49 -04:00
SFG_currentLevel.levelPointer = level;
2019-09-29 14:26:53 -04:00
SFG_currentLevel.floorColor = level->floorColor;
SFG_currentLevel.ceilingColor = level->ceilingColor;
2019-09-26 21:34:49 -04:00
for (uint8_t i = 0; i < 7; ++i)
SFG_currentLevel.textures[i] =
2019-10-23 14:06:07 -04:00
SFG_wallTextures[level->textureIndices[i]];
2019-09-26 21:34:49 -04:00
2019-10-04 11:18:46 -04:00
SFG_LOG("initializing doors");
2019-10-06 10:47:47 -04:00
SFG_currentLevel.checkedDoorIndex = 0;
2019-10-04 11:18:46 -04:00
SFG_currentLevel.doorRecordCount = 0;
2019-10-21 08:54:32 -04:00
SFG_currentLevel.projectileRecordCount = 0;
2020-02-09 09:03:15 -05:00
SFG_currentLevel.teleportCount = 0;
2020-02-21 15:21:09 -05:00
SFG_currentLevel.mapRevealMask = 0;
2020-02-09 09:03:15 -05:00
2019-10-04 11:18:46 -04:00
for (uint8_t j = 0; j < SFG_MAP_SIZE; ++j)
{
for (uint8_t i = 0; i < SFG_MAP_SIZE; ++i)
{
uint8_t properties;
SFG_getMapTile(level,i,j,&properties);
if ((properties & SFG_TILE_PROPERTY_MASK) == SFG_TILE_PROPERTY_DOOR)
{
SFG_DoorRecord *d =
2019-10-06 10:16:59 -04:00
&(SFG_currentLevel.doorRecords[SFG_currentLevel.doorRecordCount]);
2019-10-04 11:18:46 -04:00
d->coords[0] = i;
d->coords[1] = j;
2019-10-04 11:42:54 -04:00
d->state = SFG_DOOR_DEFAULT_STATE;
2019-10-04 11:18:46 -04:00
SFG_currentLevel.doorRecordCount++;
}
if (SFG_currentLevel.doorRecordCount >= SFG_MAX_DOORS)
2020-02-08 15:41:05 -05:00
{
SFG_LOG("warning: too many doors!");
2019-10-04 11:18:46 -04:00
break;
2020-02-08 15:41:05 -05:00
}
2019-10-04 11:18:46 -04:00
}
if (SFG_currentLevel.doorRecordCount >= SFG_MAX_DOORS)
break;
}
2019-10-06 10:16:59 -04:00
SFG_LOG("initializing level elements");
2019-10-17 15:19:32 -04:00
SFG_currentLevel.itemRecordCount = 0;
2019-10-06 10:47:47 -04:00
SFG_currentLevel.checkedItemIndex = 0;
2019-10-17 15:19:32 -04:00
SFG_currentLevel.monsterRecordCount = 0;
SFG_currentLevel.checkedMonsterIndex = 0;
SFG_MonsterRecord *monster;
2019-10-06 10:16:59 -04:00
2020-02-22 17:36:15 -05:00
for (uint16_t i = 0; i < ((SFG_MAP_SIZE * SFG_MAP_SIZE) / 8); ++i)
SFG_currentLevel.itemCollisionMap[i] = 0;
2019-10-06 10:16:59 -04:00
for (uint8_t i = 0; i < SFG_MAX_LEVEL_ELEMENTS; ++i)
{
2019-10-06 14:17:09 -04:00
const SFG_LevelElement *e = &(SFG_currentLevel.levelPointer->elements[i]);
2019-10-06 10:16:59 -04:00
2019-12-27 11:17:14 -05:00
if (e->type != SFG_LEVEL_ELEMENT_NONE)
2019-10-06 10:16:59 -04:00
{
2019-12-27 11:17:14 -05:00
if (SFG_LEVEL_ELEMENT_TYPE_IS_MOSTER(e->type))
{
2019-10-24 15:43:13 -04:00
SFG_LOG("adding monster");
2019-10-17 15:19:32 -04:00
monster =
&(SFG_currentLevel.monsterRecords[SFG_currentLevel.monsterRecordCount]);
2020-03-28 15:48:16 -04:00
monster->stateType = (SFG_MONSTER_TYPE_TO_INDEX(e->type) << 4)
| SFG_MONSTER_STATE_INACTIVE;
2020-02-12 10:52:18 -05:00
monster->health =
SFG_GET_MONSTER_MAX_HEALTH(SFG_MONSTER_TYPE_TO_INDEX(e->type));
2019-12-30 18:35:35 -05:00
2020-01-01 18:04:47 -05:00
monster->coords[0] = e->coords[0] * 4 + 2;
monster->coords[1] = e->coords[1] * 4 + 2;
2019-10-17 15:19:32 -04:00
SFG_currentLevel.monsterRecordCount++;
2019-12-27 11:17:14 -05:00
}
2020-02-08 15:41:05 -05:00
else if ((e->type < SFG_LEVEL_ELEMENT_LOCK0) ||
(e->type > SFG_LEVEL_ELEMENT_LOCK2))
2019-12-27 11:17:14 -05:00
{
SFG_LOG("adding item");
SFG_currentLevel.itemRecords[SFG_currentLevel.itemRecordCount] = i;
SFG_currentLevel.itemRecordCount++;
2020-02-09 09:03:15 -05:00
if (e->type == SFG_LEVEL_ELEMENT_TELEPORT)
SFG_currentLevel.teleportCount++;
2020-02-22 17:36:15 -05:00
2020-02-23 05:45:32 -05:00
if (SFG_itemCollides(e->type))
2020-02-22 17:36:15 -05:00
SFG_setItemCollisionMapBit(e->coords[0],e->coords[1],1);
2019-12-27 11:17:14 -05:00
}
2020-02-08 15:41:05 -05:00
else
{
SFG_LOG("adding door lock");
uint8_t properties;
SFG_getMapTile(level,e->coords[0],e->coords[1],&properties);
if ((properties & SFG_TILE_PROPERTY_MASK) == SFG_TILE_PROPERTY_DOOR)
{
// find the door record and lock the door:
for (uint16_t j = 0; j < SFG_currentLevel.doorRecordCount; ++j)
{
SFG_DoorRecord *d = &(SFG_currentLevel.doorRecords[j]);
if (d->coords[0] == e->coords[0] && d->coords[1] == e->coords[1])
{
d->state |= (e->type - SFG_LEVEL_ELEMENT_LOCK0 + 1) << 6;
break;
}
}
}
else
{
SFG_LOG("warning: lock not put on door tile!");
}
}
2019-10-06 10:16:59 -04:00
}
}
2019-09-27 10:38:55 -04:00
SFG_currentLevel.timeStart = SFG_getTimeMs();
2020-02-10 12:10:06 -05:00
SFG_currentLevel.frameStart = SFG_game.frame;
2019-10-22 10:48:27 -04:00
2020-02-10 12:10:06 -05:00
SFG_game.spriteAnimationFrame = 0;
2019-09-26 21:34:49 -04:00
2019-10-01 14:25:21 -04:00
SFG_initPlayer();
2019-09-26 21:34:49 -04:00
}
2019-09-25 09:51:19 -04:00
2020-02-13 02:23:44 -05:00
void SFG_setGameState(uint8_t state)
{
SFG_game.state = state;
2020-02-17 02:48:41 -05:00
SFG_game.stateChangeTime = SFG_game.frameTime;
2020-02-13 02:23:44 -05:00
}
2019-09-27 10:38:55 -04:00
void SFG_init()
{
2019-09-29 07:50:40 -04:00
SFG_LOG("initializing game")
2020-02-10 12:10:06 -05:00
SFG_game.frame = 0;
SFG_game.currentRandom = 0;
2019-10-18 10:34:51 -04:00
2020-02-10 12:10:06 -05:00
RCL_initRayConstraints(&SFG_game.rayConstraints);
SFG_game.rayConstraints.maxHits = SFG_RAYCASTING_MAX_HITS;
SFG_game.rayConstraints.maxSteps = SFG_RAYCASTING_MAX_STEPS;
2019-09-27 10:38:55 -04:00
2020-03-28 12:34:09 -04:00
SFG_game.antiSpam = 0;
2020-03-10 14:56:11 -04:00
SFG_LOG("computing average texture colors")
for (uint8_t i = 0; i < SFG_WALL_TEXTURE_COUNT; ++i)
{
/** For simplicity, we round colors so that there is only 64 of them, and
we count them up to 256. */
uint8_t colorHistogram[64];
for (uint8_t j = 0; j < 64; ++j)
colorHistogram[j] = 0;
for (uint8_t y = 0; y < SFG_TEXTURE_SIZE; ++y)
for (uint8_t x = 0; x < SFG_TEXTURE_SIZE; ++x)
{
uint8_t color = SFG_getTexel(SFG_wallTextures[i],x,y) / 4;
colorHistogram[color] += 1;
if (colorHistogram[color] == 255)
break;
}
uint8_t maxIndex = 0;
for (uint8_t j = 0; j < 64; ++j)
{
if (colorHistogram[j] == 255)
{
maxIndex = j;
break;
}
if (colorHistogram[j] > colorHistogram[maxIndex])
maxIndex = j;
}
SFG_game.textureAverageColors[i] = maxIndex * 4;
}
2019-10-04 10:32:24 -04:00
for (uint16_t i = 0; i < SFG_GAME_RESOLUTION_Y; ++i)
2020-02-10 12:10:06 -05:00
SFG_game.backgroundScaleMap[i] =
2019-10-04 10:32:24 -04:00
(i * SFG_TEXTURE_SIZE) / SFG_GAME_RESOLUTION_Y;
2019-09-27 10:38:55 -04:00
2020-01-18 07:06:52 -05:00
for (uint8_t i = 0; i < SFG_KEY_COUNT; ++i)
2020-02-10 12:10:06 -05:00
SFG_game.keyStates[i] = 0;
2019-10-14 09:56:26 -04:00
2020-02-10 12:10:06 -05:00
SFG_game.backgroundScroll = 0;
2020-02-12 06:41:23 -05:00
SFG_currentLevel.levelPointer = 0;
2020-02-10 12:10:06 -05:00
SFG_game.lastFrameTimeMs = SFG_getTimeMs();
2020-02-12 02:18:38 -05:00
SFG_game.selectedMenuItem = 0;
2020-02-12 08:44:51 -05:00
SFG_game.selectedLevel = 0;
2020-01-18 06:27:20 -05:00
SFG_player.freeLook = 0;
2020-02-15 17:04:22 -05:00
#if SFG_START_LEVEL == 0
SFG_setGameState(SFG_GAME_STATE_MENU);
#else
SFG_setAndInitLevel(&SFG_levels[SFG_START_LEVEL - 1]);
SFG_setGameState(SFG_GAME_STATE_PLAYING);
#endif
2019-09-27 10:38:55 -04:00
}
2020-01-13 13:48:40 -05:00
void SFG_getPlayerWeaponInfo(
uint8_t *ammoType, uint8_t *projectileCount, uint8_t *canShoot)
{
*ammoType = SFG_weaponAmmo(SFG_player.weapon);
*projectileCount = SFG_GET_WEAPON_PROJECTILE_COUNT(SFG_player.weapon);
2020-01-18 06:27:20 -05:00
#if SFG_INFINITE_AMMO
*canShoot = 1;
#else
2020-01-13 13:48:40 -05:00
*canShoot =
(*ammoType == SFG_AMMO_NONE ||
SFG_player.ammo[*ammoType] >= *projectileCount);
2020-01-18 06:27:20 -05:00
#endif
2020-01-13 13:48:40 -05:00
}
2019-10-14 07:31:13 -04:00
void SFG_playerRotateWeapon(uint8_t next)
{
2020-01-13 13:48:40 -05:00
RCL_Unit initialWeapon = SFG_player.weapon;
RCL_Unit increment = next ? 1 : -1;
while (1)
{
SFG_player.weapon =
RCL_wrap(SFG_player.weapon + increment,SFG_WEAPONS_TOTAL);
if (SFG_player.weapon == initialWeapon)
break;
2020-01-01 11:06:11 -05:00
2020-01-13 13:48:40 -05:00
uint8_t ammo, projectileCount, canShoot;
2020-01-01 11:06:11 -05:00
2020-01-13 13:48:40 -05:00
SFG_getPlayerWeaponInfo(&ammo,&projectileCount,&canShoot);
if (canShoot)
break;
}
2019-10-14 07:31:13 -04:00
}
2019-10-22 10:58:21 -04:00
/**
Adds new projectile to the current level, return 1 if added, 0 if not (max
count reached).
*/
uint8_t SFG_createProjectile(SFG_ProjectileRecord projectile)
{
if (SFG_currentLevel.projectileRecordCount >= SFG_MAX_PROJECTILES)
return 0;
SFG_currentLevel.projectileRecords[SFG_currentLevel.projectileRecordCount] =
projectile;
SFG_currentLevel.projectileRecordCount++;
return 1;
}
2019-10-22 11:29:21 -04:00
/**
Launches projectile of given type from given position in given direction
(has to be normalized), with given offset (so as to not collide with the
shooting entity). Returns the same value as SFG_createProjectile.
*/
uint8_t SFG_launchProjectile(
uint8_t type,
RCL_Vector2D shootFrom,
RCL_Unit shootFromHeight,
RCL_Vector2D direction,
RCL_Unit verticalSpeed,
RCL_Unit offsetDistance
)
{
2019-12-30 18:35:35 -05:00
if (type == SFG_PROJECTILE_NONE)
2019-12-30 18:40:37 -05:00
return 0;
2019-12-30 18:35:35 -05:00
2019-10-22 11:29:21 -04:00
SFG_ProjectileRecord p;
p.type = type;
2019-12-30 15:35:19 -05:00
p.doubleFramesToLive =
RCL_nonZero(SFG_GET_PROJECTILE_FRAMES_TO_LIVE(type) / 2);
2019-10-22 11:29:21 -04:00
p.position[0] =
shootFrom.x + (direction.x * offsetDistance) / RCL_UNITS_PER_SQUARE;
p.position[1] =
shootFrom.y + (direction.y * offsetDistance) / RCL_UNITS_PER_SQUARE;
p.position[2] = shootFromHeight;
p.direction[0] =
2019-12-30 15:35:19 -05:00
(direction.x * SFG_GET_PROJECTILE_SPEED_UPS(type)) / RCL_UNITS_PER_SQUARE;
2019-10-22 11:29:21 -04:00
p.direction[1] =
2019-12-30 15:35:19 -05:00
(direction.y * SFG_GET_PROJECTILE_SPEED_UPS(type)) / RCL_UNITS_PER_SQUARE;
2019-10-22 11:29:21 -04:00
p.direction[2] = verticalSpeed;
return SFG_createProjectile(p);
}
2019-12-28 18:06:22 -05:00
/**
Pushes a given position away from a center by given distance, with collisions.
Returns 1 if push away happened, otherwise 0.
*/
uint8_t SFG_pushAway(
RCL_Unit pos[3],
RCL_Unit centerX,
RCL_Unit centerY,
RCL_Unit preferredDirection,
RCL_Unit distance)
{
RCL_Vector2D fromCenter;
fromCenter.x = pos[0] - centerX;
fromCenter.y = pos[1] - centerY;
RCL_Unit l = RCL_len(fromCenter);
if (l < 128)
{
fromCenter = RCL_angleToDirection(preferredDirection);
l = RCL_UNITS_PER_SQUARE;
}
else if (l >= distance)
{
return 0;
}
RCL_Vector2D offset;
offset.x = (fromCenter.x * distance) / l;
offset.y = (fromCenter.y * distance) / l;
RCL_Camera c;
RCL_initCamera(&c);
c.position.x = pos[0];
c.position.y = pos[1];
c.height = pos[2];
2020-02-22 17:36:15 -05:00
RCL_moveCameraWithCollision(&c,offset,0,SFG_floorCollisionHeightAt,
2019-12-28 18:06:22 -05:00
SFG_ceilingHeightAt,1,1);
pos[0] = c.position.x;
pos[1] = c.position.y;
pos[2] = c.height;
return 1;
}
2020-02-12 10:52:18 -05:00
uint8_t SFG_pushPlayerAway(
RCL_Unit centerX, RCL_Unit centerY, RCL_Unit distance)
2019-12-28 18:06:22 -05:00
{
RCL_Unit p[3];
p[0] = SFG_player.camera.position.x;
p[1] = SFG_player.camera.position.y;
p[2] = SFG_player.camera.height;
uint8_t result = SFG_pushAway(p,centerX,centerY,
SFG_player.camera.direction - RCL_UNITS_PER_SQUARE / 2,
distance);
SFG_player.camera.position.x = p[0];
SFG_player.camera.position.y = p[1];
SFG_player.camera.height = p[2];
return result;
}
/**
Helper function to resolve collision with level element. The function supposes
the collision already does happen and only resolves it. Returns adjusted move
offset.
*/
RCL_Vector2D SFG_resolveCollisionWithElement(
RCL_Vector2D position, RCL_Vector2D moveOffset, RCL_Vector2D elementPos)
{
RCL_Unit dx = RCL_absVal(elementPos.x - position.x);
RCL_Unit dy = RCL_absVal(elementPos.y - position.y);
if (dx > dy)
{
// colliding from left/right
if ((moveOffset.x > 0) == (position.x < elementPos.x))
moveOffset.x = 0;
// ^ only stop if heading towards element, to avoid getting stuck
}
else
{
// colliding from up/down
if ((moveOffset.y > 0) == (position.y < elementPos.y))
moveOffset.y = 0;
}
return moveOffset;
}
/**
Adds or substracts player's health, which either hurts him (negative value)
or heals him (positive value).
*/
void SFG_playerChangeHealth(int8_t healthAdd)
{
int16_t health = SFG_player.health;
health += healthAdd;
health = RCL_clamp(health,0,SFG_PLAYER_MAX_HEALTH);
SFG_player.health = health;
if (healthAdd < 0)
2020-02-10 12:10:06 -05:00
SFG_player.lastHurtFrame = SFG_game.frame;
2019-12-28 18:06:22 -05:00
}
2020-04-01 06:29:44 -04:00
uint8_t SFG_distantSoundVolume(RCL_Unit x, RCL_Unit y, RCL_Unit z)
{
RCL_Unit distance = SFG_taxicabDistance(x,y,z,
SFG_player.camera.position.x,
SFG_player.camera.position.y,
SFG_player.camera.height);
if (distance >= SFG_SFX_MAX_DISTANCE)
return 0;
uint32_t result = 255 - (distance * 255) / SFG_SFX_MAX_DISTANCE;
return (result * result) / 256;
}
2019-12-29 06:26:06 -05:00
/**
Same as SFG_playerChangeHealth but for monsters.
*/
void SFG_monsterChangeHealth(SFG_MonsterRecord *monster, int8_t healthAdd)
{
int16_t health = monster->health;
2020-04-01 06:29:44 -04:00
2019-12-29 06:26:06 -05:00
health += healthAdd;
health = RCL_clamp(health,0,255);
monster->health = health;
2020-04-01 06:29:44 -04:00
if (healthAdd < 0)
{
// play hurt sound
uint8_t volume = SFG_distantSoundVolume(
SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[0]),
SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[1]),
SFG_floorHeightAt(
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[0]),
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[1])));
SFG_playSoundSafe(5,volume);
if (monster->health == 0)
SFG_playSoundSafe(2,volume);
}
2019-12-29 06:26:06 -05:00
}
2020-01-11 13:22:14 -05:00
void SFG_removeItem(uint8_t index)
{
SFG_LOG("removing item");
for (uint16_t j = index; j < SFG_currentLevel.itemRecordCount - 1; ++j)
SFG_currentLevel.itemRecords[j] =
SFG_currentLevel.itemRecords[j + 1];
SFG_currentLevel.itemRecordCount--;
}
2020-02-22 17:36:15 -05:00
/**
Helper function, returns a pointer to level element representing item with
given index, but only if the item is active (otherwise 0 is returned).
*/
static inline const SFG_LevelElement *SFG_getActiveItemElement(uint8_t index)
{
SFG_ItemRecord item = SFG_currentLevel.itemRecords[index];
if ((item & SFG_ITEM_RECORD_ACTIVE_MASK) == 0)
return 0;
return &(SFG_currentLevel.levelPointer->elements[item &
~SFG_ITEM_RECORD_ACTIVE_MASK]);
}
static inline const SFG_LevelElement *SFG_getLevelElement(uint8_t index)
{
SFG_ItemRecord item = SFG_currentLevel.itemRecords[index];
return &(SFG_currentLevel.levelPointer->elements[item &
~SFG_ITEM_RECORD_ACTIVE_MASK]);
}
2020-01-11 16:36:33 -05:00
void SFG_createExplosion(RCL_Unit, RCL_Unit, RCL_Unit); // forward decl
2020-01-11 13:22:14 -05:00
void SFG_explodeBarrel(uint8_t itemIndex, RCL_Unit x, RCL_Unit y, RCL_Unit z)
{
2020-02-22 17:36:15 -05:00
const SFG_LevelElement *e = SFG_getLevelElement(itemIndex);
SFG_setItemCollisionMapBit(e->coords[0],e->coords[1],0);
2020-01-11 13:22:14 -05:00
SFG_removeItem(itemIndex);
SFG_createExplosion(x,y,z);
}
2019-12-28 18:06:22 -05:00
void SFG_createExplosion(RCL_Unit x, RCL_Unit y, RCL_Unit z)
{
2019-12-30 13:24:03 -05:00
SFG_ProjectileRecord explosion;
2019-12-28 18:06:22 -05:00
2020-02-06 02:42:39 -05:00
SFG_playSound(2,SFG_distantSoundVolume(x,y,z));
2020-02-05 10:00:10 -05:00
2019-12-30 13:24:03 -05:00
explosion.type = SFG_PROJECTILE_EXPLOSION;
2019-12-28 18:06:22 -05:00
2019-12-30 13:24:03 -05:00
explosion.position[0] = x;
explosion.position[1] = y;
explosion.position[2] = z;
2019-12-28 18:06:22 -05:00
2019-12-30 13:24:03 -05:00
explosion.direction[0] = 0;
explosion.direction[1] = 0;
explosion.direction[2] = 0;
2019-12-28 18:06:22 -05:00
2019-12-30 15:35:19 -05:00
explosion.doubleFramesToLive = RCL_nonZero(
SFG_GET_PROJECTILE_FRAMES_TO_LIVE(SFG_PROJECTILE_EXPLOSION) / 2);
2019-12-28 18:06:22 -05:00
2019-12-30 13:24:03 -05:00
SFG_createProjectile(explosion);
2019-12-28 18:06:22 -05:00
2020-01-11 13:22:14 -05:00
uint8_t damage = SFG_getDamageValue(SFG_WEAPON_FIRE_TYPE_FIREBALL);
2020-03-08 10:04:02 -04:00
if (SFG_pushPlayerAway(x,y,SFG_EXPLOSION_PUSH_AWAY_DISTANCE))
2020-01-11 13:22:14 -05:00
SFG_playerChangeHealth(-1 * damage);
2019-12-29 06:26:06 -05:00
2020-01-02 13:40:00 -05:00
for (uint16_t i = 0; i < SFG_currentLevel.monsterRecordCount; ++i)
2019-12-29 06:26:06 -05:00
{
SFG_MonsterRecord *monster = &(SFG_currentLevel.monsterRecords[i]);
2020-01-07 15:26:38 -05:00
if (SFG_MR_STATE(*monster) == SFG_MONSTER_STATE_INACTIVE)
continue;
RCL_Unit monsterHeight =
SFG_floorHeightAt(
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[0]),
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[1]))
+ RCL_UNITS_PER_SQUARE / 2;
if (SFG_taxicabDistance(
SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[0]),
SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[1]),monsterHeight,
2020-02-23 05:45:32 -05:00
x,y,z) <= SFG_EXPLOSION_RADIUS)
2019-12-29 06:26:06 -05:00
{
2020-01-02 11:22:00 -05:00
SFG_monsterChangeHealth(monster,
-1 * SFG_getDamageValue(SFG_WEAPON_FIRE_TYPE_FIREBALL));
2019-12-29 06:26:06 -05:00
}
}
2020-01-07 15:26:38 -05:00
2020-01-11 13:22:14 -05:00
// explode nearby barrels
2020-01-07 15:26:38 -05:00
2020-01-11 13:22:14 -05:00
if (damage >= SFG_BARREL_EXPLOSION_DAMAGE_THRESHOLD)
for (uint16_t i = 0; i < SFG_currentLevel.itemRecordCount; ++i)
{
SFG_ItemRecord item = SFG_currentLevel.itemRecords[i];
2020-01-07 15:26:38 -05:00
2020-01-11 13:22:14 -05:00
/* We DON'T check just active barrels but all, otherwise it looks weird
that out of sight barrels in a line didn't explode.*/
2020-01-07 15:26:38 -05:00
2020-01-11 13:22:14 -05:00
SFG_LevelElement element = SFG_ITEM_RECORD_LEVEL_ELEMENT(item);
2020-01-07 15:26:38 -05:00
2020-01-11 13:22:14 -05:00
if (element.type != SFG_LEVEL_ELEMENT_BARREL)
continue;
2020-01-07 15:26:38 -05:00
2020-01-11 13:22:14 -05:00
RCL_Unit elementX =
element.coords[0] * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2;
2020-01-07 15:26:38 -05:00
2020-01-11 13:22:14 -05:00
RCL_Unit elementY =
element.coords[1] * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2;
RCL_Unit elementHeight =
SFG_floorHeightAt(element.coords[0],element.coords[1]);
if (SFG_taxicabDistance(
2020-02-23 05:45:32 -05:00
x,y,z,elementX,elementY,elementHeight) <= SFG_EXPLOSION_RADIUS)
2020-01-11 13:22:14 -05:00
{
SFG_explodeBarrel(i,elementX,elementY,elementHeight);
i--;
}
2020-01-07 15:26:38 -05:00
}
2019-12-28 18:06:22 -05:00
}
2019-12-30 13:24:03 -05:00
void SFG_createDust(RCL_Unit x, RCL_Unit y, RCL_Unit z)
{
SFG_ProjectileRecord dust;
dust.type = SFG_PROJECTILE_DUST;
dust.position[0] = x;
dust.position[1] = y;
dust.position[2] = z;
dust.direction[0] = 0;
dust.direction[1] = 0;
dust.direction[2] = 0;
2019-12-30 15:35:19 -05:00
dust.doubleFramesToLive =
RCL_nonZero(SFG_GET_PROJECTILE_FRAMES_TO_LIVE(SFG_PROJECTILE_DUST) / 2);
2019-12-30 13:24:03 -05:00
SFG_createProjectile(dust);
}
2020-01-02 13:40:00 -05:00
void SFG_getMonsterWorldPosition(SFG_MonsterRecord *monster, RCL_Unit *x,
RCL_Unit *y, RCL_Unit *z)
{
*x = SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[0]);
*y = SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[1]);
*z = SFG_floorHeightAt(
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[0]),
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[1]))
+ RCL_UNITS_PER_SQUARE / 2;
}
2019-10-18 07:48:24 -04:00
void SFG_monsterPerformAI(SFG_MonsterRecord *monster)
2019-10-17 17:58:19 -04:00
{
2019-12-26 09:28:23 -05:00
uint8_t state = SFG_MR_STATE(*monster);
uint8_t type = SFG_MR_TYPE(*monster);
2019-12-30 18:35:35 -05:00
uint8_t monsterNumber = SFG_MONSTER_TYPE_TO_INDEX(type);
uint8_t attackType = SFG_GET_MONSTER_ATTACK_TYPE(monsterNumber);
2019-10-18 09:25:07 -04:00
int8_t coordAdd[2];
coordAdd[0] = 0;
coordAdd[1] = 0;
2019-12-30 18:35:35 -05:00
uint8_t notRanged =
(attackType == SFG_MONSTER_ATTACK_MELEE) ||
(attackType == SFG_MONSTER_ATTACK_EXPLODE);
2019-10-24 17:51:24 -04:00
2019-12-30 18:35:35 -05:00
if ( // sometimes randomly attack
!notRanged &&
(SFG_random() <
SFG_GET_MONSTER_AGGRESSIVITY(SFG_MONSTER_TYPE_TO_INDEX(type)))
)
2019-10-18 11:26:13 -04:00
{
2019-12-30 18:35:35 -05:00
if (!notRanged && (SFG_random() % 4 != 0))
2019-10-22 10:58:21 -04:00
{
// attack
state = SFG_MONSTER_STATE_ATTACKING;
2019-10-24 17:51:24 -04:00
if (type != SFG_LEVEL_ELEMENT_MONSTER_WARRIOR)
{
RCL_Vector2D pos;
RCL_Vector2D dir;
pos.x = SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[0]);
pos.y = SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[1]);
dir.x = SFG_player.camera.position.x - pos.x;
dir.y = SFG_player.camera.position.y - pos.y;
dir = RCL_normalize(dir);
2019-12-30 18:35:35 -05:00
uint8_t projectile;
switch (SFG_GET_MONSTER_ATTACK_TYPE(monsterNumber))
{
case SFG_MONSTER_ATTACK_FIREBALL:
projectile = SFG_PROJECTILE_FIREBALL;
break;
case SFG_MONSTER_ATTACK_BULLET:
projectile = SFG_PROJECTILE_BULLET;
break;
case SFG_MONSTER_ATTACK_PLASMA:
projectile = SFG_PROJECTILE_PLASMA;
break;
case SFG_MONSTER_ATTACK_FIREBALL_BULLET:
projectile = (SFG_random() < 128) ?
SFG_PROJECTILE_FIREBALL :
SFG_PROJECTILE_BULLET;
break;
default:
projectile = SFG_PROJECTILE_NONE;
break;
}
2020-02-09 13:03:52 -05:00
if (projectile == SFG_PROJECTILE_BULLET)
SFG_playSoundSafe(0,
SFG_distantSoundVolume(
SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[0]),
SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[1]),
SFG_floorHeightAt(
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[0]),
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[1])
)
)
);
2019-10-24 17:51:24 -04:00
SFG_launchProjectile(
2019-12-30 18:35:35 -05:00
projectile,
2019-10-24 17:51:24 -04:00
pos,
SFG_floorHeightAt(
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[0]),
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[1])
) + RCL_UNITS_PER_SQUARE / 2,
dir,
0,
2020-02-10 06:55:15 -05:00
SFG_PROJECTILE_SPAWN_OFFSET
2019-10-24 17:51:24 -04:00
);
}
2019-10-22 10:58:21 -04:00
}
else
state = SFG_MONSTER_STATE_IDLE;
2019-10-18 11:26:13 -04:00
}
2019-10-22 10:48:27 -04:00
else if (state == SFG_MONSTER_STATE_IDLE)
2019-10-18 09:25:07 -04:00
{
2019-12-30 18:35:35 -05:00
if (notRanged)
2019-10-18 10:48:19 -04:00
{
2019-12-30 18:35:35 -05:00
// non-ranged monsters walk towards player
2019-10-24 17:51:24 -04:00
uint8_t mX = SFG_MONSTER_COORD_TO_SQUARES(monster->coords[0]);
uint8_t mY = SFG_MONSTER_COORD_TO_SQUARES(monster->coords[1]);
2019-12-28 14:16:44 -05:00
2020-01-02 13:40:00 -05:00
RCL_Unit pX, pY, pZ;
SFG_getMonsterWorldPosition(monster,&pX,&pY,&pZ);
2019-12-30 18:35:35 -05:00
uint8_t isClose = // close to player?
2020-01-07 15:26:38 -05:00
SFG_taxicabDistance(pX,pY,pZ,
SFG_player.camera.position.x,
SFG_player.camera.position.y,
SFG_player.camera.height) <= SFG_MELEE_RANGE;
2019-12-30 18:35:35 -05:00
if (!isClose)
2019-10-24 17:51:24 -04:00
{
2019-12-30 18:35:35 -05:00
// walk towards player
2019-12-28 14:16:44 -05:00
if (mX > SFG_player.squarePosition[0])
{
if (mY > SFG_player.squarePosition[1])
state = SFG_MONSTER_STATE_GOING_NW;
else if (mY < SFG_player.squarePosition[1])
state = SFG_MONSTER_STATE_GOING_SW;
else
state = SFG_MONSTER_STATE_GOING_W;
}
else if (mX < SFG_player.squarePosition[0])
{
if (mY > SFG_player.squarePosition[1])
state = SFG_MONSTER_STATE_GOING_NE;
else if (mY < SFG_player.squarePosition[1])
state = SFG_MONSTER_STATE_GOING_SE;
else
state = SFG_MONSTER_STATE_GOING_E;
}
2019-10-24 17:51:24 -04:00
else
2019-12-28 14:16:44 -05:00
{
if (mY > SFG_player.squarePosition[1])
state = SFG_MONSTER_STATE_GOING_N;
else if (mY < SFG_player.squarePosition[1])
state = SFG_MONSTER_STATE_GOING_S;
}
2019-10-24 17:51:24 -04:00
}
2020-01-02 13:40:00 -05:00
else // is close
2019-10-24 17:51:24 -04:00
{
2020-01-02 13:40:00 -05:00
// melee, close-up attack
2019-12-30 18:35:35 -05:00
if (attackType == SFG_MONSTER_ATTACK_MELEE)
{
// melee attack
state = SFG_MONSTER_STATE_ATTACKING;
2020-01-02 13:40:00 -05:00
SFG_playerChangeHealth(
-1 * SFG_getDamageValue(SFG_WEAPON_FIRE_TYPE_MELEE));
2020-02-09 13:03:52 -05:00
SFG_playSoundSafe(3,255);
2019-12-30 18:35:35 -05:00
}
else // SFG_MONSTER_ATTACK_EXPLODE
{
// explode
2019-12-28 14:16:44 -05:00
2019-12-30 18:35:35 -05:00
uint8_t properties;
2019-12-28 14:16:44 -05:00
2019-12-30 18:35:35 -05:00
SFG_TileDefinition tile =
SFG_getMapTile(SFG_currentLevel.levelPointer,mX,mY,&properties);
2020-02-12 10:52:18 -05:00
SFG_createExplosion(mX * RCL_UNITS_PER_SQUARE,
mY * RCL_UNITS_PER_SQUARE,
SFG_TILE_FLOOR_HEIGHT(tile) * SFG_WALL_HEIGHT_STEP +
SFG_WALL_HEIGHT_STEP);
2019-12-29 05:39:49 -05:00
2019-12-30 18:35:35 -05:00
monster->health = 0;
}
2019-10-24 17:51:24 -04:00
}
}
2019-12-30 18:35:35 -05:00
else // ranged monsters
2019-10-24 17:51:24 -04:00
{
2019-12-30 18:35:35 -05:00
// choose walk direction randomly
2019-10-24 17:51:24 -04:00
switch (SFG_random() % 8)
{
case 0: state = SFG_MONSTER_STATE_GOING_E; break;
case 1: state = SFG_MONSTER_STATE_GOING_W; break;
case 2: state = SFG_MONSTER_STATE_GOING_N; break;
case 3: state = SFG_MONSTER_STATE_GOING_S; break;
case 4: state = SFG_MONSTER_STATE_GOING_NE; break;
case 5: state = SFG_MONSTER_STATE_GOING_NW; break;
case 6: state = SFG_MONSTER_STATE_GOING_SE; break;
case 7: state = SFG_MONSTER_STATE_GOING_SW; break;
default: break;
}
2019-10-18 10:48:19 -04:00
}
2019-10-18 09:25:07 -04:00
}
2019-10-22 10:48:27 -04:00
else if (state == SFG_MONSTER_STATE_ATTACKING)
{
state = SFG_MONSTER_STATE_IDLE;
}
2019-10-18 09:25:07 -04:00
else
{
2019-12-30 18:35:35 -05:00
int8_t add = 1;
if (attackType == SFG_MONSTER_ATTACK_MELEE)
add = 2;
else if (attackType == SFG_MONSTER_ATTACK_EXPLODE)
add = 3;
2019-12-27 18:20:00 -05:00
2019-10-18 10:56:38 -04:00
if (state == SFG_MONSTER_STATE_GOING_E ||
state == SFG_MONSTER_STATE_GOING_NE ||
state == SFG_MONSTER_STATE_GOING_SE)
2019-12-27 18:20:00 -05:00
coordAdd[0] = add;
2019-10-18 10:56:38 -04:00
else if (state == SFG_MONSTER_STATE_GOING_W ||
state == SFG_MONSTER_STATE_GOING_SW ||
state == SFG_MONSTER_STATE_GOING_NW)
2019-12-27 18:20:00 -05:00
coordAdd[0] = -1 * add;
2019-10-18 10:56:38 -04:00
if (state == SFG_MONSTER_STATE_GOING_N ||
state == SFG_MONSTER_STATE_GOING_NE ||
state == SFG_MONSTER_STATE_GOING_NW)
2019-12-27 18:20:00 -05:00
coordAdd[1] = -1 * add;
2019-10-18 10:56:38 -04:00
else if (state == SFG_MONSTER_STATE_GOING_S ||
state == SFG_MONSTER_STATE_GOING_SE ||
state == SFG_MONSTER_STATE_GOING_SW)
2019-12-27 18:20:00 -05:00
coordAdd[1] = add;
2020-03-30 12:52:54 -04:00
if ((coordAdd[0] != 0 || coordAdd[1] != 0) && SFG_random() <
SFG_MONSTER_SOUND_PROBABILITY)
SFG_playSoundSafe(5,
SFG_distantSoundVolume(
SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[0]),
SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[1]),
SFG_floorHeightAt(
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[0]),
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[1]))) / 2);
2019-12-27 18:20:00 -05:00
if (add)
state = SFG_MONSTER_STATE_IDLE;
2019-10-18 09:25:07 -04:00
}
2019-10-18 10:48:19 -04:00
int16_t newPos[2];
2019-10-18 10:34:51 -04:00
2019-10-18 10:48:19 -04:00
newPos[0] = monster->coords[0] + coordAdd[0];
newPos[1] = monster->coords[1] + coordAdd[1];
2019-10-18 09:25:07 -04:00
2019-10-18 10:48:19 -04:00
int8_t collision = 0;
if (newPos[0] < 0 || newPos[0] >= 256 || newPos[1] < 0 || newPos[1] >= 256)
{
collision = 1;
}
else
{
RCL_Unit currentHeight =
2020-02-22 17:36:15 -05:00
SFG_floorCollisionHeightAt(monster->coords[0] / 4,monster->coords[1] / 4);
2019-10-18 09:25:07 -04:00
2019-10-18 10:48:19 -04:00
RCL_Unit newHeight =
2020-02-22 17:36:15 -05:00
SFG_floorCollisionHeightAt(newPos[0] / 4,newPos[1] / 4);
2019-10-18 09:25:07 -04:00
2019-10-18 10:48:19 -04:00
collision =
RCL_absVal(currentHeight - newHeight) > RCL_CAMERA_COLL_STEP_HEIGHT;
}
2019-10-18 10:34:51 -04:00
2019-10-18 10:48:19 -04:00
if (collision)
{
state = SFG_MONSTER_STATE_IDLE;
// ^ will force the monster to choose random direction in next update
newPos[0] = monster->coords[0];
newPos[1] = monster->coords[1];
}
2019-10-18 10:34:51 -04:00
2020-03-28 15:48:16 -04:00
monster->stateType = state | (monsterNumber << 4);
2019-10-18 10:48:19 -04:00
monster->coords[0] = newPos[0];
monster->coords[1] = newPos[1];;
2019-10-17 17:58:19 -04:00
}
2019-10-22 08:27:56 -04:00
static inline uint8_t SFG_elementCollides(
RCL_Unit pointX,
RCL_Unit pointY,
RCL_Unit pointZ,
RCL_Unit elementX,
RCL_Unit elementY,
2020-02-06 14:47:08 -05:00
RCL_Unit elementHeight
2019-10-22 08:27:56 -04:00
)
{
return
2020-01-07 15:26:38 -05:00
SFG_taxicabDistance(pointX,pointY,pointZ,elementX,elementY,elementHeight)
2020-02-23 05:45:32 -05:00
<= SFG_ELEMENT_COLLISION_RADIUS;
2019-10-22 08:27:56 -04:00
}
2020-02-10 06:55:15 -05:00
/**
Checks collision of a projectile with level element at given position.
*/
uint8_t SFG_projectileCollides(SFG_ProjectileRecord *projectile,
RCL_Unit x, RCL_Unit y, RCL_Unit z)
{
if (!SFG_elementCollides(x,y,z,
projectile->position[0],projectile->position[1],projectile->position[2]))
return 0;
if ((projectile->type == SFG_PROJECTILE_EXPLOSION) ||
(projectile->type == SFG_PROJECTILE_DUST))
return 0;
/* For directional projectiles we only register a collision if its direction
is "towards" the element so that the shooter doesn't get shot by his own
projectile. */
RCL_Vector2D projDir, toElement;
projDir.x = projectile->direction[0];
projDir.y = projectile->direction[1];
toElement.x = x - projectile->position[0];
toElement.y = y - projectile->position[1];
return RCL_vectorsAngleCos(projDir,toElement) >= 0;
}
2019-09-25 09:51:19 -04:00
/**
2020-02-15 17:04:22 -05:00
Updates a frame of the currently loaded level, i.e. enemies, projectiles,
aimations etc., with the exception of player.
2019-09-25 09:51:19 -04:00
*/
2020-02-15 17:04:22 -05:00
void SFG_updateLevel()
2019-09-25 09:51:19 -04:00
{
2020-02-15 17:04:22 -05:00
// update projectiles:
2019-10-02 08:42:30 -04:00
2020-02-15 17:04:22 -05:00
uint8_t substractFrames =
(SFG_game.frame - SFG_currentLevel.frameStart) & 0x01 ? 1 : 0;
// ^ only substract frames to live every other frame
2019-10-11 09:01:36 -04:00
2020-02-15 17:04:22 -05:00
for (int8_t i = 0; i < SFG_currentLevel.projectileRecordCount; ++i)
{ // ^ has to be signed
SFG_ProjectileRecord *p = &(SFG_currentLevel.projectileRecords[i]);
2019-10-11 09:01:36 -04:00
2020-02-15 17:04:22 -05:00
uint8_t attackType = 255;
2020-01-18 06:27:20 -05:00
2020-02-15 17:04:22 -05:00
if (p->type == SFG_PROJECTILE_BULLET)
attackType = SFG_WEAPON_FIRE_TYPE_BULLET;
else if (p->type == SFG_PROJECTILE_PLASMA)
attackType = SFG_WEAPON_FIRE_TYPE_PLASMA;
2019-12-30 13:24:03 -05:00
2020-02-22 03:23:11 -05:00
RCL_Unit pos[3] = {0,0,0}; /* we have to convert from uint16_t because
under/overflows */
2020-02-15 17:04:22 -05:00
uint8_t eliminate = 0;
2019-12-30 13:24:03 -05:00
2020-02-15 17:04:22 -05:00
for (uint8_t j = 0; j < 3; ++j)
2020-01-13 13:48:40 -05:00
{
2020-02-15 17:04:22 -05:00
pos[j] = p->position[j];
pos[j] += p->direction[j];
2020-02-06 13:07:25 -05:00
2020-02-15 17:04:22 -05:00
if ( // projectile outside map?
(pos[j] < 0) ||
(pos[j] >= (SFG_MAP_SIZE * RCL_UNITS_PER_SQUARE)))
2020-02-06 13:07:25 -05:00
{
2020-02-15 17:04:22 -05:00
eliminate = 1;
break;
2020-02-06 13:07:25 -05:00
}
2020-02-15 17:04:22 -05:00
}
2020-02-06 13:07:25 -05:00
2020-02-15 17:04:22 -05:00
if (p->doubleFramesToLive == 0) // no more time to live?
{
eliminate = 1;
}
else if (
(p->type != SFG_PROJECTILE_EXPLOSION) &&
(p->type != SFG_PROJECTILE_DUST))
{
if (SFG_projectileCollides( // collides with player?
p,
SFG_player.camera.position.x,
SFG_player.camera.position.y,
SFG_player.camera.height))
{
eliminate = 1;
SFG_playerChangeHealth(-1 * SFG_getDamageValue(attackType));
}
2020-01-01 13:43:19 -05:00
2020-02-23 05:51:56 -05:00
/* check collision with the map (we don't use SFG_floorCollisionHeightAt
because collisio with items has to be done differently for
projectiles) */
2020-01-01 13:43:19 -05:00
2020-02-15 17:04:22 -05:00
if (!eliminate &&
((SFG_floorHeightAt(pos[0] / RCL_UNITS_PER_SQUARE,pos[1] /
RCL_UNITS_PER_SQUARE) >= pos[2])
||
(SFG_ceilingHeightAt(pos[0] / RCL_UNITS_PER_SQUARE,pos[1] /
RCL_UNITS_PER_SQUARE) <= pos[2]))
)
eliminate = 1;
2020-01-13 13:48:40 -05:00
2020-02-15 17:04:22 -05:00
// check collision with active level elements
2020-01-02 13:40:00 -05:00
2020-02-15 17:04:22 -05:00
if (!eliminate) // monsters
for (uint16_t j = 0; j < SFG_currentLevel.monsterRecordCount; ++j)
2020-01-13 13:48:40 -05:00
{
2020-02-15 17:04:22 -05:00
SFG_MonsterRecord *m = &(SFG_currentLevel.monsterRecords[j]);
2020-01-13 13:48:40 -05:00
2020-02-15 17:04:22 -05:00
if (SFG_MR_STATE(*m) != SFG_MONSTER_STATE_INACTIVE)
{
if (SFG_projectileCollides(p,
SFG_MONSTER_COORD_TO_RCL_UNITS(m->coords[0]),
SFG_MONSTER_COORD_TO_RCL_UNITS(m->coords[1]),
SFG_floorHeightAt(
SFG_MONSTER_COORD_TO_SQUARES(m->coords[0]),
SFG_MONSTER_COORD_TO_SQUARES(m->coords[1]))
))
{
eliminate = 1;
SFG_monsterChangeHealth(m,-1 * SFG_getDamageValue(attackType));
break;
}
}
2020-01-13 13:48:40 -05:00
}
2020-01-02 13:40:00 -05:00
2020-02-22 17:36:15 -05:00
if (!eliminate) // items (can't check itemCollisionMap because of barrels)
2020-02-15 17:04:22 -05:00
for (uint16_t j = 0; j < SFG_currentLevel.itemRecordCount; ++j)
2020-01-13 13:48:40 -05:00
{
2020-02-15 17:04:22 -05:00
const SFG_LevelElement *e = SFG_getActiveItemElement(j);
2020-01-02 13:40:00 -05:00
2020-02-23 05:51:56 -05:00
if (e != 0 && SFG_itemCollides(e->type))
2020-02-15 17:04:22 -05:00
{
RCL_Unit x = SFG_ELEMENT_COORD_TO_RCL_UNITS(e->coords[0]);
RCL_Unit y = SFG_ELEMENT_COORD_TO_RCL_UNITS(e->coords[1]);
RCL_Unit z = SFG_floorHeightAt(e->coords[0],e->coords[1]);
2020-01-02 13:40:00 -05:00
2020-02-15 17:04:22 -05:00
if (SFG_projectileCollides(p,x,y,z))
2020-01-13 13:48:40 -05:00
{
2020-02-15 17:04:22 -05:00
if (
(e->type == SFG_LEVEL_ELEMENT_BARREL) &&
(SFG_getDamageValue(attackType) >=
SFG_BARREL_EXPLOSION_DAMAGE_THRESHOLD)
)
{
SFG_explodeBarrel(j,x,y,z);
}
2020-02-09 13:03:52 -05:00
2020-02-15 17:04:22 -05:00
eliminate = 1;
2020-01-13 13:48:40 -05:00
break;
}
2020-02-15 17:04:22 -05:00
}
2020-01-13 13:48:40 -05:00
}
2020-02-15 17:04:22 -05:00
}
2019-10-22 15:02:39 -04:00
2020-02-15 17:04:22 -05:00
if (eliminate)
{
if (p->type == SFG_PROJECTILE_FIREBALL)
SFG_createExplosion(p->position[0],p->position[1],p->position[2]);
else if (p->type == SFG_PROJECTILE_BULLET)
SFG_createDust(p->position[0],p->position[1],p->position[2]);
else if (p->type == SFG_PROJECTILE_PLASMA)
SFG_playSoundSafe(4,SFG_distantSoundVolume(pos[0],pos[1],pos[2]));
2020-01-13 13:48:40 -05:00
2020-02-15 17:04:22 -05:00
// remove the projectile
2019-10-21 08:54:32 -04:00
2020-02-15 17:04:22 -05:00
for (uint8_t j = i; j < SFG_currentLevel.projectileRecordCount - 1; ++j)
SFG_currentLevel.projectileRecords[j] =
SFG_currentLevel.projectileRecords[j + 1];
2020-01-18 06:27:20 -05:00
2020-02-15 17:04:22 -05:00
SFG_currentLevel.projectileRecordCount--;
2019-10-11 09:01:36 -04:00
2020-02-15 17:04:22 -05:00
i--;
2019-10-11 09:01:36 -04:00
}
2020-02-15 17:04:22 -05:00
else
2019-10-11 09:01:36 -04:00
{
2020-02-15 17:04:22 -05:00
p->position[0] = pos[0];
p->position[1] = pos[1];
p->position[2] = pos[2];
2019-10-11 09:01:36 -04:00
}
2020-02-15 17:04:22 -05:00
p->doubleFramesToLive -= substractFrames;
2019-09-25 09:51:19 -04:00
}
2020-02-15 17:04:22 -05:00
// handle door:
if (SFG_currentLevel.doorRecordCount > 0) // has to be here
2019-09-25 09:51:19 -04:00
{
2020-02-15 17:04:22 -05:00
/* Check one door on whether a player is standing nearby. For performance
reasons we only check a few doors and move to others in the next
frame. */
for (uint16_t i = 0;
i < RCL_min(SFG_ELEMENT_DISTANCES_CHECKED_PER_FRAME,
SFG_currentLevel.doorRecordCount);
++i)
2019-10-01 14:25:21 -04:00
{
2020-02-15 17:04:22 -05:00
SFG_DoorRecord *door =
&(SFG_currentLevel.doorRecords[SFG_currentLevel.checkedDoorIndex]);
2019-10-01 14:25:21 -04:00
2020-02-15 17:04:22 -05:00
uint8_t upDownState = door->state & SFG_DOOR_UP_DOWN_MASK;
2020-01-18 05:10:05 -05:00
2020-02-15 17:04:22 -05:00
uint8_t lock = SFG_DOOR_LOCK(door->state);
2020-01-18 05:10:05 -05:00
2020-02-15 17:04:22 -05:00
uint8_t newUpDownState =
(
((lock == 0) || (SFG_player.cards & (1 << (lock - 1)))) &&
(door->coords[0] >= (SFG_player.squarePosition[0] - 1)) &&
(door->coords[0] <= (SFG_player.squarePosition[0] + 1)) &&
(door->coords[1] >= (SFG_player.squarePosition[1] - 1)) &&
(door->coords[1] <= (SFG_player.squarePosition[1] + 1))
) ? SFG_DOOR_UP_DOWN_MASK : 0x00;
2020-01-18 05:10:05 -05:00
2020-02-15 17:04:22 -05:00
if (upDownState != newUpDownState)
SFG_playSoundSafe(1,255);
2020-01-18 05:10:05 -05:00
2020-02-15 17:04:22 -05:00
door->state = (door->state & ~SFG_DOOR_UP_DOWN_MASK) | newUpDownState;
2019-10-11 09:01:36 -04:00
2020-02-15 17:04:22 -05:00
SFG_currentLevel.checkedDoorIndex++;
2019-09-25 09:51:19 -04:00
2020-02-15 17:04:22 -05:00
if (SFG_currentLevel.checkedDoorIndex >= SFG_currentLevel.doorRecordCount)
SFG_currentLevel.checkedDoorIndex = 0;
}
2019-10-02 08:42:30 -04:00
2020-02-15 17:04:22 -05:00
// move door up/down:
for (uint32_t i = 0; i < SFG_currentLevel.doorRecordCount; ++i)
{
SFG_DoorRecord *door = &(SFG_currentLevel.doorRecords[i]);
2019-10-02 08:42:30 -04:00
2020-02-15 17:04:22 -05:00
int8_t height = door->state & SFG_DOOR_VERTICAL_POSITION_MASK;
2019-09-25 09:51:19 -04:00
2020-02-15 17:04:22 -05:00
height = (door->state & SFG_DOOR_UP_DOWN_MASK) ?
RCL_min(0x1f,height + SFG_DOOR_INCREMENT_PER_FRAME) :
RCL_max(0x00,height - SFG_DOOR_INCREMENT_PER_FRAME);
2019-10-11 09:01:36 -04:00
2020-02-15 17:04:22 -05:00
door->state = (door->state & ~SFG_DOOR_VERTICAL_POSITION_MASK) | height;
}
2019-09-25 09:51:19 -04:00
}
2020-02-15 17:04:22 -05:00
// handle items, in a similar manner to door:
if (SFG_currentLevel.itemRecordCount > 0) // has to be here
2019-10-09 11:44:31 -04:00
{
2020-02-15 17:04:22 -05:00
// check item distances:
2019-10-09 11:44:31 -04:00
2020-02-15 17:04:22 -05:00
for (uint16_t i = 0;
i < RCL_min(SFG_ELEMENT_DISTANCES_CHECKED_PER_FRAME,
SFG_currentLevel.itemRecordCount);
++i)
{
SFG_ItemRecord item =
SFG_currentLevel.itemRecords[SFG_currentLevel.checkedItemIndex];
2019-10-01 14:25:21 -04:00
2020-02-15 17:04:22 -05:00
item &= ~SFG_ITEM_RECORD_ACTIVE_MASK;
2019-10-21 19:34:40 -04:00
2020-02-15 17:04:22 -05:00
SFG_LevelElement e =
SFG_currentLevel.levelPointer->elements[item];
2019-10-21 19:34:40 -04:00
2020-02-15 17:04:22 -05:00
if (
SFG_isInActiveDistanceFromPlayer(
e.coords[0] * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2,
e.coords[1] * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2,
SFG_floorHeightAt(e.coords[0],e.coords[1]) + RCL_UNITS_PER_SQUARE / 2)
)
item |= SFG_ITEM_RECORD_ACTIVE_MASK;
2019-10-28 14:09:11 -04:00
2020-02-15 17:04:22 -05:00
SFG_currentLevel.itemRecords[SFG_currentLevel.checkedItemIndex] = item;
2019-10-22 06:13:10 -04:00
2020-02-15 17:04:22 -05:00
SFG_currentLevel.checkedItemIndex++;
2019-10-22 06:13:10 -04:00
2020-02-15 17:04:22 -05:00
if (SFG_currentLevel.checkedItemIndex >= SFG_currentLevel.itemRecordCount)
SFG_currentLevel.checkedItemIndex = 0;
2019-10-21 19:34:40 -04:00
}
}
2020-02-15 17:04:22 -05:00
// similarly handle monsters:
if (SFG_currentLevel.monsterRecordCount > 0) // has to be here
2019-10-21 20:03:44 -04:00
{
2020-02-15 17:04:22 -05:00
// check monster distances:
2019-10-21 20:03:44 -04:00
2020-02-15 17:04:22 -05:00
for (uint16_t i = 0;
i < RCL_min(SFG_ELEMENT_DISTANCES_CHECKED_PER_FRAME,
SFG_currentLevel.monsterRecordCount);
++i)
2019-10-21 20:03:44 -04:00
{
2020-02-15 17:04:22 -05:00
SFG_MonsterRecord *monster =
&(SFG_currentLevel.monsterRecords[SFG_currentLevel.checkedMonsterIndex]);
2019-10-22 06:13:10 -04:00
2020-02-15 17:04:22 -05:00
if ( // far away from the player?
!SFG_isInActiveDistanceFromPlayer(
SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[0]),
SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[1]),
SFG_floorHeightAt(
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[0]),
SFG_MONSTER_COORD_TO_SQUARES(monster->coords[1]))
+ RCL_UNITS_PER_SQUARE / 2
)
)
2019-10-21 20:03:44 -04:00
{
2020-02-15 17:04:22 -05:00
monster->stateType =
(monster->stateType & SFG_MONSTER_MASK_TYPE) |
SFG_MONSTER_STATE_INACTIVE;
}
else if (SFG_MR_STATE(*monster) == SFG_MONSTER_STATE_INACTIVE)
{
monster->stateType =
(monster->stateType & SFG_MONSTER_MASK_TYPE) |
SFG_MONSTER_STATE_IDLE;
}
2020-01-12 10:48:42 -05:00
2020-02-15 17:04:22 -05:00
SFG_currentLevel.checkedMonsterIndex++;
2019-10-25 18:50:22 -04:00
2020-02-15 17:04:22 -05:00
if (SFG_currentLevel.checkedMonsterIndex >=
SFG_currentLevel.monsterRecordCount)
SFG_currentLevel.checkedMonsterIndex = 0;
}
}
2019-10-25 18:50:22 -04:00
2020-02-15 17:04:22 -05:00
// update AI and handle dead monsters:
if ((SFG_game.frame - SFG_currentLevel.frameStart) %
SFG_AI_UPDATE_FRAME_INTERVAL == 0)
{
for (uint16_t i = 0; i < SFG_currentLevel.monsterRecordCount; ++i)
{
SFG_MonsterRecord *monster = &(SFG_currentLevel.monsterRecords[i]);
uint8_t state = SFG_MR_STATE(*monster);
2020-01-12 10:48:42 -05:00
2020-02-15 17:04:22 -05:00
if (state == SFG_MONSTER_STATE_DYING)
{
// remove dead
2020-02-08 14:53:10 -05:00
2020-02-15 17:04:22 -05:00
for (uint16_t j = i; j < SFG_currentLevel.monsterRecordCount - 1; ++j)
SFG_currentLevel.monsterRecords[j] =
SFG_currentLevel.monsterRecords[j + 1];
2020-02-09 09:03:15 -05:00
2020-02-15 17:04:22 -05:00
SFG_currentLevel.monsterRecordCount -= 1;
2019-10-25 18:50:22 -04:00
2020-02-15 17:04:22 -05:00
i--;
}
else if (monster->health == 0)
{
2020-03-28 15:48:16 -04:00
monster->stateType = (monster->stateType & SFG_MONSTER_MASK_TYPE) |
SFG_MONSTER_STATE_DYING;
2020-02-15 17:04:22 -05:00
}
else if (state != SFG_MONSTER_STATE_INACTIVE)
{
#if SFG_PREVIEW_MODE == 0
SFG_monsterPerformAI(monster);
2020-02-01 17:17:22 -05:00
#endif
2020-02-15 17:04:22 -05:00
}
}
}
}
2020-02-09 09:03:15 -05:00
2020-02-21 15:21:09 -05:00
/**
Maps square position on the map to a bit in map reveal mask.
*/
static inline uint16_t SFG_getMapRevealBit(uint8_t squareX, uint8_t squareY)
{
return 1 << ((squareY / 16) * 4 + squareX / 16);
}
2020-02-15 17:04:22 -05:00
/**
Part of SFG_gameStep() for SFG_GAME_STATE_PLAYING.
*/
void SFG_gameStepPlaying()
{
2020-02-16 07:20:22 -05:00
if (
(SFG_keyIsDown(SFG_KEY_C) && SFG_keyIsDown(SFG_KEY_DOWN)) ||
SFG_keyIsDown(SFG_KEY_MENU))
{
SFG_setGameState(SFG_GAME_STATE_MENU);
return;
}
2020-03-22 06:31:02 -04:00
int8_t recomputeDirection = SFG_currentLevel.frameStart == SFG_game.frame;
2020-02-09 09:03:15 -05:00
2020-02-15 17:04:22 -05:00
RCL_Vector2D moveOffset;
2020-02-09 09:03:15 -05:00
2020-02-15 17:04:22 -05:00
moveOffset.x = 0;
moveOffset.y = 0;
2020-02-09 09:03:15 -05:00
2020-02-15 17:04:22 -05:00
int8_t strafe = 0;
2020-02-09 09:03:15 -05:00
2020-02-15 17:04:22 -05:00
#if SFG_HEADBOB_ENABLED
int8_t bobbing = 0;
#endif
2020-02-09 09:03:15 -05:00
2020-02-15 17:04:22 -05:00
int8_t shearing = 0;
2020-02-09 09:03:15 -05:00
2020-02-15 17:04:22 -05:00
if (SFG_keyJustPressed(SFG_KEY_TOGGLE_FREELOOK))
SFG_player.freeLook = !SFG_player.freeLook;
2020-02-09 09:03:15 -05:00
2020-02-15 17:04:22 -05:00
#if SFG_PREVIEW_MODE == 0
if (
SFG_keyIsDown(SFG_KEY_B) &&
!SFG_keyIsDown(SFG_KEY_C) &&
(SFG_game.frame - SFG_player.weaponCooldownStartFrame >
2020-03-13 16:53:30 -04:00
RCL_max(SFG_MIN_WEAPON_COOLDOWN_FRAMES,
SFG_GET_WEAPON_FIRE_COOLDOWN_FRAMES(SFG_player.weapon))))
2020-02-15 17:04:22 -05:00
{
// player: attack, shoot, fire
2019-10-21 20:03:44 -04:00
2020-02-15 17:04:22 -05:00
uint8_t ammo, projectileCount, canShoot;
2019-10-22 06:13:10 -04:00
2020-02-15 17:04:22 -05:00
SFG_getPlayerWeaponInfo(&ammo,&projectileCount,&canShoot);
2019-10-22 06:13:10 -04:00
2020-02-15 17:04:22 -05:00
if (canShoot)
{
uint8_t sound;
2019-10-22 06:13:10 -04:00
2020-02-15 17:04:22 -05:00
switch (SFG_player.weapon)
{
case SFG_WEAPON_KNIFE: sound = 255; break;
case SFG_WEAPON_ROCKET_LAUNCHER:
2020-02-27 14:18:35 -05:00
case SFG_WEAPON_SHOTGUN: sound = 2; break;
2020-02-15 17:04:22 -05:00
case SFG_WEAPON_PLASMAGUN:
case SFG_WEAPON_SOLUTION: sound = 4; break;
default: sound = 0; break;
}
2019-10-22 06:13:10 -04:00
2020-02-15 17:04:22 -05:00
if (sound != 255)
SFG_playSoundSafe(sound,255);
2019-10-22 06:13:10 -04:00
2020-02-15 17:04:22 -05:00
if (ammo != SFG_AMMO_NONE)
SFG_player.ammo[ammo] -= projectileCount;
2019-10-22 06:13:10 -04:00
2020-02-15 17:04:22 -05:00
uint8_t projectile;
2019-10-21 09:21:22 -04:00
2020-02-15 17:04:22 -05:00
switch (SFG_GET_WEAPON_FIRE_TYPE(SFG_player.weapon))
{
case SFG_WEAPON_FIRE_TYPE_PLASMA:
projectile = SFG_PROJECTILE_PLASMA;
break;
2019-10-21 13:58:11 -04:00
2020-02-15 17:04:22 -05:00
case SFG_WEAPON_FIRE_TYPE_FIREBALL:
projectile = SFG_PROJECTILE_FIREBALL;
break;
2019-10-21 09:21:22 -04:00
2020-02-15 17:04:22 -05:00
case SFG_WEAPON_FIRE_TYPE_BULLET:
projectile = SFG_PROJECTILE_BULLET;
break;
2020-01-02 11:22:00 -05:00
2020-02-15 17:04:22 -05:00
case SFG_WEAPON_FIRE_TYPE_MELEE:
projectile = SFG_PROJECTILE_NONE;
break;
2020-01-02 11:22:00 -05:00
2020-02-15 17:04:22 -05:00
default:
projectile = 255;
break;
}
if (projectile != SFG_PROJECTILE_NONE)
{
uint16_t angleAdd = SFG_PROJECTILE_SPREAD_ANGLE / (projectileCount + 1);
2019-10-21 09:21:22 -04:00
2020-02-15 17:04:22 -05:00
RCL_Unit direction =
(SFG_player.camera.direction - SFG_PROJECTILE_SPREAD_ANGLE / 2)
+ angleAdd;
2019-10-21 09:21:22 -04:00
2020-02-15 17:04:22 -05:00
for (uint8_t i = 0; i < projectileCount; ++i)
{
SFG_launchProjectile(
projectile,
SFG_player.camera.position,
SFG_player.camera.height,
RCL_angleToDirection(direction),
(SFG_player.camera.shear *
SFG_GET_PROJECTILE_SPEED_UPS(projectile)) /
SFG_CAMERA_MAX_SHEAR_PIXELS,
SFG_PROJECTILE_SPAWN_OFFSET
);
2019-10-21 09:21:22 -04:00
2020-02-15 17:04:22 -05:00
direction += angleAdd;
}
2019-10-21 09:21:22 -04:00
}
2020-02-15 17:04:22 -05:00
else
{
// player's melee attack
2019-10-21 09:21:22 -04:00
2020-02-15 17:04:22 -05:00
for (uint16_t i = 0; i < SFG_currentLevel.monsterRecordCount; ++i)
2020-02-10 06:55:15 -05:00
{
2020-02-15 17:04:22 -05:00
SFG_MonsterRecord *m = &(SFG_currentLevel.monsterRecords[i]);
2020-02-10 06:55:15 -05:00
2020-02-15 17:04:22 -05:00
if (SFG_MR_STATE(*m) == SFG_MONSTER_STATE_INACTIVE)
continue;
2019-10-21 18:03:43 -04:00
2020-02-15 17:04:22 -05:00
RCL_Unit pX, pY, pZ;
SFG_getMonsterWorldPosition(m,&pX,&pY,&pZ);
2019-10-21 18:03:43 -04:00
2020-02-15 17:04:22 -05:00
if (SFG_taxicabDistance(pX,pY,pZ,
SFG_player.camera.position.x,
SFG_player.camera.position.y,
SFG_player.camera.height) > SFG_MELEE_RANGE)
continue;
RCL_Vector2D toMonster;
2020-02-10 06:55:15 -05:00
2020-02-15 17:04:22 -05:00
toMonster.x = pX - SFG_player.camera.position.x;
toMonster.y = pY - SFG_player.camera.position.y;
2020-01-11 16:12:03 -05:00
2020-02-15 17:04:22 -05:00
if (RCL_vectorsAngleCos(SFG_player.direction,toMonster) >=
(RCL_UNITS_PER_SQUARE - SFG_PLAYER_MELEE_ANGLE))
2020-01-11 16:12:03 -05:00
{
2020-02-15 17:04:22 -05:00
SFG_monsterChangeHealth(m,
-1 * SFG_getDamageValue(SFG_WEAPON_FIRE_TYPE_MELEE));
SFG_createDust(pX,pY,pZ);
2020-01-11 16:12:03 -05:00
break;
}
2019-10-21 17:50:19 -04:00
}
2020-02-15 17:04:22 -05:00
}
2019-10-21 18:03:43 -04:00
2020-02-15 17:04:22 -05:00
SFG_player.weaponCooldownStartFrame = SFG_game.frame;
2019-10-21 18:03:43 -04:00
2020-02-15 17:04:22 -05:00
if (ammo != SFG_AMMO_NONE && SFG_player.ammo[ammo] == 0)
SFG_playerRotateWeapon(1);
} // endif: has enough ammo?
} // attack
#endif // SFG_PREVIEW_MODE == 0
2020-01-11 13:22:14 -05:00
2020-02-15 17:04:22 -05:00
if (SFG_keyJustPressed(SFG_KEY_NEXT_WEAPON))
SFG_playerRotateWeapon(1);
else if (SFG_keyJustPressed(SFG_KEY_PREVIOUS_WEAPON))
SFG_playerRotateWeapon(0);
2020-01-11 13:22:14 -05:00
2020-02-15 17:04:22 -05:00
if (SFG_keyIsDown(SFG_KEY_A))
{
if (SFG_keyIsDown(SFG_KEY_UP))
{
SFG_player.camera.shear =
RCL_min(SFG_CAMERA_MAX_SHEAR_PIXELS,
SFG_player.camera.shear + SFG_CAMERA_SHEAR_STEP_PER_FRAME);
shearing = 1;
}
else if (SFG_keyIsDown(SFG_KEY_DOWN))
{
SFG_player.camera.shear =
RCL_max(-1 * SFG_CAMERA_MAX_SHEAR_PIXELS,
SFG_player.camera.shear - SFG_CAMERA_SHEAR_STEP_PER_FRAME);
shearing = 1;
2019-10-21 17:50:19 -04:00
}
2019-10-21 13:58:11 -04:00
2020-02-15 17:04:22 -05:00
if (!SFG_keyIsDown(SFG_KEY_C))
2019-10-21 09:21:22 -04:00
{
2020-02-15 17:04:22 -05:00
if (SFG_keyIsDown(SFG_KEY_LEFT))
strafe = -1;
else if (SFG_keyIsDown(SFG_KEY_RIGHT))
strafe = 1;
}
else
{
if (SFG_keyJustPressed(SFG_KEY_LEFT) || SFG_keyJustPressed(SFG_KEY_A))
SFG_playerRotateWeapon(0);
else if (SFG_keyJustPressed(SFG_KEY_RIGHT) ||
SFG_keyJustPressed(SFG_KEY_B))
SFG_playerRotateWeapon(1);
}
}
else
{
if (!SFG_keyIsDown(SFG_KEY_C))
{
if (SFG_keyIsDown(SFG_KEY_LEFT))
{
SFG_player.camera.direction -= SFG_PLAYER_TURN_UNITS_PER_FRAME;
recomputeDirection = 1;
}
else if (SFG_keyIsDown(SFG_KEY_RIGHT))
{
SFG_player.camera.direction += SFG_PLAYER_TURN_UNITS_PER_FRAME;
recomputeDirection = 1;
}
}
else
{
if (SFG_keyJustPressed(SFG_KEY_LEFT) ||
SFG_keyJustPressed(SFG_KEY_A))
SFG_playerRotateWeapon(0);
else if (SFG_keyJustPressed(SFG_KEY_RIGHT) ||
SFG_keyJustPressed(SFG_KEY_B))
SFG_playerRotateWeapon(1);
}
2019-10-21 14:07:28 -04:00
2020-02-15 17:04:22 -05:00
int16_t mouseX, mouseY;
2019-10-21 09:21:22 -04:00
2020-02-15 17:04:22 -05:00
SFG_getMouseOffset(&mouseX,&mouseY);
2019-10-21 09:21:22 -04:00
2020-02-15 17:04:22 -05:00
if (mouseX != 0 || mouseY != 0)
{
SFG_player.camera.direction +=
(mouseX * SFG_MOUSE_SENSITIVITY_HORIZONTAL) / 128;
2019-10-21 09:21:22 -04:00
2020-02-15 17:04:22 -05:00
if (SFG_player.freeLook)
SFG_player.camera.shear =
RCL_max(RCL_min(
SFG_player.camera.shear - (mouseY * SFG_MOUSE_SENSITIVITY_VERTICAL)
/ 128,
SFG_CAMERA_MAX_SHEAR_PIXELS),
-1 * SFG_CAMERA_MAX_SHEAR_PIXELS);
recomputeDirection = 1;
2019-10-21 09:21:22 -04:00
}
2020-02-15 17:04:22 -05:00
if (recomputeDirection)
2020-03-22 14:51:51 -04:00
SFG_recomputePLayerDirection();
2020-02-15 17:04:22 -05:00
if (SFG_keyIsDown(SFG_KEY_UP))
2019-10-21 09:21:22 -04:00
{
2020-02-15 17:04:22 -05:00
moveOffset.x += SFG_player.direction.x;
moveOffset.y += SFG_player.direction.y;
#if SFG_HEADBOB_ENABLED
bobbing = 1;
#endif
2019-10-21 09:21:22 -04:00
}
2020-02-15 17:04:22 -05:00
else if (SFG_keyIsDown(SFG_KEY_DOWN))
{
moveOffset.x -= SFG_player.direction.x;
moveOffset.y -= SFG_player.direction.y;
#if SFG_HEADBOB_ENABLED
bobbing = 1;
#endif
}
}
2019-10-21 13:58:11 -04:00
2020-02-15 17:04:22 -05:00
if (SFG_keyIsDown(SFG_KEY_STRAFE_LEFT))
strafe = -1;
else if (SFG_keyIsDown(SFG_KEY_STRAFE_RIGHT))
strafe = 1;
if (strafe != 0)
{
moveOffset.x += strafe * SFG_player.direction.y;
moveOffset.y -= strafe * SFG_player.direction.x;
2019-10-21 09:21:22 -04:00
}
2020-02-15 17:04:22 -05:00
#if SFG_PREVIEW_MODE
if (SFG_keyIsDown(SFG_KEY_B))
SFG_player.verticalSpeed = SFG_PLAYER_MOVE_UNITS_PER_FRAME;
else if (SFG_keyIsDown(SFG_KEY_C))
SFG_player.verticalSpeed = -1 * SFG_PLAYER_MOVE_UNITS_PER_FRAME;
else
SFG_player.verticalSpeed = 0;
#else
RCL_Unit verticalOffset =
(
(
SFG_keyIsDown(SFG_KEY_JUMP) ||
(SFG_keyIsDown(SFG_KEY_UP) && SFG_keyIsDown(SFG_KEY_C))
) &&
(SFG_player.verticalSpeed == 0) &&
(SFG_player.previousVerticalSpeed == 0)) ?
2020-03-28 12:34:09 -04:00
SFG_PLAYER_JUMP_OFFSET_PER_FRAME : // jump
2020-02-15 17:04:22 -05:00
(SFG_player.verticalSpeed - SFG_GRAVITY_SPEED_INCREASE_PER_FRAME);
#endif
if (!shearing && SFG_player.camera.shear != 0 && !SFG_player.freeLook)
2019-10-06 10:47:47 -04:00
{
2020-02-15 17:04:22 -05:00
// gradually shear back to zero
2019-10-06 10:47:47 -04:00
2020-02-15 17:04:22 -05:00
SFG_player.camera.shear =
(SFG_player.camera.shear > 0) ?
RCL_max(0,SFG_player.camera.shear - SFG_CAMERA_SHEAR_STEP_PER_FRAME) :
RCL_min(0,SFG_player.camera.shear + SFG_CAMERA_SHEAR_STEP_PER_FRAME);
}
#if SFG_HEADBOB_ENABLED && !SFG_PREVIEW_MODE
if (bobbing)
{
SFG_player.headBobFrame += SFG_HEADBOB_FRAME_INCREASE_PER_FRAME;
}
else if (SFG_player.headBobFrame != 0)
{
// smoothly stop bobbing
uint8_t quadrant = (SFG_player.headBobFrame % RCL_UNITS_PER_SQUARE) /
(RCL_UNITS_PER_SQUARE / 4);
/* when in quadrant in which sin is going away from zero, switch to the
same value of the next quadrant, so that bobbing starts to go towards
zero immediately */
if (quadrant % 2 == 0)
SFG_player.headBobFrame =
((quadrant + 1) * RCL_UNITS_PER_SQUARE / 4) +
(RCL_UNITS_PER_SQUARE / 4 - SFG_player.headBobFrame %
(RCL_UNITS_PER_SQUARE / 4));
RCL_Unit currentFrame = SFG_player.headBobFrame;
RCL_Unit nextFrame = SFG_player.headBobFrame + 16;
// only stop bobbing when we pass frame at which sin crosses ero
SFG_player.headBobFrame =
(currentFrame / (RCL_UNITS_PER_SQUARE / 2) ==
nextFrame / (RCL_UNITS_PER_SQUARE / 2)) ?
nextFrame : 0;
}
#endif
RCL_Unit previousHeight = SFG_player.camera.height;
// handle player collision with level elements:
// monsters:
for (uint16_t i = 0; i < SFG_currentLevel.monsterRecordCount; ++i)
{
SFG_MonsterRecord *m = &(SFG_currentLevel.monsterRecords[i]);
if (SFG_MR_STATE(*m) == SFG_MONSTER_STATE_INACTIVE)
continue;
RCL_Vector2D mPos;
mPos.x = SFG_MONSTER_COORD_TO_RCL_UNITS(m->coords[0]);
mPos.y = SFG_MONSTER_COORD_TO_RCL_UNITS(m->coords[1]);
if (
SFG_elementCollides(
SFG_player.camera.position.x,
SFG_player.camera.position.y,
SFG_player.camera.height,
mPos.x,
mPos.y,
SFG_floorHeightAt(
SFG_MONSTER_COORD_TO_SQUARES(m->coords[0]),
SFG_MONSTER_COORD_TO_SQUARES(m->coords[1]))
)
)
{
moveOffset = SFG_resolveCollisionWithElement(
SFG_player.camera.position,moveOffset,mPos);
}
}
uint8_t collidesWithTeleport = 0;
2020-02-23 05:45:32 -05:00
/* item collisions with player (only those that don't stop player's movement
-- these are handled differently, via itemCollisionMap): */
2020-02-15 17:04:22 -05:00
for (int16_t i = 0; i < SFG_currentLevel.itemRecordCount; ++i)
// ^ has to be int16_t (signed)
{
if (!(SFG_currentLevel.itemRecords[i] & SFG_ITEM_RECORD_ACTIVE_MASK))
continue;
const SFG_LevelElement *e = SFG_getActiveItemElement(i);
if (e != 0)
2019-12-26 09:10:38 -05:00
{
2020-02-15 17:04:22 -05:00
RCL_Vector2D ePos;
2019-10-04 11:42:54 -04:00
2020-02-15 17:04:22 -05:00
ePos.x = SFG_ELEMENT_COORD_TO_RCL_UNITS(e->coords[0]);
ePos.y = SFG_ELEMENT_COORD_TO_RCL_UNITS(e->coords[1]);
2020-02-04 11:17:00 -05:00
2020-02-23 05:45:32 -05:00
if (!SFG_itemCollides(e->type) &&
SFG_elementCollides(
SFG_player.camera.position.x,
SFG_player.camera.position.y,
SFG_player.camera.height,
ePos.x,
ePos.y,
SFG_floorHeightAt(e->coords[0],e->coords[1]))
2020-02-15 17:04:22 -05:00
)
{
uint8_t eliminate = 1;
2020-02-08 15:41:05 -05:00
2020-02-15 17:04:22 -05:00
switch (e->type)
{
case SFG_LEVEL_ELEMENT_HEALTH:
2020-03-22 06:31:02 -04:00
if (SFG_player.health < SFG_PLAYER_MAX_HEALTH)
SFG_playerChangeHealth(SFG_HEALTH_KIT_VALUE);
else
eliminate = 0;
2020-02-15 17:04:22 -05:00
break;
2020-03-22 06:31:02 -04:00
#define addAmmo(type) \
if (SFG_player.ammo[SFG_AMMO_##type] < SFG_AMMO_MAX_##type) \
SFG_player.ammo[SFG_AMMO_##type] = RCL_min(SFG_AMMO_MAX_##type,\
SFG_player.ammo[SFG_AMMO_##type] + SFG_AMMO_INCREASE_##type);\
else\
eliminate = 0;
2020-02-15 17:04:22 -05:00
case SFG_LEVEL_ELEMENT_BULLETS:
2020-03-22 06:31:02 -04:00
addAmmo(BULLETS)
2020-02-15 17:04:22 -05:00
break;
2020-02-04 11:17:00 -05:00
2020-02-15 17:04:22 -05:00
case SFG_LEVEL_ELEMENT_ROCKETS:
2020-03-22 06:31:02 -04:00
addAmmo(ROCKETS)
2020-02-15 17:04:22 -05:00
break;
2020-02-04 11:17:00 -05:00
2020-02-15 17:04:22 -05:00
case SFG_LEVEL_ELEMENT_PLASMA:
2020-03-22 06:31:02 -04:00
addAmmo(PLASMA)
2020-02-15 17:04:22 -05:00
break;
2019-10-04 11:42:54 -04:00
2020-03-22 06:31:02 -04:00
#undef addAmmo
2020-02-15 17:04:22 -05:00
case SFG_LEVEL_ELEMENT_CARD0:
case SFG_LEVEL_ELEMENT_CARD1:
case SFG_LEVEL_ELEMENT_CARD2:
SFG_player.cards |= 1 << (e->type - SFG_LEVEL_ELEMENT_CARD0);
break;
2019-10-04 11:42:54 -04:00
2020-02-15 17:04:22 -05:00
case SFG_LEVEL_ELEMENT_TELEPORT:
collidesWithTeleport = 1;
eliminate = 0;
break;
2019-10-04 11:42:54 -04:00
2020-02-21 15:48:34 -05:00
case SFG_LEVEL_ELEMENT_FINISH:
SFG_setGameState(SFG_GAME_STATE_WIN);
eliminate = 0;
break;
2020-02-15 17:04:22 -05:00
default:
eliminate = 0;
break;
}
2019-10-04 15:09:10 -04:00
2020-02-15 17:04:22 -05:00
if (eliminate) // take the item
{
#if !SFG_PREVIEW_MODE
SFG_removeItem(i);
SFG_player.lastItemTakenFrame = SFG_game.frame;
i--;
SFG_playSoundSafe(3,255);
#endif
}
2020-02-22 17:36:15 -05:00
else if (
e->type == SFG_LEVEL_ELEMENT_TELEPORT &&
SFG_currentLevel.teleportCount > 1 &&
!SFG_player.justTeleported)
2020-02-15 17:04:22 -05:00
{
2020-02-22 17:36:15 -05:00
// teleport to random destination teleport
2019-10-06 10:47:47 -04:00
2020-02-22 17:36:15 -05:00
uint8_t teleportNumber =
SFG_random() % (SFG_currentLevel.teleportCount - 1) + 1;
2019-10-06 10:47:47 -04:00
2020-02-22 17:36:15 -05:00
for (uint16_t j = 0; j < SFG_currentLevel.itemRecordCount; ++j)
{
SFG_LevelElement e2 =
SFG_currentLevel.levelPointer->elements
[SFG_currentLevel.itemRecords[j] &
~SFG_ITEM_RECORD_ACTIVE_MASK];
2019-10-06 10:47:47 -04:00
2020-02-22 17:36:15 -05:00
if ((e2.type == SFG_LEVEL_ELEMENT_TELEPORT) && (j != i))
teleportNumber--;
2019-10-06 10:47:47 -04:00
2020-02-22 17:36:15 -05:00
if (teleportNumber == 0)
{
SFG_player.camera.position.x =
SFG_ELEMENT_COORD_TO_RCL_UNITS(e2.coords[0]);
2019-10-06 10:47:47 -04:00
2020-02-22 17:36:15 -05:00
SFG_player.camera.position.y =
SFG_ELEMENT_COORD_TO_RCL_UNITS(e2.coords[1]);
2019-10-06 10:47:47 -04:00
2020-02-22 17:36:15 -05:00
SFG_player.camera.height =
SFG_floorHeightAt(e2.coords[0],e2.coords[1]) +
RCL_CAMERA_COLL_HEIGHT_BELOW;
2019-12-26 09:10:38 -05:00
2020-02-22 17:36:15 -05:00
SFG_currentLevel.itemRecords[j] |= SFG_ITEM_RECORD_ACTIVE_MASK;
/* ^ we have to make the new teleport immediately active so
that it will immediately collide */
2019-10-04 15:09:10 -04:00
2020-02-22 17:36:15 -05:00
SFG_player.justTeleported = 1;
2019-10-04 15:09:10 -04:00
2020-02-22 17:36:15 -05:00
SFG_playSoundSafe(4,255);
2019-10-04 15:09:10 -04:00
2020-02-22 17:36:15 -05:00
break;
2020-02-15 17:04:22 -05:00
}
}
}
}
}
} // item collision check
2019-10-17 15:19:32 -04:00
2020-02-15 17:04:22 -05:00
if (!collidesWithTeleport)
SFG_player.justTeleported = 0;
2019-10-17 15:19:32 -04:00
2020-02-15 17:04:22 -05:00
#if SFG_PREVIEW_MODE
SFG_player.camera.position.x +=
SFG_PREVIEW_MODE_SPEED_MULTIPLIER * moveOffset.x;
2019-10-17 15:19:32 -04:00
2020-02-15 17:04:22 -05:00
SFG_player.camera.position.y +=
SFG_PREVIEW_MODE_SPEED_MULTIPLIER * moveOffset.y;
2019-10-17 15:19:32 -04:00
2020-02-15 17:04:22 -05:00
SFG_player.camera.height +=
SFG_PREVIEW_MODE_SPEED_MULTIPLIER * SFG_player.verticalSpeed;
#else
RCL_moveCameraWithCollision(&(SFG_player.camera),moveOffset,
2020-02-22 17:36:15 -05:00
verticalOffset,SFG_floorCollisionHeightAt,SFG_ceilingHeightAt,1,1);
2019-12-26 09:10:38 -05:00
2020-02-15 17:04:22 -05:00
SFG_player.previousVerticalSpeed = SFG_player.verticalSpeed;
2019-10-17 17:58:19 -04:00
2020-02-15 17:04:22 -05:00
RCL_Unit limit = RCL_max(RCL_max(0,verticalOffset),SFG_player.verticalSpeed);
SFG_player.verticalSpeed =
RCL_min(limit,SFG_player.camera.height - previousHeight);
/* ^ By "limit" we assure height increase caused by climbing a step doesn't
add vertical velocity. */
#endif
2019-12-29 05:39:49 -05:00
2020-02-15 17:04:22 -05:00
SFG_player.squarePosition[0] =
SFG_player.camera.position.x / RCL_UNITS_PER_SQUARE;
2019-12-29 05:39:49 -05:00
2020-02-15 17:04:22 -05:00
SFG_player.squarePosition[1] =
SFG_player.camera.position.y / RCL_UNITS_PER_SQUARE;
2019-12-29 05:39:49 -05:00
2020-02-21 15:21:09 -05:00
SFG_currentLevel.mapRevealMask |=
SFG_getMapRevealBit(
SFG_player.squarePosition[0],
SFG_player.squarePosition[1]);
2020-03-28 12:34:09 -04:00
if ( // squeezer check
(SFG_ceilingHeightAt(
SFG_player.squarePosition[0],SFG_player.squarePosition[1]) -
SFG_floorHeightAt(
SFG_player.squarePosition[0],SFG_player.squarePosition[1]))
<
(RCL_CAMERA_COLL_HEIGHT_ABOVE + RCL_CAMERA_COLL_HEIGHT_BELOW))
{
SFG_LOG("player is squeezed");
SFG_player.health = 0;
}
2020-02-15 17:04:22 -05:00
SFG_updateLevel();
2019-12-29 05:39:49 -05:00
2020-02-15 17:04:22 -05:00
#if SFG_IMMORTAL == 0
if (SFG_player.health == 0)
{
SFG_LOG("player dies");
SFG_setGameState(SFG_GAME_STATE_LOSE);
2019-12-29 05:39:49 -05:00
}
2020-02-15 17:04:22 -05:00
#endif
2019-09-25 09:51:19 -04:00
}
2020-02-12 06:41:23 -05:00
uint8_t SFG_getMenuItem(uint8_t index)
{
2020-02-16 07:20:22 -05:00
uint8_t start = (SFG_currentLevel.levelPointer == 0) ? 2 : 0;
if (index <= (SFG_MENU_ITEM_EXIT - start))
return start + index;
2020-02-12 06:41:23 -05:00
return SFG_MENU_ITEM_NONE;
}
void SFG_gameStepMenu()
{
uint8_t menuItems = 0;
while (SFG_getMenuItem(menuItems) != SFG_MENU_ITEM_NONE)
menuItems++;
2020-02-12 08:44:51 -05:00
uint8_t item = SFG_getMenuItem(SFG_game.selectedMenuItem);
if (SFG_keyRegisters(SFG_KEY_DOWN) &&
(SFG_game.selectedMenuItem < menuItems - 1))
2020-02-12 06:41:23 -05:00
{
SFG_game.selectedMenuItem++;
SFG_playSoundSafe(3,64);
}
2020-02-12 08:44:51 -05:00
else if (SFG_keyRegisters(SFG_KEY_UP) && (SFG_game.selectedMenuItem > 0))
2020-02-12 06:41:23 -05:00
{
SFG_game.selectedMenuItem--;
SFG_playSoundSafe(3,64);
}
else if (SFG_keyJustPressed(SFG_KEY_A))
{
2020-02-12 08:44:51 -05:00
switch (item)
2020-02-12 06:41:23 -05:00
{
case SFG_MENU_ITEM_PLAY:
2020-02-17 02:48:41 -05:00
if (SFG_game.selectedLevel == 0)
{
SFG_setGameState(SFG_GAME_STATE_INTRO);
}
else
{
SFG_setAndInitLevel(&SFG_levels[SFG_game.selectedLevel]);
SFG_setGameState(SFG_GAME_STATE_PLAYING);
}
2020-02-12 06:41:23 -05:00
break;
2020-02-16 07:20:22 -05:00
case SFG_MENU_ITEM_CONTINUE:
SFG_setGameState(SFG_GAME_STATE_PLAYING);
break;
case SFG_MENU_ITEM_MAP:
SFG_setGameState(SFG_GAME_STATE_MAP);
break;
2020-02-12 06:41:23 -05:00
default:
break;
}
}
2020-02-12 08:44:51 -05:00
else if (item == SFG_MENU_ITEM_PLAY)
{
2020-02-12 10:52:18 -05:00
if (SFG_keyRegisters(SFG_KEY_RIGHT) &&
(SFG_game.selectedLevel < SFG_NUMBER_OF_LEVELS - 1))
2020-02-12 08:44:51 -05:00
{
SFG_game.selectedLevel++;
SFG_playSoundSafe(3,64);
}
else if (SFG_keyRegisters(SFG_KEY_LEFT) && SFG_game.selectedLevel > 0)
{
SFG_game.selectedLevel--;
SFG_playSoundSafe(3,64);
}
}
2020-02-12 06:41:23 -05:00
}
2020-02-12 02:18:38 -05:00
/**
Performs one game step (logic, physics, menu, ...), happening SFG_MS_PER_FRAME
after previous frame.
*/
void SFG_gameStep()
{
2020-04-01 06:20:18 -04:00
SFG_game.soundsPlayedThisFrame = 0;
2020-02-12 02:18:38 -05:00
for (uint8_t i = 0; i < SFG_KEY_COUNT; ++i)
if (!SFG_keyPressed(i))
SFG_game.keyStates[i] = 0;
else if (SFG_game.keyStates[i] < 255)
SFG_game.keyStates[i]++;
if ((SFG_currentLevel.frameStart - SFG_game.frame) %
SFG_SPRITE_ANIMATION_FRAME_DURATION == 0)
SFG_game.spriteAnimationFrame++;
switch (SFG_game.state)
{
case SFG_GAME_STATE_PLAYING:
SFG_gameStepPlaying();
break;
case SFG_GAME_STATE_MENU:
2020-02-12 06:41:23 -05:00
SFG_gameStepMenu();
2020-02-12 02:18:38 -05:00
break;
2020-02-15 17:04:22 -05:00
case SFG_GAME_STATE_LOSE:
2020-02-16 04:32:12 -05:00
{
2020-02-21 15:48:34 -05:00
// player die animation (lose)
2020-02-16 04:32:12 -05:00
2020-02-15 17:04:22 -05:00
SFG_updateLevel();
int32_t t = SFG_game.frameTime - SFG_game.stateChangeTime;
RCL_Unit h = SFG_floorHeightAt(
SFG_player.squarePosition[0],
2020-02-16 04:32:12 -05:00
SFG_player.squarePosition[1]);
2020-02-15 17:04:22 -05:00
SFG_player.camera.height =
2020-02-16 04:32:12 -05:00
RCL_max(h,h + ((SFG_LOSE_ANIMATION_DURATION - t) *
RCL_CAMERA_COLL_HEIGHT_BELOW) / SFG_LOSE_ANIMATION_DURATION);
2020-02-16 04:39:06 -05:00
SFG_player.camera.shear =
RCL_min(SFG_CAMERA_MAX_SHEAR_PIXELS / 4,
(t * (SFG_CAMERA_MAX_SHEAR_PIXELS / 4)) / SFG_LOSE_ANIMATION_DURATION);
2020-02-15 17:04:22 -05:00
break;
}
2020-02-21 15:48:34 -05:00
case SFG_GAME_STATE_WIN:
{
// win animation
SFG_updateLevel();
int32_t t = SFG_game.frameTime - SFG_game.stateChangeTime;
if ((t > SFG_WIN_ANIMATION_DURATION) && SFG_keyIsDown(SFG_KEY_A))
SFG_setGameState(SFG_GAME_STATE_MENU);
break;
}
2020-02-16 07:20:22 -05:00
case SFG_GAME_STATE_MAP:
if (SFG_keyIsDown(SFG_KEY_B))
SFG_setGameState(SFG_GAME_STATE_MENU);
break;
2020-02-17 02:48:41 -05:00
case SFG_GAME_STATE_INTRO:
if (SFG_keyJustPressed(SFG_KEY_A))
{
SFG_setAndInitLevel(&SFG_levels[0]);
SFG_setGameState(SFG_GAME_STATE_PLAYING);
}
break;
2020-02-12 02:18:38 -05:00
default:
break;
}
}
2020-02-22 03:23:11 -05:00
void SFG_fillRectangle(
uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t color)
2019-10-06 11:03:15 -04:00
{
2020-02-22 03:23:11 -05:00
for (uint16_t j = y; j < y + height; ++j)
for (uint16_t i = x; i < x + width; ++i)
2019-10-08 07:46:12 -04:00
SFG_setGamePixel(i,j,color);
}
2020-02-22 03:23:11 -05:00
static inline void SFG_clearScreen(uint8_t color)
{
SFG_fillRectangle(0,0,SFG_GAME_RESOLUTION_X,SFG_GAME_RESOLUTION_Y,color);
}
2019-10-08 07:46:12 -04:00
/**
Draws fullscreen map of the current level.
*/
void SFG_drawMap()
{
SFG_clearScreen(0);
uint16_t maxJ =
(SFG_MAP_PIXEL_SIZE * SFG_MAP_SIZE) < SFG_GAME_RESOLUTION_Y ?
(SFG_MAP_SIZE) : (SFG_GAME_RESOLUTION_Y / SFG_MAP_PIXEL_SIZE);
uint16_t maxI =
(SFG_MAP_PIXEL_SIZE * SFG_MAP_SIZE) < SFG_GAME_RESOLUTION_X ?
(SFG_MAP_SIZE) : (SFG_GAME_RESOLUTION_X / SFG_MAP_PIXEL_SIZE);
uint16_t topLeftX =
(SFG_GAME_RESOLUTION_X - (maxI * SFG_MAP_PIXEL_SIZE)) / 2;
uint16_t topLeftY =
(SFG_GAME_RESOLUTION_Y - (maxJ * SFG_MAP_PIXEL_SIZE)) / 2;
uint16_t x;
uint16_t y = topLeftY;
2019-10-06 11:03:15 -04:00
2020-03-07 16:43:49 -05:00
for (int16_t j = 0; j < maxJ; ++j)
2019-10-08 07:46:12 -04:00
{
x = topLeftX;
2020-03-07 16:43:49 -05:00
for (int16_t i = maxI - 1; i >= 0; --i)
2019-10-06 11:03:15 -04:00
{
2020-02-21 15:21:09 -05:00
uint8_t color = 0; // init with non-revealed color
2019-10-06 11:03:15 -04:00
2020-02-21 15:21:09 -05:00
if (SFG_currentLevel.mapRevealMask & SFG_getMapRevealBit(i,j))
{
uint8_t properties;
2019-10-06 11:03:15 -04:00
2020-02-21 15:21:09 -05:00
SFG_TileDefinition tile =
SFG_getMapTile(SFG_currentLevel.levelPointer,i,j,&properties);
2019-10-08 07:46:12 -04:00
2020-02-21 15:21:09 -05:00
color = 94; // start with player color
if (i != SFG_player.squarePosition[0] ||
j != SFG_player.squarePosition[1])
2019-10-08 07:46:12 -04:00
{
2020-02-21 15:21:09 -05:00
if (properties == SFG_TILE_PROPERTY_ELEVATOR)
color = 46;
else if (properties == SFG_TILE_PROPERTY_SQUEEZER)
color = 63;
else
{
2020-03-07 16:43:49 -05:00
color =
(SFG_TILE_FLOOR_HEIGHT(tile) - SFG_TILE_CEILING_HEIGHT(tile))
/ 8 + 2;
2019-10-06 11:03:15 -04:00
2020-02-21 15:21:09 -05:00
if (properties == SFG_TILE_PROPERTY_DOOR)
color += 8;
}
2019-10-08 07:46:12 -04:00
}
}
2019-10-06 11:03:15 -04:00
2019-10-08 07:46:12 -04:00
for (uint16_t k = 0; k < SFG_MAP_PIXEL_SIZE; ++k)
for (uint16_t l = 0; l < SFG_MAP_PIXEL_SIZE; ++l)
SFG_setGamePixel(x + l, y + k,color);
2019-10-06 11:03:15 -04:00
2019-10-08 07:46:12 -04:00
x += SFG_MAP_PIXEL_SIZE;
2019-10-06 11:03:15 -04:00
}
2019-10-08 07:46:12 -04:00
y += SFG_MAP_PIXEL_SIZE;
2019-10-10 15:58:46 -04:00
}
}
/**
Draws text on screen using the bitmap font stored in assets.
*/
void SFG_drawText(
const char *text,
uint16_t x,
uint16_t y,
uint8_t size,
2020-02-17 02:48:41 -05:00
uint8_t color,
uint16_t maxLength,
uint16_t limitX)
2019-10-10 15:58:46 -04:00
{
2019-10-10 16:31:50 -04:00
if (size == 0)
size = 1;
2020-02-17 02:48:41 -05:00
if (limitX == 0)
limitX = 65535;
if (maxLength == 0)
maxLength = 65535;
2019-10-10 15:58:46 -04:00
uint16_t pos = 0;
uint16_t currentX = x;
uint16_t currentY = y;
2020-02-17 02:48:41 -05:00
while (text[pos] != 0 && pos < maxLength) // for each character
2019-10-10 15:58:46 -04:00
{
uint16_t character = SFG_font[SFG_charToFontIndex(text[pos])];
2020-02-17 02:48:41 -05:00
for (uint8_t i = 0; i < SFG_FONT_CHARACTER_SIZE; ++i) // for each line
2019-10-10 15:58:46 -04:00
{
currentY = y;
2020-02-17 02:48:41 -05:00
for (uint8_t j = 0; j < SFG_FONT_CHARACTER_SIZE; ++j) // for each row
2019-10-10 15:58:46 -04:00
{
if (character & 0x8000)
for (uint8_t k = 0; k < size; ++k)
for (uint8_t l = 0; l < size; ++l)
{
uint16_t drawX = currentX + k;
uint16_t drawY = currentY + l;
2019-10-10 16:31:50 -04:00
if (drawX < SFG_GAME_RESOLUTION_X &&
drawY < SFG_GAME_RESOLUTION_Y)
2019-10-10 15:58:46 -04:00
SFG_setGamePixel(drawX,drawY,color);
}
currentY += size;
character = character << 1;
}
currentX += size;
}
2020-02-17 02:48:41 -05:00
currentX += size; // space
2019-10-10 15:58:46 -04:00
2020-02-17 02:48:41 -05:00
if (currentX > limitX)
{
currentX = x;
y += (SFG_FONT_CHARACTER_SIZE + 1) * size;
}
2019-10-10 15:58:46 -04:00
pos++;
2019-10-08 07:46:12 -04:00
}
}
2020-02-17 02:48:41 -05:00
/**
Draws fullscreen story text (intro/outro).
*/
void SFG_drawStoryText()
{
SFG_clearScreen(0);
const char *text = SFG_introText;
uint16_t textLen = 0;
while (text[textLen] != 0)
textLen++;
uint16_t drawLen =
RCL_min(textLen,
((SFG_game.frameTime - SFG_game.stateChangeTime) * textLen) /
SFG_STORYTEXT_DURATION + 1);
SFG_drawText(text,SFG_HUD_MARGIN,SFG_HUD_MARGIN,SFG_FONT_SIZE_SMALL,7,
drawLen,SFG_GAME_RESOLUTION_X - SFG_HUD_MARGIN);
}
2019-10-10 19:03:56 -04:00
/**
Draws a number as text on screen, returns the number of characters drawn.
*/
uint8_t SFG_drawNumber(
int16_t number,
uint16_t x,
uint16_t y,
uint8_t size,
2020-02-17 02:48:41 -05:00
uint8_t color)
2019-10-10 19:03:56 -04:00
{
char text[7];
text[6] = 0; // terminate the string
int8_t positive = 1;
if (number < 0)
{
positive = 0;
number *= -1;
}
int8_t position = 5;
while (1)
{
text[position] = '0' + number % 10;
number /= 10;
position--;
if (number == 0 || position == 0)
break;
}
if (!positive)
{
text[position] = '-';
position--;
}
2020-02-17 02:48:41 -05:00
SFG_drawText(text + position + 1,x,y,size,color,0,0);
2019-10-10 19:03:56 -04:00
return 5 - position;
}
2020-01-12 10:48:42 -05:00
/**
Draws a border that indicates something is happening, e.g. being hurt or
taking an item.
*/
void SFG_drawIndicationBorder(uint16_t width, uint8_t color)
2019-10-25 16:41:52 -04:00
{
for (uint16_t j = 0; j < width; ++j)
{
uint16_t j2 = SFG_GAME_RESOLUTION_Y - 1 - j;
for (uint16_t i = 0; i < SFG_GAME_RESOLUTION_X; ++i)
{
2019-10-25 17:06:47 -04:00
if ((i & 0x01) == (j & 0x01))
{
2019-10-25 17:17:59 -04:00
SFG_setGamePixel(i,j,color);
SFG_setGamePixel(i,j2,color);
2019-10-25 17:06:47 -04:00
}
2019-10-25 16:41:52 -04:00
}
}
for (uint16_t i = 0; i < width; ++i)
{
uint16_t i2 = SFG_GAME_RESOLUTION_X - 1 - i;
for (uint16_t j = width; j < SFG_GAME_RESOLUTION_Y - width; ++j)
{
2019-10-25 17:06:47 -04:00
if ((i & 0x01) == (j & 0x01))
{
2019-10-25 17:17:59 -04:00
SFG_setGamePixel(i,j,color);
SFG_setGamePixel(i2,j,color);
2019-10-25 17:06:47 -04:00
}
2019-10-25 16:41:52 -04:00
}
}
}
2019-10-23 18:32:04 -04:00
/**
Draws the player weapon, handling the shooting animation.
*/
2019-10-22 15:02:39 -04:00
void SFG_drawWeapon(int16_t bobOffset)
{
2020-01-01 11:06:11 -05:00
uint32_t shotAnimationFrame =
2020-02-10 12:10:06 -05:00
SFG_game.frame - SFG_player.weaponCooldownStartFrame;
2019-10-22 15:02:39 -04:00
2020-02-12 10:52:18 -05:00
uint32_t animationLength =
2020-03-13 16:53:30 -04:00
RCL_max(SFG_MIN_WEAPON_COOLDOWN_FRAMES,
SFG_GET_WEAPON_FIRE_COOLDOWN_FRAMES(SFG_player.weapon));
2019-10-22 15:02:39 -04:00
2019-11-09 16:01:19 -05:00
bobOffset -= SFG_HUD_BAR_HEIGHT;
2020-01-01 11:06:11 -05:00
uint8_t fireType = SFG_GET_WEAPON_FIRE_TYPE(SFG_player.weapon);
2019-11-09 16:01:19 -05:00
2019-10-22 15:02:39 -04:00
if (shotAnimationFrame < animationLength)
{
2020-01-01 11:06:11 -05:00
if (fireType == SFG_WEAPON_FIRE_TYPE_MELEE)
2019-10-23 18:32:04 -04:00
{
bobOffset = shotAnimationFrame < animationLength / 2 ? 0 :
2 * SFG_WEAPONBOB_OFFSET_PIXELS ;
}
else
{
bobOffset +=
((animationLength - shotAnimationFrame) * SFG_WEAPON_IMAGE_SCALE * 20)
/ animationLength;
2019-11-09 05:59:52 -05:00
if (
2020-01-01 11:06:11 -05:00
((fireType == SFG_WEAPON_FIRE_TYPE_FIREBALL) ||
2020-01-01 14:53:05 -05:00
(fireType == SFG_WEAPON_FIRE_TYPE_BULLET)) &&
2019-11-09 05:59:52 -05:00
shotAnimationFrame < animationLength / 2)
2019-10-23 18:32:04 -04:00
SFG_blitImage(SFG_effectSprites[0],
SFG_WEAPON_IMAGE_POSITION_X,
2020-02-12 10:52:18 -05:00
SFG_WEAPON_IMAGE_POSITION_Y -
(SFG_TEXTURE_SIZE / 3) * SFG_WEAPON_IMAGE_SCALE + bobOffset,
2019-10-23 18:32:04 -04:00
SFG_WEAPON_IMAGE_SCALE);
}
2019-10-22 15:02:39 -04:00
}
SFG_blitImage(SFG_weaponImages[SFG_player.weapon],
SFG_WEAPON_IMAGE_POSITION_X,
2019-11-08 15:12:46 -05:00
SFG_WEAPON_IMAGE_POSITION_Y + bobOffset - 1,
2019-10-22 15:02:39 -04:00
SFG_WEAPON_IMAGE_SCALE);
}
2020-03-13 16:53:30 -04:00
uint16_t SFG_textLen(const char *text)
{
uint16_t result = 0;
while (text[result] != 0)
result++;
return result;
}
static inline uint16_t SFG_characterSize(uint8_t textSize)
{
return (SFG_FONT_CHARACTER_SIZE + 1) * textSize;
}
static inline uint16_t
SFG_textHorizontalSize(const char *text, uint8_t textSize)
{
return (SFG_textLen(text) * SFG_characterSize(textSize));
}
2020-02-11 15:24:50 -05:00
void SFG_drawMenu()
{
#define BACKGROUND_SCALE (SFG_GAME_RESOLUTION_X / (4 * SFG_TEXTURE_SIZE ) )
#if BACKGROUND_SCALE == 0
#define BACKGROUND_SCALE 1
#endif
#define SCROLL_PIXELS_PER_FRAME ((64 * SFG_GAME_RESOLUTION_X) / (8 * SFG_FPS))
#if SCROLL_PIXELS_PER_FRAME == 0
#define SCROLL_PIXELS_PER_FRAME 1
#endif
2020-02-11 15:43:30 -05:00
#define SELECTION_START_X (SFG_GAME_RESOLUTION_X / 5)
2020-02-11 15:24:50 -05:00
uint16_t scroll = (SFG_game.frame * SCROLL_PIXELS_PER_FRAME) / 64;
for (uint16_t y = 0; y < SFG_GAME_RESOLUTION_Y; ++y)
for (uint16_t x = 0; x < SFG_GAME_RESOLUTION_X; ++x)
SFG_setGamePixel(x,y,
(y >= (SFG_TEXTURE_SIZE * BACKGROUND_SCALE)) ? 0 :
SFG_getTexel(SFG_backgroundImages[0],((x + scroll) / BACKGROUND_SCALE)
% SFG_TEXTURE_SIZE,y / BACKGROUND_SCALE));
2020-03-13 16:53:30 -04:00
uint16_t y = SFG_characterSize(SFG_FONT_SIZE_MEDIUM);
2020-02-11 15:24:50 -05:00
2020-02-12 10:52:18 -05:00
SFG_blitImage(SFG_logoImage,
SFG_GAME_RESOLUTION_X / 2 - 16 * SFG_FONT_SIZE_MEDIUM,y,
SFG_FONT_SIZE_MEDIUM);
2020-02-11 15:24:50 -05:00
2020-03-13 16:53:30 -04:00
y += 32 * SFG_FONT_SIZE_MEDIUM + SFG_characterSize(SFG_FONT_SIZE_MEDIUM);
2020-02-11 15:24:50 -05:00
2020-02-12 10:52:18 -05:00
uint8_t i = 0;
2020-02-12 06:41:23 -05:00
while (1)
2020-02-11 15:24:50 -05:00
{
2020-02-12 10:52:18 -05:00
uint8_t item = SFG_getMenuItem(i);
2020-02-11 15:24:50 -05:00
2020-02-12 06:41:23 -05:00
if (item == SFG_MENU_ITEM_NONE)
2020-02-11 15:24:50 -05:00
break;
2020-02-12 10:52:18 -05:00
const char *text = SFG_menuItemTexts[item];
2020-02-12 06:41:23 -05:00
2020-03-13 16:53:30 -04:00
uint8_t textLen = SFG_textLen(text);
2020-02-11 15:24:50 -05:00
2020-03-13 16:53:30 -04:00
uint16_t drawX = (SFG_GAME_RESOLUTION_X -
SFG_textHorizontalSize(text,SFG_FONT_SIZE_MEDIUM)) / 2;
2020-02-11 15:24:50 -05:00
2020-02-12 10:52:18 -05:00
uint8_t textColor = 7;
2020-02-11 15:24:50 -05:00
2020-02-12 10:52:18 -05:00
if (i != SFG_game.selectedMenuItem)
textColor = 23;
else
2020-02-22 03:23:11 -05:00
SFG_fillRectangle(
SELECTION_START_X,
y - SFG_FONT_SIZE_MEDIUM,
SFG_GAME_RESOLUTION_X - SELECTION_START_X * 2,
2020-03-13 16:53:30 -04:00
SFG_characterSize(SFG_FONT_SIZE_MEDIUM),2);
2020-02-12 08:44:51 -05:00
2020-02-17 02:48:41 -05:00
SFG_drawText(text,drawX,y,SFG_FONT_SIZE_MEDIUM,textColor,0,0);
2020-02-11 15:24:50 -05:00
2020-03-22 06:31:02 -04:00
if (item == SFG_MENU_ITEM_PLAY &&
(((i != SFG_game.selectedMenuItem) ||
(SFG_game.frame / SFG_BLINK_PERIOD_FRAMES) % 2)))
2020-02-12 10:52:18 -05:00
SFG_drawNumber((SFG_game.selectedLevel + 1),
2020-03-13 16:53:30 -04:00
drawX + SFG_characterSize(SFG_FONT_SIZE_MEDIUM) * (textLen + 1),
y,SFG_FONT_SIZE_MEDIUM,93);
2020-02-11 15:24:50 -05:00
2020-03-13 16:53:30 -04:00
y += SFG_characterSize(SFG_FONT_SIZE_MEDIUM) + SFG_FONT_SIZE_MEDIUM;
2020-02-12 06:41:23 -05:00
i++;
2020-02-11 15:24:50 -05:00
}
2020-02-11 15:43:30 -05:00
2020-02-12 10:52:18 -05:00
SFG_drawText("0.7 CC0",SFG_HUD_MARGIN,SFG_GAME_RESOLUTION_Y - SFG_HUD_MARGIN
2020-02-17 02:48:41 -05:00
- SFG_FONT_SIZE_SMALL * SFG_FONT_CHARACTER_SIZE,SFG_FONT_SIZE_SMALL,4,0,0);
2020-02-11 15:24:50 -05:00
#undef MAX_ITEMS
#undef BACKGROUND_SCALE
#undef SCROLL_PIXELS_PER_FRAME
}
2020-02-21 15:48:34 -05:00
void SFG_drawWinOverlay()
{
uint32_t t = RCL_min(SFG_WIN_ANIMATION_DURATION,
SFG_game.frameTime - SFG_game.stateChangeTime);
2020-03-13 16:53:30 -04:00
uint32_t t2 = RCL_min(t,SFG_WIN_ANIMATION_DURATION / 4);
2020-02-22 03:23:11 -05:00
2020-03-13 16:53:30 -04:00
#define STRIP_HEIGHT (SFG_GAME_RESOLUTION_Y / 2)
#define INNER_STRIP_HEIGHT ((STRIP_HEIGHT * 3) / 4)
#define STRIP_START ((SFG_GAME_RESOLUTION_Y - STRIP_HEIGHT) / 2)
2020-02-22 03:23:11 -05:00
2020-03-13 16:53:30 -04:00
RCL_Unit l = (t2 * STRIP_HEIGHT * 4) / SFG_WIN_ANIMATION_DURATION;
2020-02-22 03:23:11 -05:00
2020-03-13 16:53:30 -04:00
uint8_t n = (t * 5) / SFG_WIN_ANIMATION_DURATION;
2020-02-22 03:23:11 -05:00
2020-03-13 16:53:30 -04:00
for (uint16_t y = STRIP_START; y < STRIP_START + l; ++y)
for (uint16_t x = 0; x < SFG_GAME_RESOLUTION_X; ++x)
SFG_setGamePixel(x,y,
RCL_absVal(y - (SFG_GAME_RESOLUTION_Y / 2)) <= (INNER_STRIP_HEIGHT / 2) ?
0 : 172);
2020-02-22 03:23:11 -05:00
2020-03-13 16:53:30 -04:00
char textLine[] = "level done";
2020-02-22 03:23:11 -05:00
2020-03-13 16:53:30 -04:00
SFG_drawText(textLine,
(SFG_GAME_RESOLUTION_X - SFG_textHorizontalSize(textLine,SFG_FONT_SIZE_BIG))
/ 2,
SFG_GAME_RESOLUTION_Y / 2 - ((STRIP_HEIGHT + INNER_STRIP_HEIGHT) / 2) / 2,
SFG_FONT_SIZE_BIG,7,255,0);
2020-02-22 03:23:11 -05:00
2020-03-13 16:53:30 -04:00
#undef STRIP_HEIGHT
#undef STRIP_START
#undef INNER_STRIP_HEIGHT
2020-02-21 15:48:34 -05:00
}
2020-03-13 11:44:02 -04:00
/**
Checks a 3D point visibility from player's position (WITHOUT considering
facing direction).
*/
static inline uint8_t SFG_spriteIsVisible(RCL_Vector2D pos, RCL_Unit height,
uint8_t spriteSize)
{
return
RCL_castRay3D(
SFG_player.camera.position,
SFG_player.camera.height,
pos,
height,
SFG_floorHeightAt,
SFG_ceilingHeightAt,
SFG_game.rayConstraints
) == RCL_UNITS_PER_SQUARE;
}
2019-10-08 07:46:12 -04:00
void SFG_draw()
{
2020-01-02 11:22:00 -05:00
#if SFG_BACKGROUND_BLUR != 0
SFG_backgroundBlurIndex = 0;
#endif
2020-02-12 02:18:38 -05:00
if (SFG_game.state == SFG_GAME_STATE_MENU)
{
SFG_drawMenu();
return;
}
2020-02-11 15:24:50 -05:00
2020-02-17 02:48:41 -05:00
if (SFG_game.state == SFG_GAME_STATE_INTRO ||
SFG_game.state == SFG_GAME_STATE_OUTRO)
{
SFG_drawStoryText();
return;
}
2020-02-16 07:20:22 -05:00
if (SFG_keyPressed(SFG_KEY_MAP) || (SFG_game.state == SFG_GAME_STATE_MAP))
2019-10-08 07:46:12 -04:00
{
SFG_drawMap();
}
else
{
2019-10-13 20:29:13 -04:00
for (uint16_t i = 0; i < SFG_Z_BUFFER_SIZE; ++i)
2020-02-10 12:10:06 -05:00
SFG_game.zBuffer[i] = 255;
2019-10-08 07:46:12 -04:00
2020-02-16 04:32:12 -05:00
int16_t weaponBobOffset = 0;
2019-10-13 20:15:13 -04:00
2019-10-09 11:44:31 -04:00
#if SFG_HEADBOB_ENABLED
2020-02-16 04:32:12 -05:00
RCL_Unit headBobOffset = 0;
2019-10-13 20:15:13 -04:00
2020-02-16 04:32:12 -05:00
if (SFG_game.state != SFG_GAME_STATE_LOSE)
{
RCL_Unit bobSin = RCL_sinInt(SFG_player.headBobFrame);
2019-10-13 20:15:13 -04:00
2020-02-16 04:32:12 -05:00
headBobOffset = (bobSin * SFG_HEADBOB_OFFSET) / RCL_UNITS_PER_SQUARE;
2019-10-09 11:44:31 -04:00
2020-02-16 04:32:12 -05:00
weaponBobOffset =
(bobSin * SFG_WEAPONBOB_OFFSET_PIXELS) / (RCL_UNITS_PER_SQUARE) +
SFG_WEAPONBOB_OFFSET_PIXELS;
}
else
{
// player die animation
int32_t t = SFG_game.frameTime - SFG_game.stateChangeTime;
weaponBobOffset = (SFG_WEAPON_IMAGE_SCALE * SFG_TEXTURE_SIZE * t) /
SFG_LOSE_ANIMATION_DURATION;
}
2019-10-09 11:44:31 -04:00
// add head bob just for the rendering
2020-02-16 04:32:12 -05:00
2019-10-09 11:44:31 -04:00
SFG_player.camera.height += headBobOffset;
#endif
2019-10-08 07:46:12 -04:00
RCL_renderComplex(
SFG_player.camera,
SFG_floorHeightAt,
SFG_ceilingHeightAt,
SFG_texturesAt,
2020-02-10 12:10:06 -05:00
SFG_game.rayConstraints);
2019-10-09 11:44:31 -04:00
2019-10-08 07:46:12 -04:00
// draw sprites:
2019-10-18 11:06:38 -04:00
// monster sprites:
2020-01-02 13:40:00 -05:00
for (uint16_t i = 0; i < SFG_currentLevel.monsterRecordCount; ++i)
2019-10-18 11:06:38 -04:00
{
SFG_MonsterRecord m = SFG_currentLevel.monsterRecords[i];
2019-12-26 09:28:23 -05:00
uint8_t state = SFG_MR_STATE(m);
2019-10-22 10:48:27 -04:00
if (state != SFG_MONSTER_STATE_INACTIVE)
2019-10-18 11:06:38 -04:00
{
RCL_Vector2D worldPosition;
2019-10-22 06:13:10 -04:00
worldPosition.x = SFG_MONSTER_COORD_TO_RCL_UNITS(m.coords[0]);
worldPosition.y = SFG_MONSTER_COORD_TO_RCL_UNITS(m.coords[1]);
2019-10-18 11:06:38 -04:00
2020-01-02 07:16:30 -05:00
uint8_t spriteSize = SFG_GET_MONSTER_SPRITE_SIZE(
SFG_MONSTER_TYPE_TO_INDEX(SFG_MR_TYPE(m)));
2020-03-13 11:44:02 -04:00
RCL_Unit worldHeight =
SFG_floorHeightAt(
SFG_MONSTER_COORD_TO_SQUARES(m.coords[0]),
SFG_MONSTER_COORD_TO_SQUARES(m.coords[1]))
+
SFG_SPRITE_SIZE_TO_HEIGHT_ABOVE_GROUND(spriteSize);
2019-10-18 11:06:38 -04:00
RCL_PixelInfo p =
2020-03-13 11:44:02 -04:00
RCL_mapToScreen(worldPosition,worldHeight,SFG_player.camera);
if (p.depth > 0 &&
SFG_spriteIsVisible(worldPosition,worldHeight,spriteSize))
2019-10-22 10:48:27 -04:00
{
2019-10-22 18:11:26 -04:00
const uint8_t *s =
SFG_getMonsterSprite(
2019-12-26 09:28:23 -05:00
SFG_MR_TYPE(m),
2019-10-22 18:11:26 -04:00
state,
2020-02-10 12:10:06 -05:00
SFG_game.spriteAnimationFrame & 0x01);
2019-10-22 10:48:27 -04:00
SFG_drawScaledSprite(s,
2019-10-18 11:06:38 -04:00
p.position.x * SFG_RAYCASTING_SUBSAMPLE,p.position.y,
2020-03-13 11:44:02 -04:00
RCL_perspectiveScaleVertical(
2020-02-27 10:13:19 -05:00
SFG_SPRITE_SIZE_PIXELS(spriteSize),
2020-01-02 07:16:30 -05:00
p.depth),
2019-10-18 11:06:38 -04:00
p.depth / (RCL_UNITS_PER_SQUARE * 2),p.depth);
2019-10-22 10:48:27 -04:00
}
2019-10-18 11:06:38 -04:00
}
}
2019-10-17 15:19:32 -04:00
// item sprites:
2020-01-02 13:40:00 -05:00
for (uint16_t i = 0; i < SFG_currentLevel.itemRecordCount; ++i)
2019-10-08 07:46:12 -04:00
if (SFG_currentLevel.itemRecords[i] & SFG_ITEM_RECORD_ACTIVE_MASK)
{
RCL_Vector2D worldPosition;
SFG_LevelElement e =
SFG_currentLevel.levelPointer->elements[
SFG_currentLevel.itemRecords[i] & ~SFG_ITEM_RECORD_ACTIVE_MASK];
worldPosition.x =
2019-10-22 06:13:10 -04:00
SFG_ELEMENT_COORD_TO_RCL_UNITS(e.coords[0]);
2019-10-08 07:46:12 -04:00
worldPosition.y =
2019-10-22 06:13:10 -04:00
SFG_ELEMENT_COORD_TO_RCL_UNITS(e.coords[1]);
2019-10-08 07:46:12 -04:00
2020-02-24 02:26:35 -05:00
const uint8_t *sprite;
2020-02-09 09:03:15 -05:00
uint8_t spriteSize;
2020-02-05 11:22:02 -05:00
2020-02-24 02:26:35 -05:00
SFG_getItemSprite(e.type,&sprite,&spriteSize);
2019-10-08 07:46:12 -04:00
2020-02-24 02:26:35 -05:00
if (sprite != 0)
2019-10-25 18:50:22 -04:00
{
2020-03-13 11:44:02 -04:00
RCL_Unit worldHeight = SFG_floorHeightAt(e.coords[0],e.coords[1])
+ SFG_SPRITE_SIZE_TO_HEIGHT_ABOVE_GROUND(spriteSize);
2020-02-24 02:26:35 -05:00
RCL_PixelInfo p =
2020-03-13 11:44:02 -04:00
RCL_mapToScreen(worldPosition,worldHeight,SFG_player.camera);
2020-02-24 02:26:35 -05:00
2020-03-13 11:44:02 -04:00
if (p.depth > 0 &&
SFG_spriteIsVisible(worldPosition,worldHeight,spriteSize))
2020-02-24 02:26:35 -05:00
SFG_drawScaledSprite(
sprite,
p.position.x * SFG_RAYCASTING_SUBSAMPLE,p.position.y,
2020-03-13 11:44:02 -04:00
RCL_perspectiveScaleVertical(SFG_SPRITE_SIZE_PIXELS(spriteSize),p.depth),
2020-02-25 11:03:40 -05:00
p.depth / (RCL_UNITS_PER_SQUARE * 2),p.depth);
2019-10-25 18:50:22 -04:00
}
2019-10-08 07:46:12 -04:00
}
2019-10-09 11:44:31 -04:00
2019-12-30 13:24:03 -05:00
// projectile sprites:
2019-10-21 13:58:11 -04:00
for (uint8_t i = 0; i < SFG_currentLevel.projectileRecordCount; ++i)
{
SFG_ProjectileRecord *proj = &(SFG_currentLevel.projectileRecords[i]);
2019-12-30 13:24:03 -05:00
if (proj->type == SFG_PROJECTILE_BULLET)
continue; // bullets aren't drawn
2019-10-21 13:58:11 -04:00
RCL_Vector2D worldPosition;
worldPosition.x = proj->position[0];
worldPosition.y = proj->position[1];
RCL_PixelInfo p =
RCL_mapToScreen(worldPosition,proj->position[2],SFG_player.camera);
2019-12-30 13:24:03 -05:00
const uint8_t *s = SFG_effectSprites[proj->type];
2019-10-21 14:07:28 -04:00
2020-02-27 10:13:19 -05:00
int16_t spriteSize = SFG_SPRITE_SIZE_PIXELS(0);
2019-10-21 13:58:11 -04:00
2019-12-30 13:24:03 -05:00
if (proj->type == SFG_PROJECTILE_EXPLOSION ||
proj->type == SFG_PROJECTILE_DUST)
2019-10-21 13:58:11 -04:00
{
2019-12-30 15:35:19 -05:00
int16_t doubleFramesToLive =
RCL_nonZero(SFG_GET_PROJECTILE_FRAMES_TO_LIVE(proj->type) / 2);
2020-02-27 10:13:19 -05:00
// grow the explosion/dust sprite as an animation
2019-10-21 13:58:11 -04:00
spriteSize =
2019-10-22 16:58:41 -04:00
(
2020-02-27 10:13:19 -05:00
SFG_SPRITE_SIZE_PIXELS(2) *
2019-10-22 16:58:41 -04:00
RCL_sinInt(
2019-12-30 15:35:19 -05:00
((doubleFramesToLive -
2019-10-22 16:58:41 -04:00
proj->doubleFramesToLive) * RCL_UNITS_PER_SQUARE / 4)
2019-12-30 15:35:19 -05:00
/ doubleFramesToLive)
2019-10-22 16:58:41 -04:00
) / RCL_UNITS_PER_SQUARE;
2019-10-21 13:58:11 -04:00
}
2020-03-13 11:44:02 -04:00
if (p.depth > 0 &&
SFG_spriteIsVisible(worldPosition,proj->position[2],spriteSize))
2019-10-21 13:58:11 -04:00
SFG_drawScaledSprite(s,
p.position.x * SFG_RAYCASTING_SUBSAMPLE,p.position.y,
2020-03-13 11:44:02 -04:00
RCL_perspectiveScaleVertical(spriteSize,p.depth),
2020-02-01 17:17:22 -05:00
SFG_fogValueDiminish(p.depth),
p.depth);
2019-10-21 13:58:11 -04:00
}
2019-10-09 11:44:31 -04:00
#if SFG_HEADBOB_ENABLED
2020-02-16 04:32:12 -05:00
// after rendering sprites substract back the head bob offset
2019-10-09 11:44:31 -04:00
SFG_player.camera.height -= headBobOffset;
#endif
2019-10-10 15:58:46 -04:00
2020-01-11 16:12:03 -05:00
#if SFG_PREVIEW_MODE == 0
2019-11-09 16:01:19 -05:00
SFG_drawWeapon(weaponBobOffset);
2020-01-11 16:12:03 -05:00
#endif
2019-11-09 16:01:19 -05:00
2020-02-22 03:23:11 -05:00
// draw HUD:
2019-10-23 18:09:46 -04:00
2019-11-09 16:01:19 -05:00
// bar
uint8_t color = 5;
for (uint16_t j = SFG_GAME_RESOLUTION_Y - SFG_HUD_BAR_HEIGHT;
j < SFG_GAME_RESOLUTION_Y; ++j)
{
for (uint16_t i = 0; i < SFG_GAME_RESOLUTION_X; ++i)
SFG_setGamePixel(i,j,color);
color = 2;
}
2020-02-08 14:53:10 -05:00
#define TEXT_Y (SFG_GAME_RESOLUTION_Y - SFG_HUD_MARGIN - \
SFG_FONT_CHARACTER_SIZE * SFG_FONT_SIZE_MEDIUM)
2020-01-12 10:48:42 -05:00
SFG_drawNumber( // health
2019-10-23 18:09:46 -04:00
SFG_player.health,
SFG_HUD_MARGIN,
2020-02-08 14:53:10 -05:00
TEXT_Y,
2019-10-23 18:09:46 -04:00
SFG_FONT_SIZE_MEDIUM,
2019-11-09 16:01:19 -05:00
SFG_player.health > SFG_PLAYER_HEALTH_WARNING_LEVEL ? 4 : 175);
2019-10-23 18:09:46 -04:00
2020-01-12 10:48:42 -05:00
SFG_drawNumber( // ammo
2020-01-13 12:59:36 -05:00
SFG_player.ammo[SFG_weaponAmmo(SFG_player.weapon)],
2019-10-23 18:09:46 -04:00
SFG_GAME_RESOLUTION_X - SFG_HUD_MARGIN -
2020-02-11 15:24:50 -05:00
(SFG_FONT_CHARACTER_SIZE + 1) * SFG_FONT_SIZE_MEDIUM * 3,
2020-02-08 14:53:10 -05:00
TEXT_Y,
2019-10-23 18:09:46 -04:00
SFG_FONT_SIZE_MEDIUM,
2020-02-08 14:53:10 -05:00
4);
for (uint8_t i = 0; i < 3; ++i) // access cards
if (SFG_player.cards & (1 << i))
2020-02-22 03:23:11 -05:00
SFG_fillRectangle(
SFG_HUD_MARGIN + (SFG_FONT_CHARACTER_SIZE + 1) *
SFG_FONT_SIZE_MEDIUM * (5 + i),
TEXT_Y,
SFG_FONT_SIZE_MEDIUM * SFG_FONT_CHARACTER_SIZE,
SFG_FONT_SIZE_MEDIUM * SFG_FONT_CHARACTER_SIZE,
i == 0 ? 93 : (i == 1 ? 124 : 60));
2020-02-08 14:53:10 -05:00
#undef TEXT_Y
2019-10-25 16:41:52 -04:00
2020-01-12 10:48:42 -05:00
// border indicator
2020-02-16 04:39:06 -05:00
if ((SFG_game.frame - SFG_player.lastHurtFrame
<= SFG_HUD_BORDER_INDICATOR_DURATION_FRAMES) ||
(SFG_game.state == SFG_GAME_STATE_LOSE))
2020-01-12 10:48:42 -05:00
SFG_drawIndicationBorder(SFG_HUD_BORDER_INDICATOR_WIDTH_PIXELS,
SFG_HUD_HURT_INDICATION_COLOR);
2020-02-10 12:10:06 -05:00
else if (SFG_game.frame - SFG_player.lastItemTakenFrame
2020-01-12 10:48:42 -05:00
<= SFG_HUD_BORDER_INDICATOR_DURATION_FRAMES)
SFG_drawIndicationBorder(SFG_HUD_BORDER_INDICATOR_WIDTH_PIXELS,
SFG_HUD_ITEM_TAKEN_INDICATION_COLOR);
2020-02-21 15:48:34 -05:00
if (SFG_game.state == SFG_GAME_STATE_WIN)
SFG_drawWinOverlay();
2019-10-08 07:46:12 -04:00
}
2019-10-06 11:03:15 -04:00
}
2019-09-25 09:51:19 -04:00
void SFG_mainLoopBody()
{
/* standard deterministic game loop, independed on actuall achieved FPS,
each game logic (physics) frame is performed with the SFG_MS_PER_FRAME
delta time. */
uint32_t timeNow = SFG_getTimeMs();
2020-02-10 12:10:06 -05:00
uint32_t timeNextFrame = SFG_game.lastFrameTimeMs + SFG_MS_PER_FRAME;
2019-09-25 09:51:19 -04:00
2020-02-10 12:10:06 -05:00
SFG_game.frameTime = timeNow;
2019-10-06 20:07:10 -04:00
2019-10-03 13:31:25 -04:00
if (timeNow >= timeNextFrame)
2019-09-25 09:51:19 -04:00
{
2020-02-10 12:10:06 -05:00
uint32_t timeSinceLastFrame = timeNow - SFG_game.lastFrameTimeMs;
2019-10-03 13:31:25 -04:00
uint8_t steps = 0;
2019-09-25 09:51:19 -04:00
// perform game logic (physics), for each frame
while (timeSinceLastFrame >= SFG_MS_PER_FRAME)
{
SFG_gameStep();
timeSinceLastFrame -= SFG_MS_PER_FRAME;
2020-02-10 12:10:06 -05:00
SFG_game.frame++;
2019-10-03 13:31:25 -04:00
steps++;
2019-09-25 09:51:19 -04:00
}
2020-03-28 12:34:09 -04:00
if ((steps > 1) && (SFG_game.antiSpam == 0))
{
2019-10-03 13:31:25 -04:00
SFG_LOG("Failed to reach target FPS! Consider setting a lower value.")
2020-03-28 12:34:09 -04:00
SFG_game.antiSpam = 30;
}
if (SFG_game.antiSpam > 0)
SFG_game.antiSpam--;
2019-10-03 13:31:25 -04:00
2019-09-25 09:51:19 -04:00
// render noly once
2019-10-06 11:03:15 -04:00
SFG_draw();
2019-09-25 09:51:19 -04:00
2020-02-10 12:10:06 -05:00
SFG_game.lastFrameTimeMs = timeNow;
2019-09-25 09:51:19 -04:00
}
2019-09-29 07:50:40 -04:00
else
2019-10-03 13:31:25 -04:00
{
SFG_sleepMs((timeNextFrame - timeNow) / 2); // wait, relieve CPU
}
2019-09-25 09:51:19 -04:00
}