-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
80 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
#ifndef THREEPP_KEYCONTROLLER_HPP | ||
#define THREEPP_KEYCONTROLLER_HPP | ||
|
||
#include "threepp/input/KeyListener.hpp" | ||
|
||
#include "Youbot.hpp" | ||
|
||
class KeyController: public threepp::KeyListener { | ||
|
||
public: | ||
KeyController(Youbot& youbot) | ||
: youbot_(&youbot) {} | ||
|
||
void onKeyPressed(KeyEvent evt) override { | ||
if (evt.key == Key::W) { | ||
keyState_.up = true; | ||
} else if (evt.key == Key::S) { | ||
keyState_.down = true; | ||
} else if (evt.key == Key::D) { | ||
keyState_.right = true; | ||
} else if (evt.key == Key::A) { | ||
keyState_.left = true; | ||
} | ||
} | ||
|
||
void onKeyReleased(KeyEvent evt) override { | ||
if (evt.key == Key::W) { | ||
keyState_.up = false; | ||
} else if (evt.key == Key::S) { | ||
keyState_.down = false; | ||
} else if (evt.key == Key::D) { | ||
keyState_.right = false; | ||
} else if (evt.key == Key::A) { | ||
keyState_.left = false; | ||
} | ||
} | ||
|
||
void update(float dt) { | ||
|
||
if (keyState_.up) { | ||
youbot_->driveForwards(dt); | ||
} | ||
if (keyState_.down) { | ||
youbot_->driveBackwards(dt); | ||
} | ||
if (keyState_.right) { | ||
youbot_->driveRight(dt); | ||
} | ||
if (keyState_.left) { | ||
youbot_->driveLeft(dt); | ||
} | ||
} | ||
|
||
private: | ||
Youbot* youbot_; | ||
|
||
struct KeyState { | ||
bool left = false; | ||
bool right = false; | ||
bool up = false; | ||
bool down = false; | ||
} keyState_; | ||
}; | ||
|
||
#endif//THREEPP_KEYCONTROLLER_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters