Dynamically assign script values for items and inputs
This commit is contained in:
@@ -1,51 +1,41 @@
|
|||||||
module('platform')
|
module('platform')
|
||||||
module('input')
|
module('input')
|
||||||
module('scene')
|
module('scene')
|
||||||
module('item')
|
|
||||||
|
|
||||||
-- Default Input bindings.
|
-- Default Input bindings.
|
||||||
if PLATFORM == "psp" then
|
if PLATFORM == "psp" then
|
||||||
inputBind("up", "up")
|
inputBind("up", INPUT_ACTION_UP)
|
||||||
inputBind("down", "down")
|
inputBind("down", INPUT_ACTION_DOWN)
|
||||||
inputBind("left", "left")
|
inputBind("left", INPUT_ACTION_LEFT)
|
||||||
inputBind("right", "right")
|
inputBind("right", INPUT_ACTION_RIGHT)
|
||||||
inputBind("circle", "cancel")
|
inputBind("circle", INPUT_ACTION_CANCEL)
|
||||||
inputBind("cross", "accept")
|
inputBind("cross", INPUT_ACTION_ACCEPT)
|
||||||
inputBind("select", "ragequit")
|
inputBind("select", INPUT_ACTION_RAGEQUIT)
|
||||||
inputBind("lstick_up", "up")
|
inputBind("lstick_up", INPUT_ACTION_UP)
|
||||||
inputBind("lstick_down", "down")
|
inputBind("lstick_down", INPUT_ACTION_DOWN)
|
||||||
inputBind("lstick_left", "left")
|
inputBind("lstick_left", INPUT_ACTION_LEFT)
|
||||||
inputBind("lstick_right", "right")
|
inputBind("lstick_right", INPUT_ACTION_RIGHT)
|
||||||
else
|
else
|
||||||
if INPUT_KEYBOARD then
|
if INPUT_KEYBOARD then
|
||||||
inputBind("w", "up")
|
inputBind("w", INPUT_ACTION_UP)
|
||||||
inputBind("s", "down")
|
inputBind("s", INPUT_ACTION_DOWN)
|
||||||
inputBind("a", "left")
|
inputBind("a", INPUT_ACTION_LEFT)
|
||||||
inputBind("d", "right")
|
inputBind("d", INPUT_ACTION_RIGHT)
|
||||||
|
|
||||||
inputBind("left", "left")
|
inputBind("left", INPUT_ACTION_LEFT)
|
||||||
inputBind("right", "right")
|
inputBind("right", INPUT_ACTION_RIGHT)
|
||||||
inputBind("up", "up")
|
inputBind("up", INPUT_ACTION_UP)
|
||||||
inputBind("down", "down")
|
inputBind("down", INPUT_ACTION_DOWN)
|
||||||
|
|
||||||
inputBind("enter", "accept")
|
inputBind("enter", INPUT_ACTION_ACCEPT)
|
||||||
inputBind("e", "accept" )
|
inputBind("e", INPUT_ACTION_ACCEPT)
|
||||||
|
|
||||||
inputBind("escape", "cancel")
|
inputBind("escape", INPUT_ACTION_CANCEL)
|
||||||
inputBind("q", "cancel")
|
inputBind("q", INPUT_ACTION_CANCEL)
|
||||||
|
|
||||||
inputBind("z", "ragequit")
|
inputBind("z", INPUT_ACTION_RAGEQUIT)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
print("Backpack:", BACKPACK)
|
|
||||||
print('Has Potion?', inventoryItemExists(BACKPACK, 1))
|
|
||||||
inventoryAdd(BACKPACK, 1, 3)
|
|
||||||
print('Has Potion?', inventoryItemExists(BACKPACK, 1))
|
|
||||||
inventoryRemove(BACKPACK, 1)
|
|
||||||
print('Has Potion?', inventoryItemExists(BACKPACK, 1))
|
|
||||||
print('Potion Count:', inventoryGetCount(BACKPACK, 1))
|
|
||||||
print('Is Backpack Full?', inventoryIsFull(BACKPACK))
|
|
||||||
|
|
||||||
-- sceneSet('map')
|
-- sceneSet('map')
|
||||||
-- mapLoad('map/testmap/testmap.dmf')
|
-- mapLoad('map/testmap/testmap.dmf')
|
||||||
@@ -149,6 +149,7 @@ void inputBind(const inputbutton_t button, const inputaction_t act) {
|
|||||||
act < INPUT_ACTION_COUNT,
|
act < INPUT_ACTION_COUNT,
|
||||||
"Invalid input action"
|
"Invalid input action"
|
||||||
);
|
);
|
||||||
|
assertTrue(act != INPUT_ACTION_NULL, "Cannot bind to NULL action");
|
||||||
|
|
||||||
// Get the button data for this button.
|
// Get the button data for this button.
|
||||||
inputbuttondata_t *data = INPUT_BUTTON_DATA;
|
inputbuttondata_t *data = INPUT_BUTTON_DATA;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
#include "item/itemtype.h"
|
#include "item/itemtype.h"
|
||||||
#include "item/itemid.h"
|
#include "item/itemid.h"
|
||||||
|
#include "item/itemname.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
itemtype_t type;
|
itemtype_t type;
|
||||||
|
|||||||
@@ -18,13 +18,20 @@ int moduleInputBind(lua_State *L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!lua_isstring(L, 2)) {
|
// Expect action ID
|
||||||
luaL_error(L, "inputBind: Expected action name as second argument");
|
if(!lua_isinteger(L, 2)) {
|
||||||
|
luaL_error(L, "inputBind: Expected action ID as second argument");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char_t *strBtn = lua_tostring(L, 1);
|
const char_t *strBtn = lua_tostring(L, 1);
|
||||||
const char_t *strAct = lua_tostring(L, 2);
|
const inputaction_t action = (inputaction_t)lua_tointeger(L, 2);
|
||||||
|
|
||||||
|
// Validate action
|
||||||
|
if(action < INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
||||||
|
luaL_error(L, "inputBind: Invalid action ID %d", (int)action);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Get button by name
|
// Get button by name
|
||||||
inputbutton_t btn = inputButtonGetByName(strBtn);
|
inputbutton_t btn = inputButtonGetByName(strBtn);
|
||||||
@@ -33,20 +40,22 @@ int moduleInputBind(lua_State *L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get action by name
|
inputBind(btn, action);
|
||||||
inputaction_t act = inputActionGetByName(strAct);
|
|
||||||
if(act == INPUT_ACTION_COUNT) {
|
|
||||||
printf("inputBind: Unknown action name '%s'\n", strAct);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
inputBind(btn, act);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void moduleInput(scriptcontext_t *context) {
|
void moduleInput(scriptcontext_t *context) {
|
||||||
|
char_t buffer[128];
|
||||||
assertNotNull(context, "Script context cannot be NULL");
|
assertNotNull(context, "Script context cannot be NULL");
|
||||||
|
|
||||||
|
// Set input information
|
||||||
|
for(inputaction_t act = INPUT_ACTION_NULL + 1; act < INPUT_ACTION_COUNT; act++) {
|
||||||
|
const char_t *actName = INPUT_ACTION_NAMES[act];
|
||||||
|
assertStrLenMax(actName, 64, "Input action name too long for buffer");
|
||||||
|
snprintf(buffer, sizeof(buffer), "INPUT_ACTION_%s = %d\n", actName, act);
|
||||||
|
scriptContextExec(context, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
// Input values.
|
// Input values.
|
||||||
scriptContextExec(context,
|
scriptContextExec(context,
|
||||||
#if INPUT_KEYBOARD == 1
|
#if INPUT_KEYBOARD == 1
|
||||||
|
|||||||
@@ -238,9 +238,16 @@ int moduleInventorySort(lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void moduleItem(scriptcontext_t *context) {
|
void moduleItem(scriptcontext_t *context) {
|
||||||
|
char_t buffer[128];
|
||||||
assertNotNull(context, "Script context cannot be NULL");
|
assertNotNull(context, "Script context cannot be NULL");
|
||||||
|
|
||||||
// Item information
|
// Set item information
|
||||||
|
for(itemid_t itemId = ITEM_ID_NULL + 1; itemId < ITEM_ID_COUNT; itemId++) {
|
||||||
|
const char_t *itemName = ITEM_NAMES[itemId];
|
||||||
|
assertStrLenMax(itemName, 64, "Item name too long for buffer");
|
||||||
|
snprintf(buffer, sizeof(buffer), "ITEM_ID_%s = %d\n", itemName, itemId);
|
||||||
|
scriptContextExec(context, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
// Bind BACKPACK const pointer
|
// Bind BACKPACK const pointer
|
||||||
scriptContextRegPointer(context, "BACKPACK", (void *)&BACKPACK);
|
scriptContextRegPointer(context, "BACKPACK", (void *)&BACKPACK);
|
||||||
|
|||||||
Reference in New Issue
Block a user