Skip to content

Commit

Permalink
#40: implement continuous fire
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaulwurff committed Oct 5, 2019
1 parent 9d002c4 commit 2ceda63
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 28 deletions.
3 changes: 2 additions & 1 deletion menudef.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ OptionMenu tt_Options

// Option Values ///////////////////////////////////////////////////////////////

// Attention! Keep this list consistent with the list in tt_Supervisor.makeQuestionSource().
// Attention! Keep this list consistent with the list in
// tt_Supervisor.makeQuestionSource().
OptionValue tt_QuestionSourceValues
{
0 , "$TT_CHALLENGE_LETTERS"
Expand Down
21 changes: 17 additions & 4 deletions zscript/typist/answer/tt_answer.zs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,40 @@
* Typist.pk3. If not, see <https://www.gnu.org/licenses/>.
*/

/** This class represents an answer to a tt_Question.
/**
* This class represents an answer to a tt_Question.
* @see tt_Question
*/
class tt_Answer
{

// public: /////////////////////////////////////////////////////////////////////

tt_Answer init(String answer)
tt_Answer init(String answer = "")
{
_answer = answer;
_answer = answer;
_isFinished = false;
_isReadyToReset = false;

return self;
}

// public: /////////////////////////////////////////////////////////////////////

String getString() const { return _answer; }
String getString() const { return _answer; }
bool isFinished() const { return _isFinished; }
bool isReadyToReset() const { return _isReadyToReset; }

void append(String character) { _answer = _answer .. character; }
void deleteLastCharacter() { _answer.DeleteLastCharacter(); }

void finish() { _isFinished = true; }
void setReadyToReset() { _isReadyToReset = true; }

// private: ////////////////////////////////////////////////////////////////////

private String _answer;
private bool _isFinished;
private bool _isReadyToReset;

} // class tt_Answer
31 changes: 22 additions & 9 deletions zscript/typist/answer/tt_player_input.zs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@
* Typist.pk3. If not, see <https://www.gnu.org/licenses/>.
*/

/** This class implements tt_AnswerSource by receiving player key inputs and
/**
* This class implements tt_AnswerSource by receiving player key inputs and
* composing an answer from them.
*/
class tt_PlayerInput : tt_AnswerSource
{

// public: /////////////////////////////////////////////////////////////////////

tt_PlayerInput init() { return self; }
tt_PlayerInput init()
{
_answer = new("tt_Answer").init();

return self;
}

// public: /////////////////////////////////////////////////////////////////////

Expand All @@ -33,9 +39,14 @@ class tt_PlayerInput : tt_AnswerSource
switch (type)
{
case tt_Character.NONE: break;
case tt_Character.PRINTABLE: _answer = _answer .. character.getCharacter(); break;
case tt_Character.BACKSPACE: _answer.DeleteLastCharacter(); break;
case tt_Character.CTRL_BACKSPACE: _answer = ""; break;

case tt_Character.PRINTABLE: _answer.append(character.getCharacter()); break;
case tt_Character.BACKSPACE: _answer.deleteLastCharacter(); break;

case tt_Character.ENTER: _answer.finish(); break;
case tt_Character.ENTER_UP: _answer.setReadyToReset(); break;

case tt_Character.CTRL_BACKSPACE: _answer = new("tt_Answer").init(); break;
}
}

Expand All @@ -44,18 +55,20 @@ class tt_PlayerInput : tt_AnswerSource
override
tt_Answer getAnswer()
{
let result = new("tt_Answer").init(_answer);
return result;
return _answer;
}

override
void reset()
{
_answer = "";
if (_answer.isReadyToReset())
{
_answer = new("tt_Answer").init();
}
}

// private: ////////////////////////////////////////////////////////////////////

private String _answer;
private tt_Answer _answer;

} // class tt_PlayerInput
14 changes: 6 additions & 8 deletions zscript/typist/character/tt_character.zs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@ class tt_Character

tt_Character init(int code, bool isShift, bool isCtrl)
{
if (code == 8)
{
_type = (isCtrl ? CTRL_BACKSPACE : BACKSPACE);
}
else if (code <= 31)
{
_type = NONE;
}
if (code == 8) { _type = (isCtrl ? CTRL_BACKSPACE : BACKSPACE); }
else if (code == 13) { _type = ENTER; }
else if (code == 3) { _type = ENTER_UP; }
else if (code <= 31) { _type = NONE; }
else
{
_type = PRINTABLE;
Expand All @@ -54,6 +50,8 @@ class tt_Character
PRINTABLE,
BACKSPACE,
CTRL_BACKSPACE,
ENTER,
ENTER_UP,
}

int getType() const { return _type; }
Expand Down
5 changes: 5 additions & 0 deletions zscript/typist/character/tt_character_test.zs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class tt_CharacterTest : tt_Clematis
It("Ctrl-Backspace", Assert(ctrlBackspace.getType() == tt_Character.CTRL_BACKSPACE));
}

{
let enter = new("tt_Character").init(13, false, true);
It("Enter", Assert(enter.getType() == tt_Character.ENTER));
}

EndDescribe();
}

Expand Down
37 changes: 32 additions & 5 deletions zscript/typist/event_handler/tt_event_handler.zs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class tt_EventHandler : EventHandler
int mode = _supervisor.getMode();
bool isCapturingKeys = (mode == tt_Mode.MODE_COMBAT);

// detect the end of combat
if (self.IsUiProcessor && isCapturingKeys == false)
{
let reset = new("tt_Character").init(CODE_BACKSPACE, false, true);
_supervisor.processKey(reset);
}

self.IsUiProcessor = isCapturingKeys;
}

Expand All @@ -41,12 +48,26 @@ class tt_EventHandler : EventHandler
{
if (_supervisor == NULL) { return false; }

bool isChar = (event.type == UiEvent.Type_Char);
bool isDown = (event.type == UiEvent.Type_KeyDown);
bool isBackspace = (event.keyChar == 8);
if (isChar || (isDown && isBackspace))
int code = event.keyChar;
int type = event.type;

//Console.Printf("type: %d, code: %d, string: %s", type, code, event.keyString);

if (type == UiEvent.Type_KeyUp && code == CODE_ENTER)
{
let character = new("tt_Character").init(event.keyChar, event.isShift, event.isCtrl);
type = UiEvent.Type_KeyDown;
code = CODE_END_OF_TEXT;
console.printf("222");
}

bool isChar = (type == UiEvent.Type_Char);
bool isDown = (type == UiEvent.Type_KeyDown);
bool isControl = (code == CODE_BACKSPACE
|| code == CODE_ENTER
|| code == CODE_END_OF_TEXT);
if (isChar || (isDown && isControl))
{
let character = new("tt_Character").init(code, event.isShift, event.isCtrl);
_supervisor.processKey(character);
}

Expand Down Expand Up @@ -107,6 +128,12 @@ class tt_EventHandler : EventHandler
_gameTweaks = new("tt_GameTweaks").init();
}

// private: ////////////////////////////////////////////////////////////////////

const CODE_BACKSPACE = 8;
const CODE_ENTER = 13;
const CODE_END_OF_TEXT = 3;

// private: ////////////////////////////////////////////////////////////////////

private tt_Supervisor _supervisor;
Expand Down
3 changes: 2 additions & 1 deletion zscript/typist/origin/tt_question_answer_matcher.zs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* Typist.pk3. If not, see <https://www.gnu.org/licenses/>.
*/

/** This class implements OriginSource by finding an origin for a known target
/**
* This class implements OriginSource by finding an origin for a known target
* that fits to for the answer.
*/
class tt_QuestionAnswerMatcher : tt_OriginSource
Expand Down
2 changes: 2 additions & 0 deletions zscript/typist/question/tt_match.zs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class tt_Match : tt_Question
override
bool isRight(tt_Answer answer)
{
if (!answer.isFinished()) { return false; }

bool isEqual = (_question == answer.getString());

return isEqual;
Expand Down

0 comments on commit 2ceda63

Please sign in to comment.