Held key repeat pressing

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent d94382c568
commit ee2154a67b
  1. BIN
      C++ProjectTemplate
  2. 56
      main.cpp

Binary file not shown.

@ -163,8 +163,9 @@ public:
bool HEALING_OVERWORLD_SINGLE_MEMBER=false; //When set to true, a single member is being healed. bool HEALING_OVERWORLD_SINGLE_MEMBER=false; //When set to true, a single member is being healed.
bool OVERWORLD_ITEM_BEING_USED=false; //When set to true, we need to equip an item after target selected. bool OVERWORLD_ITEM_BEING_USED=false; //When set to true, we need to equip an item after target selected.
std::string DISPLAY_TARGET_MESSAGE=""; //Display some text while selecting a target. std::string DISPLAY_TARGET_MESSAGE=""; //Display some text while selecting a target.
Key KEY_GENERATE_EVENTS=UP; Key KEY_LASTPRESSED=NONE;
float lastPress=0; int lastPress=0;
int lastRepeatedFrame=0;
bool MOUSE_PRESSED_DOWN=false,MOUSE_DOWN=false,MOUSE_RELEASED=false; //TODO Implement Mouse things. bool MOUSE_PRESSED_DOWN=false,MOUSE_DOWN=false,MOUSE_RELEASED=false; //TODO Implement Mouse things.
@ -303,13 +304,13 @@ This is a test message that lets us trigger straight from a cutscene! Cool!)"),
return true; return true;
} }
void GetAnyKey() {
}
void GetAnyKeyPress(olc::Key keypress) override { void GetAnyKeyPress(olc::Key keypress) override {
if (keypress==UP||keypress==DOWN||keypress==RIGHT||keypress==LEFT) { if (keypress==UP||keypress==DOWN||keypress==RIGHT||keypress==LEFT||
KEY_GENERATE_EVENTS=keypress; keypress==W||keypress==A||keypress==S||keypress==D||
keypress==NP2||keypress==NP4||keypress==NP5||keypress==NP6||keypress==NP8) {
KEY_LASTPRESSED=keypress;
lastPress=frameCount;
lastRepeatedFrame=frameCount;
} }
if (messageBoxVisible) { if (messageBoxVisible) {
if (messageBoxMarker==messageBoxFinalText.length()) { if (messageBoxMarker==messageBoxFinalText.length()) {
@ -675,6 +676,10 @@ This is a test message that lets us trigger straight from a cutscene! Cool!)"),
StartEffect(FOUNTAIN_EFFECT); StartEffect(FOUNTAIN_EFFECT);
} }
if (GetKey(KEY_LASTPRESSED).bReleased) {
KEY_LASTPRESSED=NONE;
}
if (BATTLE_ENCOUNTER!=nullptr&&!messageBoxVisible) { if (BATTLE_ENCOUNTER!=nullptr&&!messageBoxVisible) {
switch (BATTLE_STATE) { switch (BATTLE_STATE) {
case BattleState::SELECT_ACTION:{ case BattleState::SELECT_ACTION:{
@ -2654,16 +2659,16 @@ This is a test message that lets us trigger straight from a cutscene! Cool!)"),
return GetKey(TAB).bHeld; return GetKey(TAB).bHeld;
} }
bool UpPressed(){ bool UpPressed(){
return GetKey(W).bPressed||GetKey(UP).bPressed||GetKey(NP8).bPressed||MOUSE_PRESSED_DOWN&&GetMouseY()<=HEIGHT-128+32&&GetMouseY()>=HEIGHT-128&&GetMouseX()<=HEIGHT-128; return GetKey(W).bPressed||GetKey(UP).bPressed||GetKey(NP8).bPressed||MOUSE_PRESSED_DOWN&&GetMouseY()<=HEIGHT-128+32&&GetMouseY()>=HEIGHT-128&&GetMouseX()<=HEIGHT-128||KeyRepeat(UP);
} }
bool DownPressed(){ bool DownPressed(){
return GetKey(S).bPressed||GetKey(DOWN).bPressed||GetKey(NP5).bPressed||GetKey(NP2).bPressed||MOUSE_PRESSED_DOWN&&GetMouseY()<=HEIGHT&&GetMouseY()>=HEIGHT-32&&GetMouseX()<=HEIGHT-128; return GetKey(S).bPressed||GetKey(DOWN).bPressed||GetKey(NP5).bPressed||GetKey(NP2).bPressed||MOUSE_PRESSED_DOWN&&GetMouseY()<=HEIGHT&&GetMouseY()>=HEIGHT-32&&GetMouseX()<=HEIGHT-128||KeyRepeat(DOWN);
} }
bool LeftPressed(){ bool LeftPressed(){
return GetKey(A).bPressed||GetKey(LEFT).bPressed||GetKey(NP4).bPressed||MOUSE_PRESSED_DOWN&&GetMouseX()<=32&&GetMouseX()>=0&&GetMouseY()>=HEIGHT-128; return GetKey(A).bPressed||GetKey(LEFT).bPressed||GetKey(NP4).bPressed||MOUSE_PRESSED_DOWN&&GetMouseX()<=32&&GetMouseX()>=0&&GetMouseY()>=HEIGHT-128||KeyRepeat(LEFT);
} }
bool RightPressed(){ bool RightPressed(){
return GetKey(D).bPressed||GetKey(RIGHT).bPressed||GetKey(NP6).bPressed||MOUSE_PRESSED_DOWN&&GetMouseX()<=128&&GetMouseX()>=96&&GetMouseY()>=HEIGHT-128; return GetKey(D).bPressed||GetKey(RIGHT).bPressed||GetKey(NP6).bPressed||MOUSE_PRESSED_DOWN&&GetMouseX()<=128&&GetMouseX()>=96&&GetMouseY()>=HEIGHT-128||KeyRepeat(RIGHT);
} }
bool UpHeld(){ bool UpHeld(){
return GetKey(W).bHeld||GetKey(UP).bHeld||GetKey(NP8).bHeld||MOUSE_DOWN&&GetMouseY()<=HEIGHT-128+32&&GetMouseY()>=HEIGHT-128&&GetMouseX()<=HEIGHT-128; return GetKey(W).bHeld||GetKey(UP).bHeld||GetKey(NP8).bHeld||MOUSE_DOWN&&GetMouseY()<=HEIGHT-128+32&&GetMouseY()>=HEIGHT-128&&GetMouseX()<=HEIGHT-128;
@ -4148,6 +4153,33 @@ This is a test message that lets us trigger straight from a cutscene! Cool!)"),
} }
} }
} }
//Returns true if the key is meant to be continuously repeated because of being held down.
//dir should be UP,DOWN,LEFT,RIGHT
bool KeyRepeat(Key dir) {
if (KEY_LASTPRESSED!=NONE) {
bool allowed=false;
switch (dir) {
case UP:{
allowed=(KEY_LASTPRESSED==W||KEY_LASTPRESSED==NP8||KEY_LASTPRESSED==UP)&&frameCount-lastPress>20&&frameCount%4==0&&lastRepeatedFrame!=frameCount;
}break;
case DOWN:{
allowed=(KEY_LASTPRESSED==S||KEY_LASTPRESSED==NP2||KEY_LASTPRESSED==NP5||KEY_LASTPRESSED==DOWN)&&frameCount-lastPress>20&&frameCount%4==0&&lastRepeatedFrame!=frameCount;
}break;
case LEFT:{
allowed=(KEY_LASTPRESSED==A||KEY_LASTPRESSED==NP4||KEY_LASTPRESSED==LEFT)&&frameCount-lastPress>20&&frameCount%4==0&&lastRepeatedFrame!=frameCount;
}break;
case RIGHT:{
allowed=(KEY_LASTPRESSED==D||KEY_LASTPRESSED==NP6||KEY_LASTPRESSED==RIGHT)&&frameCount-lastPress>20&&frameCount%4==0&&lastRepeatedFrame!=frameCount;
}break;
}
if (allowed) {
lastRepeatedFrame=frameCount;
return true;
}
}
return false;
}
}; };
int main() int main()

Loading…
Cancel
Save