Basic, buggy movement purely from Python API

This commit is contained in:
John McCardle 2023-07-17 22:08:06 -04:00
parent e85861cbb2
commit c1c17bab69
4 changed files with 27 additions and 5 deletions

View File

@ -90,3 +90,12 @@ Main problem that came up today: all Python code is executed at the moment the G
Workaround: There's a clickable button that performs the input registration. This is good for working out the behavior, but doesn't really allow Python scripts to properly control and set up their own environment. Workaround: There's a clickable button that performs the input registration. This is good for working out the behavior, but doesn't really allow Python scripts to properly control and set up their own environment.
The module name is passed to the PythonScene constructor, and the `start()` method is called to set up class objects. Can I add more methods that are called on this module to swap scenes? The module name is passed to the PythonScene constructor, and the `start()` method is called to set up class objects. Can I add more methods that are called on this module to swap scenes?
## Notes 17 July
The player entity is moving around via Python now!
Unfortunately, the "actiononce" macro I use in C++ is not the behavior passed on to the Python key events. Key release events are being passed to Python just the way that keypresses are.
I'll need to expose more of the input event properties down to Python. I also don't like how keycodes are being passed from python to C++, which currently limits user input to key strokes. Mouse buttons and mouse wheel should be possible too (just as they are under the SFML event binding).

View File

@ -297,7 +297,7 @@ void Grid::render(sf::RenderWindow & window)
// visible & discovered layers for testing purposes // visible & discovered layers for testing purposes
if (!gridpoint.discovered) { if (!gridpoint.discovered) {
r.setFillColor(sf::Color(16, 16, 20, 255)); r.setFillColor(sf::Color(16, 16, 20, 192)); // 255 opacity for actual blackout
renderTexture.draw(r); renderTexture.draw(r);
} else if (!gridpoint.visible) { } else if (!gridpoint.visible) {
r.setFillColor(sf::Color(32, 32, 40, 128)); r.setFillColor(sf::Color(32, 32, 40, 128));

View File

@ -12,6 +12,7 @@ PythonScene::PythonScene(GameEngine* g, std::string pymodule)
registerAction(ActionCode::MOUSEWHEEL + ActionCode::WHEEL_NEG + ActionCode::WHEEL_DEL, "wheel_down"); registerAction(ActionCode::MOUSEWHEEL + ActionCode::WHEEL_NEG + ActionCode::WHEEL_DEL, "wheel_down");
// keyboard events // keyboard events
/*
registerAction(ActionCode::KEY + sf::Keyboard::Q, "upleft"); registerAction(ActionCode::KEY + sf::Keyboard::Q, "upleft");
registerAction(ActionCode::KEY + sf::Keyboard::W, "up"); registerAction(ActionCode::KEY + sf::Keyboard::W, "up");
registerAction(ActionCode::KEY + sf::Keyboard::E, "upright"); registerAction(ActionCode::KEY + sf::Keyboard::E, "upright");
@ -31,7 +32,7 @@ PythonScene::PythonScene(GameEngine* g, std::string pymodule)
registerAction(ActionCode::KEY + sf::Keyboard::Numpad1, "downleft"); registerAction(ActionCode::KEY + sf::Keyboard::Numpad1, "downleft");
registerAction(ActionCode::KEY + sf::Keyboard::Numpad2, "down"); registerAction(ActionCode::KEY + sf::Keyboard::Numpad2, "down");
registerAction(ActionCode::KEY + sf::Keyboard::Numpad3, "downright"); registerAction(ActionCode::KEY + sf::Keyboard::Numpad3, "downright");
*/
// window resize // window resize
registerAction(0, "event"); registerAction(0, "event");

View File

@ -259,14 +259,15 @@ class TestScene:
mcrfpy.createSprite("o", 4, 4, 10, 10, 1.0) mcrfpy.createSprite("o", 4, 4, 10, 10, 1.0)
mcrfpy.createMenu("p", 20, 20, 40, 40) #11 mcrfpy.createMenu("p", 20, 20, 40, 40) #11
mcrfpy.createButton("p", 0, 0, 130, 40, DARKGREEN, (0, 0, 0), "Register", "keyregistration") #mcrfpy.createButton("p", 0, 0, 130, 40, DARKGREEN, (0, 0, 0), "Register", "keyregistration")
mcrfpy.createButton("p", 0, 0, 130, 40, DARKGREEN, (0, 0, 0), "Register", "startrepl")
mcrfpy.registerPyAction("keyregistration", keyregistration) mcrfpy.registerPyAction("keyregistration", keyregistration)
#print("Make UIs visible") #print("Make UIs visible")
self.menus = mcrfpy.listMenus() self.menus = mcrfpy.listMenus()
self.menus[0].visible = True self.menus[0].visible = True
self.menus[1].w = 170 self.menus[1].w = 170
self.menus[1].visible = True self.menus[1].visible = True
#self.menus[2].visible = True self.menus[2].visible = True
for mn in range(2, 6): for mn in range(2, 6):
self.menus[mn].bgcolor = BLACK self.menus[mn].bgcolor = BLACK
@ -551,7 +552,18 @@ def keyregistration():
print("Registering 'keytest'") print("Registering 'keytest'")
mcrfpy.registerPyAction("keytest", keytest) mcrfpy.registerPyAction("keytest", keytest)
print("Registering input") print("Registering input")
print(mcrfpy.registerInputAction(15, "keytest")) print(mcrfpy.registerInputAction(15, "keytest")) # 15 = P
mcrfpy.registerPyAction("player_move_up", lambda: scene.player.move(0, -1))
mcrfpy.registerPyAction("player_move_left", lambda: scene.player.move(-1, 0))
mcrfpy.registerPyAction("player_move_down", lambda: scene.player.move(0, 1))
mcrfpy.registerPyAction("player_move_right", lambda: scene.player.move(1, 0))
mcrfpy.registerInputAction(ord('w') - ord('a'), "player_move_up")
mcrfpy.registerInputAction(ord('a') - ord('a'), "player_move_left")
mcrfpy.registerInputAction(ord('s') - ord('a'), "player_move_down")
mcrfpy.registerInputAction(ord('d') - ord('a'), "player_move_right")
def start(): def start():
global scene global scene