mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-06-15 20:01:54 -07:00
e23dbb9f99
by @Svaarich
1102 lines
40 KiB
C
1102 lines
40 KiB
C
#include <furi.h>
|
||
#include <gui/gui.h>
|
||
#include <input/input.h>
|
||
#include <stdlib.h>
|
||
#include <math.h>
|
||
#include <sys/time.h>
|
||
#include "sound.h"
|
||
#include "display.h"
|
||
#include "compiled/assets_icons.h"
|
||
#include "constants.h"
|
||
#include "entities.h"
|
||
#include "types.h"
|
||
#include "level.h"
|
||
#include <notification/notification.h>
|
||
#include <notification/notification_messages.h>
|
||
|
||
#define SOUND
|
||
|
||
// Useful macros
|
||
#define swap(a, b) \
|
||
do { \
|
||
typeof(a) temp = a; \
|
||
a = b; \
|
||
b = temp; \
|
||
} while(0)
|
||
#define sign(a, b) (double)(a > b ? 1 : (b > a ? -1 : 0))
|
||
#define pgm_read_byte(addr) (*(const unsigned char*)(addr))
|
||
|
||
typedef enum {
|
||
EventTypeTick,
|
||
EventTypeKey,
|
||
} EventType;
|
||
|
||
typedef struct {
|
||
EventType type;
|
||
InputEvent input;
|
||
} PluginEvent;
|
||
|
||
typedef struct {
|
||
Player player;
|
||
Entity entity[MAX_ENTITIES];
|
||
StaticEntity static_entity[MAX_STATIC_ENTITIES];
|
||
uint8_t num_entities;
|
||
uint8_t num_static_entities;
|
||
|
||
uint8_t scene;
|
||
uint8_t gun_pos;
|
||
double jogging;
|
||
double view_height;
|
||
bool init;
|
||
|
||
bool up;
|
||
bool down;
|
||
bool left;
|
||
bool right;
|
||
bool fired;
|
||
bool gun_fired;
|
||
|
||
double rot_speed;
|
||
double old_dir_x;
|
||
double old_plane_x;
|
||
NotificationApp* notify;
|
||
#ifdef SOUND
|
||
MusicPlayer* music_instance;
|
||
bool intro_sound;
|
||
#endif
|
||
} PluginState;
|
||
|
||
static const NotificationSequence sequence_short_sound = {
|
||
&message_note_c5,
|
||
&message_delay_50,
|
||
&message_sound_off,
|
||
NULL,
|
||
};
|
||
static const NotificationSequence sequence_long_sound = {
|
||
&message_note_c3,
|
||
&message_delay_100,
|
||
&message_sound_off,
|
||
NULL,
|
||
};
|
||
|
||
Coords translateIntoView(Coords* pos, PluginState* const plugin_state);
|
||
void updateHud(Canvas* const canvas, PluginState* const plugin_state);
|
||
// general
|
||
|
||
bool invert_screen = false;
|
||
uint8_t flash_screen = 0;
|
||
|
||
// game
|
||
// player and entities
|
||
|
||
uint8_t getBlockAt(const uint8_t level[], uint8_t x, uint8_t y) {
|
||
if(x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT) {
|
||
return E_FLOOR;
|
||
}
|
||
|
||
// y is read in inverse order
|
||
return pgm_read_byte(level + (((LEVEL_HEIGHT - 1 - y) * LEVEL_WIDTH + x) / 2)) >>
|
||
(!(x % 2) * 4) // displace part of wanted bits
|
||
& 0b1111; // mask wanted bits
|
||
}
|
||
|
||
// Finds the player in the map
|
||
void initializeLevel(const uint8_t level[], PluginState* const plugin_state) {
|
||
for(uint8_t y = LEVEL_HEIGHT - 1; y > 0; y--) {
|
||
for(uint8_t x = 0; x < LEVEL_WIDTH; x++) {
|
||
uint8_t block = getBlockAt(level, x, y);
|
||
|
||
if(block == E_PLAYER) {
|
||
plugin_state->player = create_player(x, y);
|
||
return;
|
||
}
|
||
|
||
// todo create other static entities
|
||
}
|
||
}
|
||
}
|
||
|
||
bool isSpawned(UID uid, PluginState* const plugin_state) {
|
||
for(uint8_t i = 0; i < plugin_state->num_entities; i++) {
|
||
if(plugin_state->entity[i].uid == uid) return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
bool isStatic(UID uid, PluginState* const plugin_state) {
|
||
for(uint8_t i = 0; i < plugin_state->num_static_entities; i++) {
|
||
if(plugin_state->static_entity[i].uid == uid) return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
void spawnEntity(uint8_t type, uint8_t x, uint8_t y, PluginState* const plugin_state) {
|
||
// Limit the number of spawned entities
|
||
if(plugin_state->num_entities >= MAX_ENTITIES) {
|
||
return;
|
||
}
|
||
|
||
// todo: read static entity status
|
||
|
||
switch(type) {
|
||
case E_ENEMY:
|
||
plugin_state->entity[plugin_state->num_entities] = create_enemy(x, y);
|
||
plugin_state->num_entities++;
|
||
break;
|
||
|
||
case E_KEY:
|
||
plugin_state->entity[plugin_state->num_entities] = create_key(x, y);
|
||
plugin_state->num_entities++;
|
||
break;
|
||
|
||
case E_MEDIKIT:
|
||
plugin_state->entity[plugin_state->num_entities] = create_medikit(x, y);
|
||
plugin_state->num_entities++;
|
||
break;
|
||
}
|
||
}
|
||
|
||
void spawnFireball(double x, double y, PluginState* const plugin_state) {
|
||
// Limit the number of spawned entities
|
||
if(plugin_state->num_entities >= MAX_ENTITIES) {
|
||
return;
|
||
}
|
||
|
||
UID uid = create_uid(E_FIREBALL, x, y);
|
||
// Remove if already exists, don't throw anything. Not the best, but shouldn't happen too often
|
||
if(isSpawned(uid, plugin_state)) return;
|
||
|
||
// Calculate direction. 32 angles
|
||
int16_t dir =
|
||
FIREBALL_ANGLES + atan2(y - plugin_state->player.pos.y, x - plugin_state->player.pos.x) /
|
||
(double)PI * FIREBALL_ANGLES;
|
||
if(dir < 0) dir += FIREBALL_ANGLES * 2;
|
||
plugin_state->entity[plugin_state->num_entities] = create_fireball(x, y, dir);
|
||
plugin_state->num_entities++;
|
||
}
|
||
|
||
void removeEntity(UID uid, PluginState* const plugin_state) {
|
||
uint8_t i = 0;
|
||
bool found = false;
|
||
|
||
while(i < plugin_state->num_entities) {
|
||
if(!found && plugin_state->entity[i].uid == uid) {
|
||
// todo: doze it
|
||
found = true;
|
||
plugin_state->num_entities--;
|
||
}
|
||
|
||
// displace entities
|
||
if(found) {
|
||
plugin_state->entity[i] = plugin_state->entity[i + 1];
|
||
}
|
||
|
||
i++;
|
||
}
|
||
}
|
||
|
||
void removeStaticEntity(UID uid, PluginState* const plugin_state) {
|
||
uint8_t i = 0;
|
||
bool found = false;
|
||
|
||
while(i < plugin_state->num_static_entities) {
|
||
if(!found && plugin_state->static_entity[i].uid == uid) {
|
||
found = true;
|
||
plugin_state->num_static_entities--;
|
||
}
|
||
|
||
// displace entities
|
||
if(found) {
|
||
plugin_state->static_entity[i] = plugin_state->static_entity[i + 1];
|
||
}
|
||
|
||
i++;
|
||
}
|
||
}
|
||
|
||
UID detectCollision(
|
||
const uint8_t level[],
|
||
Coords* pos,
|
||
double relative_x,
|
||
double relative_y,
|
||
bool only_walls,
|
||
PluginState* const plugin_state) {
|
||
// Wall collision
|
||
uint8_t round_x = (int)pos->x + (int)relative_x;
|
||
uint8_t round_y = (int)pos->y + (int)relative_y;
|
||
uint8_t block = getBlockAt(level, round_x, round_y);
|
||
|
||
if(block == E_WALL) {
|
||
//playSound(hit_wall_snd, HIT_WALL_SND_LEN);
|
||
return create_uid(block, round_x, round_y);
|
||
}
|
||
|
||
if(only_walls) {
|
||
return UID_null;
|
||
}
|
||
|
||
// Entity collision
|
||
for(uint8_t i = 0; i < plugin_state->num_entities; i++) {
|
||
// Don't collide with itself
|
||
if(&(plugin_state->entity[i].pos) == pos) {
|
||
continue;
|
||
}
|
||
|
||
uint8_t type = uid_get_type(plugin_state->entity[i].uid);
|
||
|
||
// Only ALIVE enemy collision
|
||
if(type != E_ENEMY || plugin_state->entity[i].state == S_DEAD ||
|
||
plugin_state->entity[i].state == S_HIDDEN) {
|
||
continue;
|
||
}
|
||
|
||
Coords new_coords = {
|
||
plugin_state->entity[i].pos.x - relative_x,
|
||
plugin_state->entity[i].pos.y - relative_y};
|
||
uint8_t distance = coords_distance(pos, &new_coords);
|
||
|
||
// Check distance and if it's getting closer
|
||
if(distance < ENEMY_COLLIDER_DIST && distance < plugin_state->entity[i].distance) {
|
||
return plugin_state->entity[i].uid;
|
||
}
|
||
}
|
||
|
||
return UID_null;
|
||
}
|
||
|
||
// Shoot
|
||
void fire(PluginState* const plugin_state) {
|
||
//playSound(shoot_snd, SHOOT_SND_LEN);
|
||
|
||
for(uint8_t i = 0; i < plugin_state->num_entities; i++) {
|
||
// Shoot only ALIVE enemies
|
||
if(uid_get_type(plugin_state->entity[i].uid) != E_ENEMY ||
|
||
plugin_state->entity[i].state == S_DEAD || plugin_state->entity[i].state == S_HIDDEN) {
|
||
continue;
|
||
}
|
||
|
||
Coords transform = translateIntoView(&(plugin_state->entity[i].pos), plugin_state);
|
||
if(fabs(transform.x) < 20 && transform.y > 0) {
|
||
uint8_t damage = (double)fmin(
|
||
GUN_MAX_DAMAGE,
|
||
GUN_MAX_DAMAGE / (fabs(transform.x) * plugin_state->entity[i].distance) / 5);
|
||
if(damage > 0) {
|
||
plugin_state->entity[i].health = fmax(0, plugin_state->entity[i].health - damage);
|
||
plugin_state->entity[i].state = S_HIT;
|
||
plugin_state->entity[i].timer = 4;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
UID updatePosition(
|
||
const uint8_t level[],
|
||
Coords* pos,
|
||
double relative_x,
|
||
double relative_y,
|
||
bool only_walls,
|
||
PluginState* const plugin_state) {
|
||
UID collide_x = detectCollision(level, pos, relative_x, 0, only_walls, plugin_state);
|
||
UID collide_y = detectCollision(level, pos, 0, relative_y, only_walls, plugin_state);
|
||
|
||
if(!collide_x) pos->x += relative_x;
|
||
if(!collide_y) pos->y += relative_y;
|
||
|
||
return collide_x || collide_y || UID_null;
|
||
}
|
||
|
||
void updateEntities(const uint8_t level[], Canvas* const canvas, PluginState* const plugin_state) {
|
||
uint8_t i = 0;
|
||
while(i < plugin_state->num_entities) {
|
||
// update distance
|
||
plugin_state->entity[i].distance =
|
||
coords_distance(&(plugin_state->player.pos), &(plugin_state->entity[i].pos));
|
||
|
||
// Run the timer. Works with actual frames.
|
||
// Todo: use delta here. But needs double type and more memory
|
||
if(plugin_state->entity[i].timer > 0) plugin_state->entity[i].timer--;
|
||
|
||
// too far away. put it in doze mode
|
||
if(plugin_state->entity[i].distance > MAX_ENTITY_DISTANCE) {
|
||
removeEntity(plugin_state->entity[i].uid, plugin_state);
|
||
// don't increase 'i', since current one has been removed
|
||
continue;
|
||
}
|
||
|
||
// bypass render if hidden
|
||
if(plugin_state->entity[i].state == S_HIDDEN) {
|
||
i++;
|
||
continue;
|
||
}
|
||
|
||
uint8_t type = uid_get_type(plugin_state->entity[i].uid);
|
||
|
||
switch(type) {
|
||
case E_ENEMY: {
|
||
// Enemy "IA"
|
||
if(plugin_state->entity[i].health == 0) {
|
||
if(plugin_state->entity[i].state != S_DEAD) {
|
||
plugin_state->entity[i].state = S_DEAD;
|
||
plugin_state->entity[i].timer = 6;
|
||
}
|
||
} else if(plugin_state->entity[i].state == S_HIT) {
|
||
if(plugin_state->entity[i].timer == 0) {
|
||
// Back to alert state
|
||
plugin_state->entity[i].state = S_ALERT;
|
||
plugin_state->entity[i].timer = 40; // delay next fireball thrown
|
||
}
|
||
} else if(plugin_state->entity[i].state == S_FIRING) {
|
||
if(plugin_state->entity[i].timer == 0) {
|
||
// Back to alert state
|
||
plugin_state->entity[i].state = S_ALERT;
|
||
plugin_state->entity[i].timer = 40; // delay next fireball throwm
|
||
}
|
||
} else {
|
||
// ALERT STATE
|
||
if(plugin_state->entity[i].distance > ENEMY_MELEE_DIST &&
|
||
plugin_state->entity[i].distance < MAX_ENEMY_VIEW) {
|
||
if(plugin_state->entity[i].state != S_ALERT) {
|
||
plugin_state->entity[i].state = S_ALERT;
|
||
plugin_state->entity[i].timer = 20; // used to throw fireballs
|
||
} else {
|
||
if(plugin_state->entity[i].timer == 0) {
|
||
// Throw a fireball
|
||
spawnFireball(
|
||
plugin_state->entity[i].pos.x,
|
||
plugin_state->entity[i].pos.y,
|
||
plugin_state);
|
||
plugin_state->entity[i].state = S_FIRING;
|
||
plugin_state->entity[i].timer = 6;
|
||
} else {
|
||
// move towards to the player.
|
||
updatePosition(
|
||
level,
|
||
&(plugin_state->entity[i].pos),
|
||
sign(plugin_state->player.pos.x, plugin_state->entity[i].pos.x) *
|
||
(double)ENEMY_SPEED * 1, // NOT SURE (delta)
|
||
sign(plugin_state->player.pos.y, plugin_state->entity[i].pos.y) *
|
||
(double)ENEMY_SPEED * 1, // NOT SURE (delta)
|
||
true,
|
||
plugin_state);
|
||
}
|
||
}
|
||
} else if(plugin_state->entity[i].distance <= ENEMY_MELEE_DIST) {
|
||
if(plugin_state->entity[i].state != S_MELEE) {
|
||
// Preparing the melee attack
|
||
plugin_state->entity[i].state = S_MELEE;
|
||
plugin_state->entity[i].timer = 10;
|
||
} else if(plugin_state->entity[i].timer == 0) {
|
||
// Melee attack
|
||
plugin_state->player.health =
|
||
fmax(0, plugin_state->player.health - ENEMY_MELEE_DAMAGE);
|
||
plugin_state->entity[i].timer = 14;
|
||
flash_screen = 1;
|
||
updateHud(canvas, plugin_state);
|
||
}
|
||
} else {
|
||
// stand
|
||
plugin_state->entity[i].state = S_STAND;
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
|
||
case E_FIREBALL: {
|
||
if(plugin_state->entity[i].distance < FIREBALL_COLLIDER_DIST) {
|
||
// Hit the player and disappear
|
||
plugin_state->player.health =
|
||
fmax(0, plugin_state->player.health - ENEMY_FIREBALL_DAMAGE);
|
||
flash_screen = 1;
|
||
updateHud(canvas, plugin_state);
|
||
removeEntity(plugin_state->entity[i].uid, plugin_state);
|
||
continue; // continue in the loop
|
||
} else {
|
||
// Move. Only collide with walls.
|
||
// Note: using health to store the angle of the movement
|
||
UID collided = updatePosition(
|
||
level,
|
||
&(plugin_state->entity[i].pos),
|
||
cos((double)plugin_state->entity[i].health / FIREBALL_ANGLES * (double)PI) *
|
||
(double)FIREBALL_SPEED,
|
||
sin((double)plugin_state->entity[i].health / FIREBALL_ANGLES * (double)PI) *
|
||
(double)FIREBALL_SPEED,
|
||
true,
|
||
plugin_state);
|
||
|
||
if(collided) {
|
||
removeEntity(plugin_state->entity[i].uid, plugin_state);
|
||
continue; // continue in the entity check loop
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
|
||
case E_MEDIKIT: {
|
||
if(plugin_state->entity[i].distance < ITEM_COLLIDER_DIST) {
|
||
// pickup
|
||
notification_message(plugin_state->notify, &sequence_long_sound);
|
||
//playSound(medkit_snd, MEDKIT_SND_LEN);
|
||
plugin_state->entity[i].state = S_HIDDEN;
|
||
plugin_state->player.health = fmin(100, plugin_state->player.health + 50);
|
||
updateHud(canvas, plugin_state);
|
||
flash_screen = 1;
|
||
}
|
||
break;
|
||
}
|
||
|
||
case E_KEY: {
|
||
if(plugin_state->entity[i].distance < ITEM_COLLIDER_DIST) {
|
||
// pickup
|
||
notification_message(plugin_state->notify, &sequence_long_sound);
|
||
//playSound(get_key_snd, GET_KEY_SND_LEN);
|
||
plugin_state->entity[i].state = S_HIDDEN;
|
||
plugin_state->player.keys++;
|
||
updateHud(canvas, plugin_state);
|
||
flash_screen = 1;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
|
||
i++;
|
||
}
|
||
}
|
||
|
||
// The map raycaster. Based on https://lodev.org/cgtutor/raycasting.html
|
||
void renderMap(
|
||
const uint8_t level[],
|
||
double view_height,
|
||
Canvas* const canvas,
|
||
PluginState* const plugin_state) {
|
||
UID last_uid = 0; // NOT SURE ?
|
||
|
||
for(uint8_t x = 0; x < SCREEN_WIDTH; x += RES_DIVIDER) {
|
||
double camera_x = 2 * (double)x / SCREEN_WIDTH - 1;
|
||
double ray_x = plugin_state->player.dir.x + plugin_state->player.plane.x * camera_x;
|
||
double ray_y = plugin_state->player.dir.y + plugin_state->player.plane.y * camera_x;
|
||
uint8_t map_x = (uint8_t)plugin_state->player.pos.x;
|
||
uint8_t map_y = (uint8_t)plugin_state->player.pos.y;
|
||
Coords map_coords = {plugin_state->player.pos.x, plugin_state->player.pos.y};
|
||
double delta_x = fabs(1 / ray_x);
|
||
double delta_y = fabs(1 / ray_y);
|
||
|
||
int8_t step_x;
|
||
int8_t step_y;
|
||
double side_x;
|
||
double side_y;
|
||
|
||
if(ray_x < 0) {
|
||
step_x = -1;
|
||
side_x = (plugin_state->player.pos.x - map_x) * delta_x;
|
||
} else {
|
||
step_x = 1;
|
||
side_x = (map_x + (double)1.0 - plugin_state->player.pos.x) * delta_x;
|
||
}
|
||
|
||
if(ray_y < 0) {
|
||
step_y = -1;
|
||
side_y = (plugin_state->player.pos.y - map_y) * delta_y;
|
||
} else {
|
||
step_y = 1;
|
||
side_y = (map_y + (double)1.0 - plugin_state->player.pos.y) * delta_y;
|
||
}
|
||
|
||
// Wall detection
|
||
uint8_t depth = 0;
|
||
bool hit = 0;
|
||
bool side;
|
||
while(!hit && depth < MAX_RENDER_DEPTH) {
|
||
if(side_x < side_y) {
|
||
side_x += delta_x;
|
||
map_x += step_x;
|
||
side = 0;
|
||
} else {
|
||
side_y += delta_y;
|
||
map_y += step_y;
|
||
side = 1;
|
||
}
|
||
|
||
uint8_t block = getBlockAt(level, map_x, map_y);
|
||
|
||
if(block == E_WALL) {
|
||
hit = 1;
|
||
} else {
|
||
// Spawning entities here, as soon they are visible for the
|
||
// player. Not the best place, but would be a very performance
|
||
// cost scan for them in another loop
|
||
if(block == E_ENEMY || (block & 0b00001000) /* all collectable items */) {
|
||
// Check that it's close to the player
|
||
if(coords_distance(&(plugin_state->player.pos), &map_coords) <
|
||
MAX_ENTITY_DISTANCE) {
|
||
UID uid = create_uid(block, map_x, map_y);
|
||
if(last_uid != uid && !isSpawned(uid, plugin_state)) {
|
||
spawnEntity(block, map_x, map_y, plugin_state);
|
||
last_uid = uid;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
depth++;
|
||
}
|
||
|
||
if(hit) {
|
||
double distance;
|
||
|
||
if(side == 0) {
|
||
distance =
|
||
fmax(1, (map_x - plugin_state->player.pos.x + (1 - step_x) / 2) / ray_x);
|
||
} else {
|
||
distance =
|
||
fmax(1, (map_y - plugin_state->player.pos.y + (1 - step_y) / 2) / ray_y);
|
||
}
|
||
|
||
// store zbuffer value for the column
|
||
zbuffer[x / Z_RES_DIVIDER] = fmin(distance * DISTANCE_MULTIPLIER, 255);
|
||
|
||
// rendered line height
|
||
uint8_t line_height = RENDER_HEIGHT / distance;
|
||
|
||
drawVLine(
|
||
x,
|
||
view_height / distance - line_height / 2 + RENDER_HEIGHT / 2,
|
||
view_height / distance + line_height / 2 + RENDER_HEIGHT / 2,
|
||
GRADIENT_COUNT - (int)distance / MAX_RENDER_DEPTH * GRADIENT_COUNT - side * 2,
|
||
canvas);
|
||
}
|
||
}
|
||
}
|
||
|
||
// Sort entities from far to close
|
||
uint8_t sortEntities(PluginState* const plugin_state) {
|
||
uint8_t gap = plugin_state->num_entities;
|
||
bool swapped = false;
|
||
while(gap > 1 || swapped) {
|
||
//shrink factor 1.3
|
||
gap = (gap * 10) / 13;
|
||
if(gap == 9 || gap == 10) gap = 11;
|
||
if(gap < 1) gap = 1;
|
||
swapped = false;
|
||
for(uint8_t i = |