- [NEW] Consola in-game

This commit is contained in:
2024-07-03 11:38:39 +02:00
parent 06d58b090d
commit c7186119c2
7 changed files with 371 additions and 99 deletions

View File

@@ -1260,6 +1260,31 @@ namespace actor
for (int i=0; i<100; ++i) boosters_collected[i] = false;
}
const int getBoosterFromString(char *booster)
{
static const char *boostset_name[3] = {"RUN", "GOD", "JUMP"};
for (int i=0;i<3;++i)
{
if (strcmp(booster, boostset_name[i])==0)
{
return 1<<i;
}
}
return 0;
}
bool giveBooster(char *booster)
{
const int value = getBoosterFromString(booster);
switch (value)
{
case BOOST_GOD: boost_god = 99*2; break;
case BOOST_RUN: boost_steps = 99*2; break;
case BOOST_JUMP: boost_jumps = 10; break;
}
return value!=0;
}
void collectBooster(int booster, int id)
{
boosters_collected[id] = true;
@@ -1309,7 +1334,7 @@ namespace actor
const int getSkillFromString(char *skill)
{
static const char *skillset_name[4] = {"SHOES", "GLOBES", "PANTS", "BAG"};
static const char *skillset_name[4] = {"SHOES", "GLOVES", "PANTS", "BAG"};
for (int i=0;i<4;++i)
{
if (strcmp(skill, skillset_name[i])==0)
@@ -1320,9 +1345,18 @@ namespace actor
return 0;
}
void giveSkill(char *skill)
bool giveSkill(char *skill)
{
skills |= getSkillFromString(skill);
const int value = getSkillFromString(skill);
skills |= value;
return value!=0;
}
bool dropSkill(char *skill)
{
const int value = getSkillFromString(skill);
skills &= ~value;
return value!=0;
}
bool wasSkillCollected(char *skill)
@@ -1338,7 +1372,7 @@ namespace actor
const int getPartFromString(char *part)
{
static const char *partset_name[6] = {"FILTER", "PUMP", "TIMER", "SALT", "PIPE", "ELBOW"};
for (int i=0;i<4;++i)
for (int i=0;i<6;++i)
{
if (strcmp(part, partset_name[i])==0)
{
@@ -1348,9 +1382,18 @@ namespace actor
return PART_NONE;
}
void pickPart(char *part)
bool pickPart(char *part)
{
parts |= getPartFromString(part);
const int value = getPartFromString(part);
parts |= value;
return value!=0;
}
bool dropPart(char *part)
{
const int value = getPartFromString(part);
parts &= ~value;
return value!=0;
}
bool wasPartCollected(char *part)
@@ -1362,5 +1405,14 @@ namespace actor
{
return parts;
}
void move(int *x, int *y, int *z)
{
actor_t *hero = actor::find("HERO");
if (x) hero->pos.x = *x;
if (y) hero->pos.y = *y;
if (z) hero->pos.z = *z;
}
}
}

View File

@@ -167,6 +167,7 @@ namespace actor
namespace hero
{
void init();
bool giveBooster(char *booster);
void collectBooster(int booster, int id);
bool wasBoosterCollected(int id);
int getBoostGod();
@@ -176,12 +177,16 @@ namespace actor
void useBoostRun();
void useBoostJump();
void giveSkill(char *skill);
bool giveSkill(char *skill);
bool dropSkill(char *skill);
bool wasSkillCollected(char *skill);
int getSkills();
void pickPart(char *part);
bool pickPart(char *part);
bool dropPart(char *part);
bool wasPartCollected(char *part);
int getParts();
void move(int *x, int *y, int *z);
}
}

248
source/console.cpp Normal file
View File

@@ -0,0 +1,248 @@
#include "console.h"
#include <SDL2/SDL.h>
#include "jdraw.h"
#include "jinput.h"
#include "room.h"
#include "actor.h"
#include "jgame.h"
namespace console
{
bool enabled = false;
bool initialized = false;
char msg[80];
char cmd[80];
int cmd_size = 0;
uint8_t cursor_blinker = 0;
void execute_command();
void init()
{
strcpy(msg, "PoolOS v0.1 ready.");
cmd[0] = '\0';
cmd_size = 0;
initialized = true;
}
void toggle()
{
if (!initialized) return;
enabled = !enabled;
cmd[0] = '\0';
cmd_size = 0;
}
const bool isEnabled()
{
return enabled;
}
const char scancode_to_ascii(uint8_t scancode)
{
if (scancode == SDL_SCANCODE_SPACE) return ' ';
if (scancode == SDL_SCANCODE_0) return '0';
if (scancode >= SDL_SCANCODE_1 && scancode <= SDL_SCANCODE_9) return scancode+19;
if (scancode >= SDL_SCANCODE_A && scancode <= SDL_SCANCODE_Z) return scancode+61;
if (scancode == SDL_SCANCODE_MINUS) return '-';
if (scancode == SDL_SCANCODE_EQUALS) return '=';
if (scancode == SDL_SCANCODE_LEFTBRACKET) return '[';
if (scancode == SDL_SCANCODE_RIGHTBRACKET) return ']';
if (scancode == SDL_SCANCODE_BACKSLASH) return '\\';
if (scancode == SDL_SCANCODE_SEMICOLON) return ';';
if (scancode == SDL_SCANCODE_APOSTROPHE) return '<';
if (scancode == SDL_SCANCODE_COMMA) return ',';
if (scancode == SDL_SCANCODE_PERIOD) return '.';
if (scancode == SDL_SCANCODE_SLASH) return '/';
return '\0';
}
const bool update()
{
if (!initialized || !enabled) return false;
const uint8_t scancode = input::getKeyPressed();
if (scancode != SDL_SCANCODE_UNKNOWN) {
cursor_blinker = 0;
if (scancode == SDL_SCANCODE_BACKSPACE) {
if (cmd_size>0) cmd[--cmd_size] = '\0';
} else if (scancode == SDL_SCANCODE_UNKNOWN) {
// do nothing
} else if (scancode == SDL_SCANCODE_RETURN) {
execute_command();
} else {
char ascii = scancode_to_ascii(scancode);
if (ascii != '\0')
{
cmd[cmd_size++] = ascii;
cmd[cmd_size] = '\0';
}
}
}
cursor_blinker=(cursor_blinker+1)&0x07;
return true;
}
void draw()
{
if (!initialized || !enabled) return;
draw::color(BLACK);
draw::fillrect(1, 0, 318, 14);
draw::color(GREEN);
draw::rect(1, -1, 318, 15);
draw::print(msg, 3, 1, WHITE, 0);
draw::print(">", 3, 7, WHITE+LIGHT, 0);
draw::print(cmd, 7, 7, WHITE+LIGHT, 0);
if (cursor_blinker<4)
{
const int size = strlen(cmd);
draw::print("_", 7+size*4, 7, WHITE+LIGHT, 0);
}
}
char *tokens[4];
void tokenize()
{
int token = 0;
for (int i=0; i<4; ++i) tokens[i]=nullptr;
char *p = cmd;
char *fin = cmd;
while (true)
{
while (*fin!='\0' && *fin!=' ') fin++;
tokens[token++] = p;
if (*fin=='\0') return;
*fin = '\0';
fin++; p = fin;
}
}
#define CMD_UNKNOWN -1
#define CMD_GIVE 0
#define CMD_DROP 1
#define CMD_GOTO 2
#define CMD_MOVE 3
#define CMD_EXIT 4
#define NUM_CMDS 5
const char *command_text[NUM_CMDS] = { "GIVE", "DROP", "GOTO", "MOVE", "EXIT" };
#define PARAM_UNKNOWN -1
#define PARAM_RUN 0
#define PARAM_GOD 1
#define PARAM_JUMP 2
#define NUM_PARAMS 3
const char *param_text[NUM_PARAMS] = { "RUN", "GOD", "JUMP" };
const int getCommand()
{
for (int i=0; i<NUM_CMDS; ++i)
{
if (strcmp(tokens[0], command_text[i])==0) return i;
}
return -1;
}
const int getParam(int index)
{
for (int i=0; i<NUM_PARAMS; ++i)
{
if (strcmp(tokens[index], param_text[i])==0) return i;
}
return -1;
}
const int getNumber(int index)
{
return SDL_atoi(tokens[index]);
}
void execute_command()
{
tokenize();
int command = getCommand();
switch (command)
{
case CMD_UNKNOWN:
strcpy(msg, "ERROR: Command not found.");
break;
case CMD_GIVE:
if (!tokens[1]) {
strcpy(msg, "ERROR: Nothing to give.");
} else {
strcpy(msg, "Ok.");
if (!actor::hero::giveBooster(tokens[1]))
if (!actor::hero::giveSkill(tokens[1]))
if (!actor::hero::pickPart(tokens[1]))
strcpy(msg, "ERROR: Cannot give that.");
}
break;
case CMD_DROP:
if (!tokens[1]) {
strcpy(msg, "ERROR: Nothing to drop.");
} else {
strcpy(msg, "Ok.");
if (!actor::hero::giveSkill(tokens[1]))
if (!actor::hero::pickPart(tokens[1]))
strcpy(msg, "ERROR: Cannot drop that.");
}
break;
case CMD_GOTO:
if (!tokens[1]) {
strcpy(msg, "ERROR: Nowhere to go.");
} else {
room::load(getNumber(1));
strcpy(msg, "Ok.");
}
break;
case CMD_MOVE:
if (!tokens[1]) {
strcpy(msg, "ERROR: Nowhere to move.");
} else {
switch (tokens[1][0])
{
case 'X':
if (!tokens[2]) {
strcpy(msg, "ERROR: Nowhere to move.");
} else {
int x = SDL_atoi(tokens[2]);
actor::hero::move(&x, nullptr, nullptr);
}
break;
case 'Y':
if (!tokens[2]) {
strcpy(msg, "ERROR: Nowhere to move.");
} else {
int y = SDL_atoi(tokens[2]);
actor::hero::move(nullptr, &y, nullptr);
}
break;
case 'Z':
if (!tokens[2]) {
strcpy(msg, "ERROR: Nowhere to move.");
} else {
int z = SDL_atoi(tokens[2]);
actor::hero::move(nullptr, nullptr, &z);
}
break;
default:
strcpy(msg, "ERROR: Unknown axis.");
}
}
break;
case CMD_EXIT:
game::exit();
break;
}
cmd_size=0;
cmd[cmd_size] = '\0';
}
}

10
source/console.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
namespace console
{
void init();
void toggle();
const bool isEnabled();
const bool update();
void draw();
}

View File

@@ -7,6 +7,7 @@ namespace game
{
static int param_count = 0;
static char **params = nullptr;
static bool should_exit = false;
static unsigned int ticks_per_frame = 1000/60;
@@ -15,6 +16,11 @@ namespace game
ticks_per_frame = ticks;
}
void exit()
{
should_exit = true;
}
const char* getParams(const int index)
{
if (index<param_count) return params[index];
@@ -32,13 +38,12 @@ int main(int argc, char *argv[])
static unsigned int current_ticks = SDL_GetTicks();
bool should_exit=false;
SDL_Event e;
while (!should_exit)
while (!game::should_exit)
{
while(SDL_PollEvent(&e))
{
if (e.type==SDL_QUIT) { should_exit = true; break; }
if (e.type==SDL_QUIT) { game::should_exit = true; break; }
if (e.type==SDL_KEYDOWN)
{
input::updateKey(e.key.keysym.scancode);
@@ -59,7 +64,7 @@ int main(int argc, char *argv[])
if (SDL_GetTicks()-current_ticks >= game::ticks_per_frame)
{
if (!game::loop()) should_exit = true;
if (!game::loop()) game::should_exit = true;
input::updateKey(SDL_SCANCODE_UNKNOWN);
input::updateKeypressed(SDL_SCANCODE_UNKNOWN);
input::updateClk(0);

View File

@@ -8,5 +8,7 @@ namespace game
bool loop();
void exit();
const char* getParams(const int index);
}

View File

@@ -8,6 +8,7 @@
#include "editor.h"
#include <vector>
#include <string>
#include "console.h"
enum sections { SECTION_GENERAL, SECTION_ROOM, SECTION_ACTOR };
@@ -31,23 +32,7 @@ int treeview_scroll = 0;
void restart()
{
actor::hero::init();
room::load(1); //room_w, room_h, room_xp, room_xn, room_yp, room_yn, room_color, room_floor, room_walls, room_doors, room_walldoors);
/*
box = actor::create("ASCENSOR",{16,32,0}, {8,8,4}, {64,0,32,24}, {0,24});
box->flags = FLAG_MOVING;
box->movement = MOV_Z;
box->mov_push = PUSH_ZP;
actor::setDirty(box, true);
*/
//box = actor::create("HERO", {16,32,8}, {8,8,12}, {0,32,20,32}, {-6,38});
//box->flags = FLAG_HERO | FLAG_PUSHABLE | FLAG_GRAVITY | FLAG_ORIENTABLE | FLAG_ANIMATED;
//actor::setDirty(box, true);
//actor::reorder();
room::load(1);
}
void game::init()
@@ -56,8 +41,10 @@ void game::init()
if (editor::isDevMode())
draw::init("The Pool", 520, 240, 3);
else
else {
draw::init("The Pool", 320, 240, 3);
console::init();
}
//room::init();
surf = draw::getSurface("test.gif");
@@ -82,56 +69,29 @@ void game::init()
restart();
/*
actor::actor_t *box = actor::create("BOX", {32,32,16}, {8,8,8}, "test.gif", {32,0,32,32}, {0,32});
box->flags = FLAG_PUSHABLE | FLAG_GRAVITY;
box->movement = MOV_CW;
box->mov_push = PUSH_XN;
actor::setDirty(box, true);
box = actor::create("BOX2", {32,32,0}, {8,8,8}, "test.gif", {32,0,32,32}, {0,32});
box->flags = FLAG_PUSHABLE | FLAG_GRAVITY;
box->movement = MOV_CW;
box->mov_push = PUSH_XN;
actor::setDirty(box, true);
*/
actor::select(actor::find("BOX"));
}
int sx=1, sy=0;
void print(int x, int y, int num)
{
int digits=0;
bool sign = num < 0;
num = SDL_abs(num);
int n = num;
while (n>0) {n=n/10;digits++;}
if (sign) digits++;
x=x+digits*4;
if (num==0) draw::draw(x+4,y,5,7,0,120);
while (num>0)
{
draw::draw(x,y,5,7,(num%10)*5,120);
num=num/10;
x=x-4;
}
if (sign) draw::draw(x,y,5,7,50,120);
}
/*const bool btn(const char* label, const int x, const int y, int &var, int min, int max)
void print(int x, int y, int num)
{
char buffer[100];
int result=0;
draw::print(label, x, y+3, 15, 0);
result=ui::button(SDL_itoa(var, buffer, 10), x+50, y, 28, 11, TEAL, LIGHT+TEAL, LIGHT+PURPLE);
if (result)
int digits=0;
bool sign = num < 0;
num = SDL_abs(num);
int n = num;
while (n>0) {n=n/10;digits++;}
if (sign) digits++;
x=x+digits*4;
if (num==0) draw::draw(x+4,y,5,7,0,120);
while (num>0)
{
var=SDL_max(min, SDL_min(max, var-(result-2)));
return true;
draw::draw(x,y,5,7,(num%10)*5,120);
num=num/10;
x=x-4;
}
return false;
}*/
if (sign) draw::draw(x,y,5,7,50,120);
}
const bool btn_small(const int x, const int y, int &var, int min, int max)
{
@@ -159,26 +119,6 @@ const bool btn_small(const int x, const int y, int &var, int min, int max, int w
return false;
}
/*
void btn_check(const int x, const int y, const char* label, uint8_t &flags, const uint8_t value)
{
int result=0;
if (flags & value) {
result=ui::check(label, x, y, 12, 11, true);
} else {
result=ui::check(label, x, y, 12, 11, false);
}
if (result)
{
if (flags & value) {
flags = flags & ~value;
} else {
flags = flags | value;
}
}
}
*/
const bool btn_check(const int x, const int y, const char* label, int &flags, const uint16_t value, const int width=24)
{
int result=0;
@@ -349,8 +289,14 @@ void editor_move_selected()
bool game::loop()
{
if (input::keyDown(SDL_SCANCODE_ESCAPE)) return false;
if (input::keyDown(SDL_SCANCODE_ESCAPE))
{
if (console::isEnabled())
console::toggle();
else
return false;
}
if (input::keyPressed(SDL_SCANCODE_TAB) || input::keyPressed(SDL_SCANCODE_GRAVE) ) console::toggle();
// WHILE EDITING...
if (editor::isEditing())
@@ -360,18 +306,21 @@ bool game::loop()
}
else
{
actor::update(actor::getFirst());
actor::hero::useBoostGod();
if (!console::update())
{
actor::update(actor::getFirst());
actor::hero::useBoostGod();
}
}
actor::reorder();
if (editor::isEditing()) editor_select_by_keyboard();
draw::resetViewport();
draw::cls(2);
if (editor::isDevMode()) draw::setViewport(100,0,320,240);
actor::reorder();
if (editor::isEditing()) editor_select_by_keyboard();
room::draw();
actor::draw(actor::getFirst());
room::draw2();
@@ -407,6 +356,7 @@ bool game::loop()
*/
if (!editor::isDevMode())
{
console::draw();
draw::render();
return true;
}