Merge pull request #1246 from tsoding/back-platform-secret

Introduce Phantom Platforms
master
Alexey Kutepov 2020-01-26 04:08:58 +07:00 committed by GitHub
commit 05c585b85f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 116 additions and 5 deletions

View File

@ -73,6 +73,8 @@ add_executable(nothing
src/game/level/lava/wavy_rect.c
src/game/level/platforms.h
src/game/level/platforms.c
src/game/level/phantom_platforms.h
src/game/level/phantom_platforms.c
src/game/level/player.h
src/game/level/player.c
src/game/level/explosion.h

View File

@ -18,10 +18,7 @@ platform_0 -1307.807495 -5332.081543 519.166626 4358.668457 80926d
2
lava_0 182.002197 589.132202 4415.973633 688.541870 d42b2b
lava_0 -8050.391602 730.803101 4415.973633 688.541870 d42b2b
3
back_platform_0 -185.010040 442.315765 519.166626 221.875031 80926d
back_platform_0 1640.383789 420.668457 519.166626 221.875031 80926d
back_platform_0 -1307.840332 -1029.460205 519.166626 221.875031 80926d
0
17
box_0 120.543991 -72.326294 112.500031 107.291695 9aa034
box_1 714.640747 516.840698 112.500031 107.291695 9aa034
@ -42,3 +39,7 @@ box_1 -609.278137 -667.919678 112.500031 107.291695 9aa034
box_0 -3923.780518 452.429810 112.500031 107.291695 a05034
0
0
3
back_platform_0 -185.010040 442.315765 519.166626 221.875031 80926d
back_platform_0 1640.383789 420.668457 519.166626 221.875031 80926d
back_platform_0 -1307.840332 -1029.460205 519.166626 221.875031 80926d

View File

@ -49,3 +49,4 @@
#include "src/dynarray.c"
#include "src/system/file.c"
#include "src/ring_buffer.c"
#include "src/game/level/phantom_platforms.c"

View File

@ -10,6 +10,7 @@
#include "game/level/labels.h"
#include "game/level/lava.h"
#include "game/level/platforms.h"
#include "game/level/phantom_platforms.h"
#include "game/level/player.h"
#include "game/level/regions.h"
#include "game/level/rigid_bodies.h"
@ -48,6 +49,7 @@ struct Level
Boxes *boxes;
Labels *labels;
Regions *regions;
Phantom_Platforms pp;
};
Level *create_level_from_level_editor(const LevelEditor *level_editor)
@ -143,12 +145,15 @@ Level *create_level_from_level_editor(const LevelEditor *level_editor)
RETURN_LT(lt, NULL);
}
level->pp = create_phantom_platforms(level_editor->pp_layer);
return level;
}
void destroy_level(Level *level)
{
trace_assert(level);
destroy_phantom_platforms(level->pp);
RETURN_LT0(level->lt);
}
@ -164,6 +169,8 @@ int level_render(const Level *level, const Camera *camera)
return -1;
}
phantom_platforms_render(&level->pp, camera);
if (player_render(level->player, camera) < 0) {
return -1;
}
@ -220,6 +227,10 @@ int level_update(Level *level, float delta_time)
lava_update(level->lava, delta_time);
labels_update(level->labels, delta_time);
Rect hitbox = player_hitbox(level->player);
phantom_platforms_hide_at(&level->pp, vec(hitbox.x, hitbox.y));
phantom_platforms_update(&level->pp, delta_time);
return 0;
}

View File

@ -50,6 +50,7 @@ LevelEditor *create_level_editor(Memory *memory, Cursor *cursor)
level_editor->regions_layer = create_rect_layer(memory, "region", cursor);
level_editor->goals_layer = create_point_layer(memory, "goal");
level_editor->label_layer = create_label_layer(memory, "label");
level_editor->pp_layer = create_rect_layer(memory, "pp", cursor);
level_editor->layers[LAYER_PICKER_BOXES] = rect_layer_as_layer(level_editor->boxes_layer);
level_editor->layers[LAYER_PICKER_PLATFORMS] = rect_layer_as_layer(level_editor->platforms_layer);
@ -60,6 +61,7 @@ LevelEditor *create_level_editor(Memory *memory, Cursor *cursor)
level_editor->layers[LAYER_PICKER_REGIONS] = rect_layer_as_layer(level_editor->regions_layer);
level_editor->layers[LAYER_PICKER_BACKGROUND] = background_layer_as_layer(&level_editor->background_layer);
level_editor->layers[LAYER_PICKER_LABELS] = label_layer_as_layer(level_editor->label_layer);
level_editor->layers[LAYER_PICKER_PP] = rect_layer_as_layer(level_editor->pp_layer);
level_editor->notice = (FadingWigglyText) {
@ -111,6 +113,7 @@ LevelEditor *create_level_editor_from_file(Memory *memory, Cursor *cursor, const
rect_layer_load(level_editor->boxes_layer, memory, &input);
label_layer_load(level_editor->label_layer, memory, &input);
rect_layer_load(level_editor->regions_layer, memory, &input);
rect_layer_load(level_editor->pp_layer, memory, &input);
undo_history_clean(level_editor->undo_history);
return level_editor;
@ -363,7 +366,8 @@ static LayerPicker level_format_layer_order[LAYER_PICKER_N] = {
LAYER_PICKER_BACK_PLATFORMS,
LAYER_PICKER_BOXES,
LAYER_PICKER_LABELS,
LAYER_PICKER_REGIONS
LAYER_PICKER_REGIONS,
LAYER_PICKER_PP
};
/* TODO(#904): LevelEditor does not check that the saved level file is modified by external program */

View File

@ -36,6 +36,7 @@ struct LevelEditor
RectLayer *regions_layer;
BackgroundLayer background_layer;
LabelLayer *label_layer;
RectLayer *pp_layer;
LayerPtr layers[LAYER_PICKER_N];

View File

@ -25,6 +25,7 @@ static const Color LAYER_CELL_BACKGROUND_COLORS[LAYER_PICKER_N] = {
{0.2f, 1.0f, 0.6f, 1.0f}, // LAYER_PICKER_BOXES
{0.2f, 0.6f, 1.0f, 1.0f}, // LAYER_PICKER_LABELS
{0.2f, 1.0f, 0.6f, 1.0f}, // LAYER_PICKER_REGIONS
{0.2f, 1.0f, 0.6f, 1.0f}, // LAYER_PICKER_PP
};
static const char *LAYER_CELL_TITLES[LAYER_PICKER_N] = {
@ -37,6 +38,7 @@ static const char *LAYER_CELL_TITLES[LAYER_PICKER_N] = {
"Boxes", // LAYER_PICKER_BOXES
"Labels", // LAYER_PICKER_LABELS
"Regions", // LAYER_PICKER_REGIONS
"Phantom Platforms", // LAYER_PICKER_PP
};
inline static float layer_picker_max_width(void)

View File

@ -15,6 +15,7 @@ typedef enum {
LAYER_PICKER_BOXES,
LAYER_PICKER_LABELS,
LAYER_PICKER_REGIONS,
LAYER_PICKER_PP,
LAYER_PICKER_N
} LayerPicker;

View File

@ -0,0 +1,65 @@
#include "phantom_platforms.h"
Phantom_Platforms create_phantom_platforms(RectLayer *rect_layer)
{
Phantom_Platforms pp;
pp.size = rect_layer->rects.count;
pp.rects = malloc(sizeof(pp.rects[0]) * pp.size);
memcpy(pp.rects, rect_layer->rects.data, sizeof(pp.rects[0]) * pp.size);
pp.colors = malloc(sizeof(pp.colors[0]) * pp.size);
memcpy(pp.colors, rect_layer->colors.data, sizeof(pp.colors[0]) * pp.size);
pp.hiding = calloc(1, sizeof(pp.hiding[0]) * pp.size);
return pp;
}
void destroy_phantom_platforms(Phantom_Platforms pp)
{
free(pp.rects);
free(pp.colors);
free(pp.hiding);
}
void phantom_platforms_render(const Phantom_Platforms *pp, const Camera *camera)
{
trace_assert(pp);
trace_assert(camera);
for (size_t i = 0; i < pp->size; ++i) {
camera_fill_rect(camera, pp->rects[i], pp->colors[i]);
}
}
#define HIDING_SPEED 4.0f
// TODO(#1247): phantom_platforms_update is O(N) even when nothing is animated
void phantom_platforms_update(Phantom_Platforms *pp, float dt)
{
trace_assert(pp);
for (size_t i = 0; i < pp->size; ++i) {
if (pp->hiding[i]) {
if (pp->colors[i].a > 0.0f) {
pp->colors[i].a =
fmaxf(0.0f, pp->colors[i].a - HIDING_SPEED * dt);
} else {
pp->hiding[i] = 0;
}
}
}
}
// TODO(#1248): phantom_platforms_hide_at is O(N)
void phantom_platforms_hide_at(Phantom_Platforms *pp, Vec2f position)
{
trace_assert(pp);
for (size_t i = 0; i < pp->size; ++i) {
if (rect_contains_point(pp->rects[i], position)) {
pp->hiding[i] = 1;
}
}
}

View File

@ -0,0 +1,23 @@
#ifndef PHANTOM_PLATFORMS_H_
#define PHANTOM_PLATFORMS_H_
#include <stdlib.h>
#include "math/rect.h"
#include "color.h"
#include "game/level/level_editor/rect_layer.h"
typedef struct {
size_t size;
Rect *rects;
Color *colors;
int *hiding;
} Phantom_Platforms;
Phantom_Platforms create_phantom_platforms(RectLayer *rect_layer);
void destroy_phantom_platforms(Phantom_Platforms pp);
void phantom_platforms_render(const Phantom_Platforms *pp, const Camera *camera);
void phantom_platforms_update(Phantom_Platforms *pp, float dt);
void phantom_platforms_hide_at(Phantom_Platforms *pp, Vec2f position);
#endif // PHANTOM_PLATFORMS_H_