Skip to content

Commit

Permalink
Add next [n] - go to next frame from devtools
Browse files Browse the repository at this point in the history
User can go to next frame when the game is paused. To do so he can:

* issue next command in REPL terminal (or 'n')
* press n in game window
  • Loading branch information
elgopher committed Aug 13, 2023
1 parent 74af10d commit e8c0b55
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
14 changes: 12 additions & 2 deletions devtools/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
)

var (
gamePaused bool
timeWhenPaused float64
gamePaused bool
pauseOnNextFrame bool
timeWhenPaused float64
)

var helpShown bool
Expand All @@ -35,3 +36,12 @@ func resumeGame() {
snapshot.Draw()
fmt.Println("Game resumed")
}

func resumeUntilNextFrame() {
if gamePaused {
resumeGame()
pauseOnNextFrame = true
} else {
fmt.Println("Game not paused")
}
}
2 changes: 1 addition & 1 deletion devtools/internal/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func PrintHelp(topic string) error {
"\n\n" +
"Type help topic for more information. For example: help pi or help pi.Spr" +
"\n\n" +
"Available commands: help [h], pause [p], resume [r], undo [u]",
"Available commands: help [h], pause [p], resume [r], undo [u], next [n]",
)
return nil
default:
Expand Down
3 changes: 3 additions & 0 deletions devtools/internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ const (
Paused EvalResult = 3
Undoed EvalResult = 4
Continued EvalResult = 5
NextFrame EvalResult = 6
)

func (i Instance) Eval(cmd string) (EvalResult, error) {
Expand All @@ -199,6 +200,8 @@ func (i Instance) Eval(cmd string) (EvalResult, error) {
if isHelpCommand(trimmedCmd) {
topic := strings.Trim(strings.TrimLeft(trimmedCmd, "help"), " ")
return HelpPrinted, i.printHelp(topic)
} else if trimmedCmd == "next" || trimmedCmd == "n" {
return NextFrame, nil
} else if trimmedCmd == "resume" || trimmedCmd == "r" {
return Resumed, nil
} else if trimmedCmd == "pause" || trimmedCmd == "p" {
Expand Down
2 changes: 2 additions & 0 deletions devtools/scripting.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func evaluateNextCommandFromTerminal() {
case interpreter.Undoed:
snapshot.Undo()
fmt.Println("Undoed last draw operation")
case interpreter.NextFrame:
resumeUntilNextFrame()
}

if result == interpreter.Continued {
Expand Down
9 changes: 9 additions & 0 deletions devtools/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ func updateDevTools() {
}
}

if pauseOnNextFrame {
pauseGame()
pauseOnNextFrame = false
}

if gamePaused {
if key.Btnp(key.N) {
resumeUntilNextFrame()
}

inspector.Update()
}

Expand Down

0 comments on commit e8c0b55

Please sign in to comment.