reading input mappings via Lua, related refactoring

This commit is contained in:
devZoGok
2022-02-04 20:58:11 +02:00
parent caab51fc69
commit 2ab8b583e2
15 changed files with 252 additions and 329 deletions
+36
View File
@@ -0,0 +1,36 @@
mappings = {
{bind = 0, trigger = 0, action = true, bindType = 1, inOptions = false},
{bind = 1, trigger = 87, action = true, bindType = 0, inOptions = false},
{bind = 2, trigger = 83, action = true, bindType = 0, inOptions = false},
{bind = 3, trigger = 263, action = true, bindType = 0, inOptions = false},
{bind = 4, trigger = 262, action = true, bindType = 0, inOptions = false},
{bind = 5, trigger = 259, action = true, bindType = 0, inOptions = false},
{bind = 6, trigger = 256, action = true, bindType = 0, inOptions = true},
{bind = 7, trigger = 312, action = false, bindType = 2, inOptions = false},
{bind = 8, trigger = 313, action = false, bindType = 2, inOptions = false},
{bind = 9, trigger = 310, action = false, bindType = 2, inOptions = false},
{bind = 10, trigger = 311, action = false, bindType = 2, inOptions = false},
{bind = 11, trigger = 1, action = true, bindType = 1, inOptions = false},
{bind = 12, trigger = 0, action = true, bindType = 1, inOptions = true},
{bind = 13, trigger = 1, action = true, bindType = 1, inOptions = true},
{bind = 14, trigger = 72, action = true, bindType = 0, inOptions = true},
{bind = 15, trigger = 0, action = true, bindType = 0, inOptions = true},
{bind = 16, trigger = 0, action = true, bindType = 0, inOptions = true},
{bind = 17, trigger = 341, action = true, bindType = 0, inOptions = true},
{bind = 18, trigger = 340, action = true, bindType = 0, inOptions = true},
{bind = 19, trigger = 80, action = true, bindType = 0, inOptions = true},
{bind = 20, trigger = 67, action = true, bindType = 0, inOptions = true},
{bind = 21, trigger = 83, action = true, bindType = 0, inOptions = true},
{bind = 22, trigger = 48, action = true, bindType = 0, inOptions = true},
{bind = 23, trigger = 49, action = true, bindType = 0, inOptions = true},
{bind = 24, trigger = 50, action = true, bindType = 0, inOptions = true},
{bind = 25, trigger = 51, action = true, bindType = 0, inOptions = true},
{bind = 26, trigger = 52, action = true, bindType = 0, inOptions = true},
{bind = 27, trigger = 53, action = true, bindType = 0, inOptions = true},
{bind = 28, trigger = 54, action = true, bindType = 0, inOptions = true},
{bind = 29, trigger = 55, action = true, bindType = 0, inOptions = true},
{bind = 30, trigger = 56, action = true, bindType = 0, inOptions = true},
{bind = 31, trigger = 57, action = true, bindType = 0, inOptions = true},
}
@@ -1,7 +1,7 @@
--dofile("defConfigs.lua")
numUnits = 11
UnitType = {VESSEL = 0, DESTROYER = 1, CRUISER = 2, AIRCRAFT_CARRIER = 3, SUBMARINE = 4, MISSILE_JET = 5, DEMO_JET = 6};
unitType = {
UnitType.VESSEL, UnitType.VESSEL,
UnitType.DESTROYER, UnitType.DESTROYER,
@@ -10,6 +10,7 @@ unitType = {
UnitType.SUBMARINE,
UnitType.MISSILE_JET, UnitType.DEMO_JET
};
health = {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500}
speed = {.025, .025, .05, .05, .1, .1, .1, .1, .1, .1, .15}
destinationOffset = {.75, .75, 1, 1, .1, .1, .1, .1, .1, .5, .5}
@@ -261,7 +262,6 @@ meshPath = {
"submarine.vb",
"jet00.x", "jet01.x"
}
PATH = ""
basePath = {
PATH .. "Models/Battleships/", PATH .. "Models/Battleships/",
PATH .. "Models/Destroyers/", PATH .. "Models/Destroyers/",
+1 -6
View File
@@ -7,14 +7,13 @@ cmake_policy(SET CMP0015 NEW)
set(states activeGameState.cpp inGameAppState.cpp guiAppState.cpp)
set(util util.cpp binds.h)
set(gui tooltip.cpp optionsButton.cpp exitButton.cpp consoleCommand.cpp)
set(core gameManager.cpp luaManager.cpp main.cpp)
set(core gameManager.cpp main.cpp defConfigs.cpp)
set(ships vessel.cpp destroyer.cpp cruiser.cpp aircraftCarrier.cpp submarine.cpp)
set(projectiles projectile.cpp guidedMissile.cpp depthCharge.cpp missile.cpp shell.cpp torpedo.cpp)
set(aircraft jet.cpp demoJet.cpp missileJet.cpp)
set(fx explosion.cpp)
set(content player.cpp unitDataManager.cpp unit.cpp map.cpp ${ships} ${projectiles} ${aircraft} ${fx})
set(LUA_DIR external/lua)
set(SFML_DIR external/SFML)
set(VB01_DIR external/vb01)
set(FREETYPE_DIR ${VB01_DIR}/external/freetype)
@@ -54,10 +53,6 @@ set(GLFW_LIB_DIR build/${GLFW_DIR}/src)
include_directories(${GLFW_DIR}/include/GLFW)
target_link_directories(${GAME_NAME} PUBLIC ${GLFW_LIB_DIR})
set(LUA_LIB_DIR ${LUA_DIR}/testes/libs)
include_directories(${LUA_DIR})
target_link_libraries(${GAME_NAME} lua)
target_link_libraries(${GAME_NAME} vb01)
target_link_libraries(${GAME_NAME} vb01Gui)
target_link_libraries(${GAME_NAME} gameBase)
-7
View File
@@ -1,7 +0,0 @@
PATH = "/home/dominykas/c++/Battleship/Assets/"
DEFAULT_TEXTURE = PATH .. "Textures/defaultTexture.jpg"
numAppStates = 3
numMaxBinds = 23
numBinds = {12, 1, 23}
numConfBinds = {0, 1, 13}
+7 -27
View File
@@ -29,11 +29,15 @@ using namespace std;
namespace battleship{
using namespace configData;
using namespace gameBase;
const int size = 50;
ActiveGameState::ActiveGameState(GuiAppState *guiState, Map *map, vector<Player*> players, int playerId) {
type = AppStateType::ACTIVE_STATE;
ActiveGameState::ActiveGameState(GuiAppState *guiState, Map *map, vector<Player*> players, int playerId) : AbstractAppState(
AppStateType::ACTIVE_STATE,
configData::calcSumBinds(AppStateType::ACTIVE_STATE, true),
configData::calcSumBinds(AppStateType::ACTIVE_STATE, false),
PATH + "Scripts/options.lua"){
this->guiState = guiState;
this->map = map;
this->players = players;
@@ -62,31 +66,10 @@ namespace battleship{
}
void ActiveGameState::onAttached() {
readFile(PATH + "../options.cfg", bindingsLines, configData::numConfBinds[AppStateType::IN_GAME_STATE], configData::numConfBinds[AppStateType::ACTIVE_STATE]);
AbstractAppState::onAttached();
GameManager *gm = GameManager::getSingleton();
int faction = mainPlayer->getFaction(), width = gm->getWidth(), height = gm->getHeight(), size = 50;
string names[5] = {"Battleship", "Destroyer", "Cruiser", "Carrier", "Submarine"};
Bind binds[]{Bind::LOOK_UP, Bind::LOOK_DOWN, Bind::LOOK_LEFT, Bind::LOOK_RIGHT};
int triggers[]{Mapping::MOUSE_AXIS_UP, Mapping::MOUSE_AXIS_DOWN, Mapping::MOUSE_AXIS_LEFT, Mapping::MOUSE_AXIS_RIGHT};
int numData = sizeof(triggers) / sizeof(int);
for(int i = 0; i < numData; i++){
Mapping *m = new Mapping;
m->bind = binds[i];
m->trigger = triggers[i];
m->action = false;
m->type = Mapping::MOUSE_AXIS;
mappings.push_back(m);
}
}
void ActiveGameState::onDettached() {
AbstractAppState::onDettached();
void ActiveGameState::onDettached() { AbstractAppState::onDettached();
}
void ActiveGameState::update() {
@@ -365,9 +348,6 @@ namespace battleship{
break;
case Bind::LAUNCH:
selectingGuidedMissileTarget = isPressed;
break;
case Bind::INSTALL_AAM:
case Bind::INSTALL_AWM:
break;
case Bind::GROUP_0:
case Bind::GROUP_1:
+21 -24
View File
@@ -10,34 +10,31 @@ namespace battleship{
RIGHT,
DELETE_CHAR,
TOGGLE_MAIN_MENU,
LOOK_AROUND,
DRAG_BOX,
DESELECT,
LEFT_CONTROL,
SELECT_PATROL_POINTS,
HALT,
ZOOM_IN,
ZOOM_OUT,
LOOK_UP,
LOOK_DOWN,
LOOK_LEFT,
LOOK_RIGHT,
LEFT_SHIFT,
LAUNCH,
TOGGLE_SUB,
INSTALL_AWM,
INSTALL_AAM,
GROUP_0,
GROUP_1,
GROUP_2,
GROUP_3,
GROUP_4,
GROUP_5,
GROUP_6,
GROUP_7,
GROUP_8,
GROUP_9,
LAST_BIND
LOOK_AROUND,
DRAG_BOX,
DESELECT,
HALT,
ZOOM_IN,
ZOOM_OUT,
LEFT_CONTROL,
LEFT_SHIFT,
SELECT_PATROL_POINTS,
LAUNCH,
TOGGLE_SUB,
GROUP_0,
GROUP_1,
GROUP_2,
GROUP_3,
GROUP_4,
GROUP_5,
GROUP_6,
GROUP_7,
GROUP_8,
GROUP_9
};
}
+33
View File
@@ -0,0 +1,33 @@
#include "defConfigs.h"
namespace battleship{
namespace configData{
int calcSumStaticBinds(int id, bool calcPrev){
int numBinds = 0;
if(calcPrev)
for(int i = 0; i < id; i++)
numBinds += numStaticBinds[i];
else
numBinds = numStaticBinds[id];
return numBinds;
}
int calcSumConfBinds(int id, bool calcPrev){
int numBinds = 0;
if(calcPrev)
for(int i = 0; i < id; i++)
numBinds += numConfBinds[i];
else
numBinds = numConfBinds[id];
return numBinds;
}
int calcSumBinds(int id, bool calcPrev){
return calcSumStaticBinds(id, calcPrev) + calcSumConfBinds(id, calcPrev);
}
}
}
+130 -101
View File
@@ -9,110 +9,139 @@
#include "binds.h"
namespace battleship{
namespace configData{
using namespace gameBase;
namespace configData{
using namespace gameBase;
const std::string PATH = "/home/dominykas/c++/Battleship/Assets/";
const std::string DEFAULT_TEXTURE = PATH + "Textures/defaultTexture.jpg";
const double camPanSpeed = .1;
const std::string PATH = "/home/dominykas/c++/Battleship/Assets/";
const std::string DEFAULT_TEXTURE = PATH + "Textures/defaultTexture.jpg";
const double camPanSpeed = .1;
const static int numAppStates = 3, numMaxBinds = 23;
static const int numBinds[numAppStates]{12, 1, 23};
static const int numConfBinds[numAppStates]{0, 1, 13};
const static int numAppStates = 3;
const static int numStaticBinds[numAppStates]{6, 0, 5};
const static int numConfBinds[numAppStates]{0, 1, 20};
const static int maxStaticBinds = 6;
const static int maxConfBinds = 20;
const static Bind binds[numAppStates][numMaxBinds]{
{
Bind::LEFT_CLICK,
Bind::SCROLLING_UP,
Bind::SCROLLING_DOWN,
Bind::LEFT,
Bind::RIGHT,
Bind::DELETE_CHAR,
},
{
Bind::TOGGLE_MAIN_MENU
},
{
Bind::LOOK_AROUND,
Bind::DRAG_BOX,
Bind::DESELECT,
Bind::HALT,
Bind::ZOOM_IN,
Bind::ZOOM_OUT,
Bind::LEFT_CONTROL,
Bind::LEFT_SHIFT,
Bind::SELECT_PATROL_POINTS,
Bind::LAUNCH,
Bind::TOGGLE_SUB,
Bind::INSTALL_AAM,
Bind::INSTALL_AWM,
Bind::GROUP_0,
Bind::GROUP_1,
Bind::GROUP_2,
Bind::GROUP_3,
Bind::GROUP_4,
Bind::GROUP_5,
Bind::GROUP_6,
Bind::GROUP_7,
Bind::GROUP_8,
Bind::GROUP_9
}
};
const static int triggers[numAppStates][numMaxBinds]{
{
0,
3,
4,
GLFW_KEY_LEFT,
GLFW_KEY_RIGHT,
GLFW_KEY_BACKSPACE,
GLFW_KEY_CAPS_LOCK,
GLFW_KEY_LEFT_SHIFT,
GLFW_KEY_SPACE,
GLFW_KEY_KP_ADD,
GLFW_KEY_MINUS,
GLFW_KEY_3
},
{
GLFW_KEY_ESCAPE
},
{
1,
0,
1,
GLFW_KEY_H,
3,
4,
GLFW_KEY_LEFT_CONTROL,
GLFW_KEY_LEFT_SHIFT,
GLFW_KEY_P,
GLFW_KEY_C,
GLFW_KEY_S,
GLFW_KEY_A,
GLFW_KEY_W,
GLFW_KEY_0,
GLFW_KEY_1,
GLFW_KEY_2,
GLFW_KEY_3,
GLFW_KEY_4,
GLFW_KEY_5,
GLFW_KEY_6,
GLFW_KEY_7,
GLFW_KEY_8,
GLFW_KEY_9
}
};
const static bool isKey[numAppStates][numMaxBinds]{
{0,0,0,1,1,1,1,1,1,1,1,1},
{1},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,1,1,1,1,1,1,1,1,1,1,1}
};
const static bool isAnalog[numAppStates][numMaxBinds]{
{0,0,0,0,0,0,0,0,0,0,0,0},
{0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0}
};
}
const static Bind staticBinds[numAppStates][maxStaticBinds]{
{
Bind::LEFT_CLICK,
Bind::SCROLLING_UP,
Bind::SCROLLING_DOWN,
Bind::LEFT,
Bind::RIGHT,
Bind::DELETE_CHAR,
},
{},
{
Bind::LOOK_UP,
Bind::LOOK_DOWN,
Bind::LOOK_LEFT,
Bind::LOOK_RIGHT,
Bind::LOOK_AROUND,
}
};
const static Bind confBinds[numAppStates][maxConfBinds]{
{},
{
Bind::TOGGLE_MAIN_MENU
},
{
Bind::DRAG_BOX,
Bind::DESELECT,
Bind::HALT,
Bind::ZOOM_IN,
Bind::ZOOM_OUT,
Bind::LEFT_CONTROL,
Bind::LEFT_SHIFT,
Bind::SELECT_PATROL_POINTS,
Bind::LAUNCH,
Bind::TOGGLE_SUB,
Bind::GROUP_0,
Bind::GROUP_1,
Bind::GROUP_2,
Bind::GROUP_3,
Bind::GROUP_4,
Bind::GROUP_5,
Bind::GROUP_6,
Bind::GROUP_7,
Bind::GROUP_8,
Bind::GROUP_9
}
};
const static int staticTriggers[numAppStates][maxStaticBinds]{
{
0,
GLFW_KEY_W,
GLFW_KEY_S,
GLFW_KEY_LEFT,
GLFW_KEY_RIGHT,
GLFW_KEY_BACKSPACE
},
{},
{
Mapping::MOUSE_AXIS_UP,
Mapping::MOUSE_AXIS_DOWN,
Mapping::MOUSE_AXIS_LEFT,
Mapping::MOUSE_AXIS_RIGHT,
0
}
};
const static int confTriggers[numAppStates][maxConfBinds]{
{},
{
GLFW_KEY_ESCAPE
},
{
GLFW_KEY_LEFT,
GLFW_KEY_RIGHT,
GLFW_KEY_H,
3,
4,
GLFW_KEY_LEFT_CONTROL,
GLFW_KEY_LEFT_SHIFT,
GLFW_KEY_P,
GLFW_KEY_C,
GLFW_KEY_S,
GLFW_KEY_0,
GLFW_KEY_1,
GLFW_KEY_2,
GLFW_KEY_3,
GLFW_KEY_4,
GLFW_KEY_5,
GLFW_KEY_6,
GLFW_KEY_7,
GLFW_KEY_8,
GLFW_KEY_9
}
};
const static bool isStaticKey[numAppStates][maxStaticBinds]{
{0, 1, 1, 1, 1, 1},
{},
{0, 0, 0, 0, 1}
};
const static bool isConfKey[numAppStates][maxConfBinds]{
{},
{1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
const static bool isStaticAction[numAppStates][maxStaticBinds]{
{1, 1, 1, 1, 1, 1},
{},
{0, 0, 0, 0, 1}
};
const static bool isConfAction[numAppStates][maxConfBinds]{
{},
{1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
int calcSumStaticBinds(int, bool);
int calcSumConfBinds(int, bool);
int calcSumBinds(int, bool);
}
}
#endif
-1
View File
@@ -2,7 +2,6 @@
#ifndef GAME_MANAGER_H
#define GAME_MANAGER_H
#include "defConfigs.h"
#include "util.h"
#include <mapping.h>
+8 -31
View File
@@ -3,6 +3,7 @@
#include <util.h>
#include "util.h"
#include "defConfigs.h"
#include "gameManager.h"
#include "guiAppState.h"
@@ -12,8 +13,13 @@ using namespace vb01Gui;
using namespace std;
namespace battleship{
GuiAppState::GuiAppState() {
type = AppStateType::GUI_STATE;
using namespace configData;
GuiAppState::GuiAppState() : AbstractAppState(
AppStateType::GUI_STATE,
configData::calcSumBinds(AppStateType::GUI_STATE, true),
configData::calcSumBinds(AppStateType::GUI_STATE, false),
PATH + "Scripts/options.lua"){
}
GuiAppState::~GuiAppState() {}
@@ -70,35 +76,6 @@ namespace battleship{
void GuiAppState::onAttached() {
AbstractAppState::onAttached();
Mapping *leftClick = new Mapping;
leftClick->bind = Bind::LEFT_CLICK;
leftClick->type = Mapping::MOUSE_KEY;
leftClick->trigger = 0;
leftClick->action = true;
Mapping *deleteCharacter = new Mapping;
deleteCharacter->bind = Bind::DELETE_CHAR;
deleteCharacter->type = Mapping::KEYBOARD;
deleteCharacter->trigger = GLFW_KEY_BACKSPACE;
deleteCharacter->action = true;
Mapping *up = new Mapping;
up->bind = Bind::SCROLLING_UP;
up->type = Mapping::KEYBOARD;
up->trigger = GLFW_KEY_W;
up->action = true;
Mapping *down = new Mapping;
down->bind = Bind::SCROLLING_DOWN;
down->type = Mapping::KEYBOARD;
down->trigger = GLFW_KEY_S;
down->action = true;
mappings.push_back(leftClick);
mappings.push_back(deleteCharacter);
mappings.push_back(up);
mappings.push_back(down);
}
void GuiAppState::onDettached() {
+5 -4
View File
@@ -173,8 +173,11 @@ namespace battleship{
void InGameAppState::SubmarineCreationButton::onClick() {
}
InGameAppState::InGameAppState(vector<string> difficultyLevels, vector<string> factions) {
type = AppStateType::IN_GAME_STATE;
InGameAppState::InGameAppState(vector<string> difficultyLevels, vector<string> factions) : AbstractAppState(
AppStateType::IN_GAME_STATE,
configData::calcSumBinds(AppStateType::IN_GAME_STATE, true),
configData::calcSumBinds(AppStateType::IN_GAME_STATE, false),
PATH + "Scripts/options.lua"){
this->playerId = 0;
this->difficultyLevels = difficultyLevels;
this->factions = factions;
@@ -184,8 +187,6 @@ namespace battleship{
}
void InGameAppState::onAttached() {
readFile(PATH + "../options.cfg", bindingsLines, 0, configData::numBinds[AppStateType::IN_GAME_STATE]);
AbstractAppState::onAttached();
map = new Map();
-79
View File
@@ -1,79 +0,0 @@
#include "luaManager.h"
#include <iostream>
using namespace std;
namespace battleship{
static LuaManager *luaManager = nullptr;
LuaManager* LuaManager::getSingleton(){
if(!luaManager) luaManager = new LuaManager();
return luaManager;
}
LuaManager::LuaManager(){
state = luaL_newstate();
luaL_openlibs(state);
}
void LuaManager::readFile(string file){
if(luaL_dofile(state, file.c_str()) && lua_pcall(state, 0, 0, 0))
cout << "Error reading Lua file" << file << "\n";
}
int LuaManager::getInt(string varName){
lua_getglobal(state, varName.c_str());
int isNum;
int var = (int)lua_tointegerx(state, -1, &isNum);
lua_pop(state, 1);
return var;
}
void LuaManager::prepareTableForRetrieval(string table, vector<Index> &indices){
lua_getglobal(state, table.c_str());
if(!lua_istable(state, -1))
cout << table << " is not a table\n";
for(int i = 0; i < indices.size(); i++){
if(indices[i].string)
lua_pushstring(state, indices[i].index.c_str());
else
lua_pushnumber(state, atoi(indices[i].index.c_str()));
lua_gettable(state, -2);
}
}
int LuaManager::getIntFromTable(string table, vector<Index> indices){
prepareTableForRetrieval(table, indices);
int isNum;
int result = lua_tointegerx(state, -1, &isNum);
lua_pop(state, 1 + indices.size());
return result;
}
float LuaManager::getFloatFromTable(string table, vector<Index> indices){
prepareTableForRetrieval(table, indices);
int isNum;
float result = lua_tonumberx(state, -1, &isNum);
lua_pop(state, 1 + indices.size());
return result;
}
string LuaManager::getStringFromTable(string table, vector<Index> indices){
prepareTableForRetrieval(table, indices);
string result = lua_tostring(state, -1);
lua_pop(state, 1 + indices.size());
return result;
}
}
-39
View File
@@ -1,39 +0,0 @@
#ifndef LUA_MANAGER_H
#define LUA_MANAGER_H
#include <lua.hpp>
#include <lauxlib.h>
#include <lualib.h>
#include <string>
#include <vector>
namespace battleship{
class LuaManager{
public:
struct Index{
std::string index;
bool string;
Index(std::string index, bool string){
this->index = index;
this->string = string;
}
};
static LuaManager* getSingleton();
inline lua_State* getState(){return state;}
void readFile(std::string);
int getInt(std::string);
int getIntFromTable(std::string, std::vector<Index>);
float getFloatFromTable(std::string, std::vector<Index>);
std::string getStringFromTable(std::string, std::vector<Index>);
private:
LuaManager();
void prepareTableForRetrieval(std::string, std::vector<Index>&);
lua_State *state = nullptr;
};
}
#endif
+1
View File
@@ -1,5 +1,6 @@
#include "optionsButton.h"
#include "util.h"
#include "defConfigs.h"
#include <stateManager.h>
+7 -7
View File
@@ -1,16 +1,16 @@
#include "unitDataManager.h"
#include "luaManager.h"
#include "defConfigs.h"
#include <lauxlib.h>
#include <lualib.h>
#include <luaManager.h>
#include <string>
#include <iostream>
using namespace std;
using namespace vb01;
namespace battleship{
using namespace std;
using namespace vb01;
using namespace gameBase;
typedef LuaManager::Index Index;
UnitDataManager *unitDataManager = nullptr;
@@ -24,7 +24,7 @@ namespace battleship{
UnitDataManager::UnitDataManager(){
LuaManager *luaManager = LuaManager::getSingleton();
luaManager->readFile("../Scripts/unitData.lua");
luaManager->buildScript(vector<string>{configData::PATH + "defPaths.lua", configData::PATH + "unitData.lua"});
numUnits = luaManager->getInt("numUnits");