Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Jan 2, 2025
1 parent 5e8e31b commit 9b64647
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ build: ## Build

configure: clean ## Configure
conan remote update conancenter --url https://center2.conan.io
conan install . --output-folder=build --build=webassembly --profile=webassembly --settings compiler.cppstd=20 --settings build_type=Release
conan install . --output-folder=build --build=missing --profile=webassembly --settings compiler.cppstd=20 --settings build_type=Release
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DSDL2_DIR=generators -DCMAKE_BUILD_TYPE=Release -DLOCAL=ON -DSANDBOX=OFF -DENABLE_PROFILING=ON
6 changes: 3 additions & 3 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ void engine::flush() const noexcept {
_resourcemanager->flush();
}

bool engine::is_keydown(const input::keyevent &event) const noexcept {
return _statemanager->is_keydown(event);
}
// bool engine::is_keydown(const input::keyevent &event) const noexcept {
// return _statemanager->is_keydown(event);
// }

void engine::prefetch(const std::vector<std::string> &filenames) noexcept {
_resourcemanager->prefetch(filenames);
Expand Down
2 changes: 1 addition & 1 deletion src/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class engine : public input::eventreceiver {

void add_loopable(std::shared_ptr<loopable> loopable) noexcept;
void flush() const noexcept;
bool is_keydown(const input::keyevent &event) const noexcept;
// bool is_keydown(const input::keyevent &event) const noexcept;
void prefetch(const std::vector<std::string> &filenames) noexcept;
void run() noexcept;
void _loop() noexcept;
Expand Down
11 changes: 11 additions & 0 deletions src/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ enum class keyevent : int32_t {
space = SDLK_SPACE,
};

enum class controller : uint32_t {
up,
down,
left,
right,
triangle,
circle,
cross,
square
};

enum class mouseevent : int32_t {};

enum class joystickevent : int32_t {
Expand Down
53 changes: 28 additions & 25 deletions src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ void framework::scriptengine::run() {
"stop", &audio::soundmanager::stop
);

lua.new_enum(
"Controller",
"up", input::controller::up,
"down", input::controller::down,
"left", input::controller::left,
"right", input::controller::right,
"triangle", input::controller::triangle,
"circle", input::controller::circle,
"cross", input::controller::cross,
"square", input::controller::square
);

lua.new_enum(
"Anchor",
"top", framework::anchor::top,
Expand Down Expand Up @@ -292,40 +304,31 @@ void framework::scriptengine::run() {
}
);

struct playerproxy {
framework::statemanager &e;

int8_t at(int8_t val) noexcept {
return val;
}
};

struct playereventproxy {
framework::statemanager &e;
uint8_t index;
struct player {
int index;
const framework::statemanager &e;

void event() noexcept {
std::cout << "Event called for index: " << static_cast<int>(index) << std::endl;
bool on(std::variant<input::controller> type) {
return e.on(index, type);
}
};

lua.new_usertype<playereventproxy>(
"PlayerEventProxy",
"event", &playereventproxy::event
lua.new_usertype<player>(
"Player",
"on", &player::on
);

lua.new_usertype<playerproxy>(
"PlayerProxy",
sol::meta_function::index, [](playerproxy &proxy, uint8_t index) -> playereventproxy {
// return proxy.at(static_cast<int8_t>(index));
return playereventproxy{proxy.e, index};
}
);
static std::unordered_map<int, player> _p;

lua.new_usertype<framework::statemanager>(
"StateManager",
"player", sol::property([](framework::statemanager &e) -> playerproxy { return playerproxy{e}; }),
"is_keydown", &framework::statemanager::is_keydown
"player", [](const framework::statemanager &self, int index, sol::this_state state) {
sol::state_view lua(state);

auto [iterator, inserted] = _p.try_emplace(index, index, self);

return iterator->second;
}
);

lua.new_usertype<framework::scenemanager>(
Expand Down
43 changes: 37 additions & 6 deletions src/statemanager.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
#include "statemanager.hpp"

#include "event.hpp"
#include <unordered_map>

using namespace framework;

bool statemanager::is_keydown(const input::keyevent &event) const {
const auto it = _keys.find(event);
return it != _keys.end() && it->second;
bool statemanager::on(int player, const std::variant<input::controller> &type) const noexcept {
auto pit = _state.find(player);
if (pit != _state.end()) {
auto &map = pit->second;
auto tit = map.find(type);
if (tit != map.end()) {
return tit->second;
}
}

return false;
}

constexpr std::optional<input::controller> keytoctrl(const input::keyevent &event) {
using input::controller;
using input::keyevent;

switch (event) {
case keyevent::up:
return controller::up;
case keyevent::down:
return controller::down;
case keyevent::left:
return controller::left;
case keyevent::right:
return controller::right;
case keyevent::space:
return controller::cross;
default:
return std::nullopt;
}
}

void statemanager::on_keydown(const input::keyevent &event) noexcept {
_keys.emplace(event, true).first->second = true;
if (auto ctrl = keytoctrl(event)) {
_state[0][*ctrl] = true;
}
}

void statemanager::on_keyup(const input::keyevent &event) noexcept {
if (auto it = _keys.find(event); it != _keys.end()) {
it->second = false;
if (auto ctrl = keytoctrl(event)) {
_state[0][*ctrl] = false;
}
}
5 changes: 3 additions & 2 deletions src/statemanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ class statemanager : public input::eventreceiver {
statemanager() = default;
virtual ~statemanager() = default;

bool is_keydown(const input::keyevent &event) const;
// bool is_keydown(const input::keyevent &event) const;
bool on(int player, const std::variant<input::controller> &type) const noexcept;

protected:
virtual void on_keydown(const input::keyevent &event) noexcept;

virtual void on_keyup(const input::keyevent &event) noexcept;

private:
std::unordered_map<input::keyevent, bool> _keys;
std::unordered_map<uint8_t, std::unordered_map<std::variant<input::controller>, bool>> _state;
};
}

0 comments on commit 9b64647

Please sign in to comment.