mirror of https://git.sr.ht/~kikoodx/momento
Level selection menu
This commit is contained in:
parent
b8a0ace053
commit
6056b7d22d
|
@ -26,6 +26,7 @@ include_directories(include generated)
|
|||
|
||||
set(SOURCES
|
||||
src/main.c
|
||||
src/util.c
|
||||
src/level/load.c
|
||||
src/level/draw.c
|
||||
src/level/get_tile.c
|
||||
|
@ -66,6 +67,7 @@ set(ASSETS
|
|||
assets/graphics/exit-active-particle.png
|
||||
assets/graphics/exit-unlock-particle.png
|
||||
assets/graphics/font.png
|
||||
assets/graphics/font_big.png
|
||||
assets/graphics/player/blink.png
|
||||
assets/graphics/player/idle.png
|
||||
assets/graphics/player/jump.png
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
|
@ -30,3 +30,9 @@ font.png:
|
|||
grid.size: 13x16
|
||||
proportional: true
|
||||
name: font_main
|
||||
font_big.png:
|
||||
type: font
|
||||
charset: print
|
||||
grid.size: 27x32
|
||||
proportional: true
|
||||
name: font_big
|
||||
|
|
|
@ -2,10 +2,24 @@
|
|||
/* Copyright (C) 2021 KikooDX */
|
||||
#pragma once
|
||||
|
||||
#include "conf.h"
|
||||
#include "input.h"
|
||||
#include <gint/display.h>
|
||||
|
||||
#define PACKBOX_PADDING_Y 16
|
||||
#define PACKBOX_WIDTH (DWIDTH / 2 - PACKBOX_PADDING_Y)
|
||||
#define PACKBOX_PADDING_X (PACKBOX_WIDTH / 3)
|
||||
#define PACKBOX_SPACING \
|
||||
(PACKBOX_WIDTH - PACKBOX_PADDING_X + (DWIDTH - PACKBOX_WIDTH) / 2)
|
||||
#define PACKBOX_BORDER_UNS 2
|
||||
#define PACKBOX_BORDER_SEL 4
|
||||
#define PACKBOX_TRANSITION_SPEED (1.0 / 40.0)
|
||||
|
||||
struct LevelSelection {
|
||||
int pack_cursor;
|
||||
int visual_cursor;
|
||||
float transition;
|
||||
float transition_speed;
|
||||
};
|
||||
|
||||
struct LevelSelection levelselection_init(void);
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/* Copyright (C) 2021 KikooDX */
|
||||
#pragma once
|
||||
|
||||
/* collection of handy "math" functions */
|
||||
float square(float x);
|
||||
float isquare(float x);
|
||||
float abs(float x);
|
||||
int sign(float x);
|
|
@ -2,12 +2,52 @@
|
|||
/* Copyright (C) 2021 KikooDX */
|
||||
|
||||
#include "conf.h"
|
||||
#include "filepaths.h"
|
||||
#include "levelselection.h"
|
||||
#include "util.h"
|
||||
#include "zxcolors.h"
|
||||
#include <gint/display.h>
|
||||
|
||||
extern const font_t font_main;
|
||||
extern const font_t font_big;
|
||||
|
||||
static void packboxes_draw(int pack_id, int off_x);
|
||||
static void packbox_draw(int x, int pack_id, int selected);
|
||||
|
||||
void
|
||||
levelselection_draw(struct LevelSelection levelselection)
|
||||
{
|
||||
dprint(0, 0, ZX_WHITE, ".pack_cursor=%d", levelselection.pack_cursor);
|
||||
const float smoothed_transition =
|
||||
square(abs(levelselection.transition)) *
|
||||
sign(levelselection.transition);
|
||||
const int off_x = smoothed_transition * PACKBOX_SPACING;
|
||||
packboxes_draw(levelselection.pack_cursor, off_x);
|
||||
}
|
||||
|
||||
static void
|
||||
packboxes_draw(int pack_id, int off_x)
|
||||
{
|
||||
packbox_draw(off_x + PACKBOX_PADDING_X - PACKBOX_WIDTH, pack_id - 1, 0);
|
||||
packbox_draw(off_x + (DWIDTH - PACKBOX_WIDTH) / 2, pack_id, 1);
|
||||
packbox_draw(off_x + DWIDTH - PACKBOX_PADDING_X, pack_id + 1, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
packbox_draw(int x, int pack_id, int selected)
|
||||
{
|
||||
if (pack_id < 0)
|
||||
return;
|
||||
if (pack_id >= PACK_COUNT)
|
||||
return;
|
||||
|
||||
drect_border(x, PACKBOX_PADDING_Y, x + PACKBOX_WIDTH,
|
||||
DHEIGHT - PACKBOX_PADDING_Y, ZX_BLACK,
|
||||
selected ? PACKBOX_BORDER_SEL : PACKBOX_BORDER_UNS,
|
||||
ZX_WHITE);
|
||||
|
||||
/* draw level number with big font */
|
||||
dfont(&font_big);
|
||||
dprint_opt(x + PACKBOX_WIDTH / 2, DHEIGHT / 2, ZX_WHITE, ZX_BLACK,
|
||||
DTEXT_CENTER, DTEXT_MIDDLE, "%d", pack_id);
|
||||
dfont(&font_main);
|
||||
}
|
||||
|
|
|
@ -6,5 +6,10 @@
|
|||
struct LevelSelection
|
||||
levelselection_init(void)
|
||||
{
|
||||
return (struct LevelSelection){.pack_cursor = 0};
|
||||
return (struct LevelSelection){
|
||||
.pack_cursor = 0,
|
||||
.visual_cursor = 0,
|
||||
.transition = 0.0,
|
||||
.transition_speed = 0.0,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -4,23 +4,55 @@
|
|||
#include "filepaths.h"
|
||||
#include "input.h"
|
||||
#include "levelselection.h"
|
||||
#include "util.h"
|
||||
#include <gint/keyboard.h>
|
||||
|
||||
/* Return 1 if game state should change. */
|
||||
int
|
||||
levelselection_update(struct LevelSelection *levelselection, struct Input input)
|
||||
{
|
||||
/* decrease selected pack id */
|
||||
if (input.keystates[K_LEFT] == KS_PRESS &&
|
||||
levelselection->pack_cursor > 0)
|
||||
levelselection->pack_cursor -= 1;
|
||||
/* increase selected pack id */
|
||||
if (input.keystates[K_RIGHT] == KS_PRESS &&
|
||||
levelselection->pack_cursor < PACK_COUNT - 1)
|
||||
levelselection->pack_cursor += 1;
|
||||
const int sign_transition_speed =
|
||||
sign(levelselection->transition_speed);
|
||||
/* update transition state */
|
||||
if (levelselection->transition_speed != 0.0)
|
||||
levelselection->transition += levelselection->transition_speed;
|
||||
/* end transition if necessary */
|
||||
if (levelselection->transition * -sign_transition_speed <
|
||||
levelselection->transition_speed * -sign_transition_speed) {
|
||||
levelselection->visual_cursor = levelselection->pack_cursor;
|
||||
levelselection->transition = 0.0;
|
||||
levelselection->transition_speed = 0.0;
|
||||
}
|
||||
|
||||
if (levelselection->transition == 0.0) {
|
||||
/* decrease selected pack id */
|
||||
if (input.keystates[K_LEFT] &&
|
||||
levelselection->pack_cursor > 0) {
|
||||
levelselection->visual_cursor =
|
||||
levelselection->pack_cursor;
|
||||
levelselection->pack_cursor -= 1;
|
||||
levelselection->transition = -1.0;
|
||||
levelselection->transition_speed =
|
||||
PACKBOX_TRANSITION_SPEED;
|
||||
}
|
||||
/* increase selected pack id */
|
||||
if (input.keystates[K_RIGHT] &&
|
||||
levelselection->pack_cursor < PACK_COUNT - 1) {
|
||||
levelselection->visual_cursor =
|
||||
levelselection->pack_cursor;
|
||||
levelselection->pack_cursor += 1;
|
||||
levelselection->transition = 1.0;
|
||||
levelselection->transition_speed =
|
||||
-PACKBOX_TRANSITION_SPEED;
|
||||
}
|
||||
}
|
||||
/* confirm */
|
||||
if (input.keystates[K_A] == KS_PRESS)
|
||||
if (input.keystates[K_A] == KS_PRESS) {
|
||||
levelselection->visual_cursor = levelselection->pack_cursor;
|
||||
levelselection->transition = 0.0;
|
||||
levelselection->transition_speed = 0.0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include "particles.h"
|
||||
#include "player.h"
|
||||
#include "tiles.h"
|
||||
#include "util.h"
|
||||
#include <gint/keyboard.h>
|
||||
|
||||
static void player_move(struct Player *player, int x, int y);
|
||||
|
@ -161,8 +162,8 @@ player_update(struct Player *player, struct Input input)
|
|||
static void
|
||||
player_move(struct Player *player, int x, int y)
|
||||
{
|
||||
const int sign_x = 1 * (x > 0) - 1 * (x < 0);
|
||||
const int sign_y = 1 * (y > 0) - 1 * (y < 0);
|
||||
const int sign_x = sign(x);
|
||||
const int sign_y = sign(y);
|
||||
player->x += x;
|
||||
player->y += y;
|
||||
if (player_collide_solid(player->x, player->y)) {
|
||||
|
|
|
@ -2,12 +2,10 @@
|
|||
/* Copyright (C) 2021 KikooDX */
|
||||
|
||||
#include "transition.h"
|
||||
#include "util.h"
|
||||
#include "zxcolors.h"
|
||||
#include <gint/display.h>
|
||||
|
||||
static float square(float x);
|
||||
static float isquare(float x);
|
||||
|
||||
void
|
||||
hfade_in(float step, color_t color)
|
||||
{
|
||||
|
@ -51,15 +49,3 @@ vfade_out(float step, color_t color)
|
|||
dhline(y, color);
|
||||
}
|
||||
}
|
||||
|
||||
static float
|
||||
square(float x)
|
||||
{
|
||||
return x * x;
|
||||
}
|
||||
|
||||
static float
|
||||
isquare(float x)
|
||||
{
|
||||
return 1.0 - square(1.0 - x);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/* Copyright (C) 2021 KikooDX */
|
||||
|
||||
#include "util.h"
|
||||
|
||||
float
|
||||
square(float x)
|
||||
{
|
||||
return x * x;
|
||||
}
|
||||
|
||||
float
|
||||
isquare(float x)
|
||||
{
|
||||
return 1.0 - square(1.0 - x);
|
||||
}
|
||||
|
||||
float
|
||||
abs(float x)
|
||||
{
|
||||
return (x > 0) ? x : -x;
|
||||
}
|
||||
|
||||
int
|
||||
sign(float x)
|
||||
{
|
||||
return 1 * (x > 0) - 1 * (x < 0);
|
||||
}
|
Loading…
Reference in New Issue