demo

package
v0.0.0-...-1babf2d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package demo contains support for playing scripted demos.

There is an entity Demo which can be added to the screen and it will play demo Script. Demo Script consist of ordered commands. Each command can perform some in game action.

There is support for parsing demo script from string representation. Script string should contain one command per line. It can contain empty lines and commented lines starting with `#`. Alternatively there can be multiple commands per line separated by `;` (semicolon).

Supported commands:

  • `wait s` - will wait for `s` seconds
  • `hideMessageBox` - hides any visible message box
  • `setAngle a` - continuosly change cannon angle to `a` for active player
  • `shoot p` - load power to `p` and shoot with active player
  • `waitForFinishTurn` - will wait for all explosions and bullets are gone and turn is shifted to next player
  • `nextRound` - switches game to the next round
  • `moveFocus` - move focus to next ui component
  • `pressButton` - send action event to ui component with focus
  • `exit` - exits game

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command interface {
	Eval(c *GameContext) bool
}

Command represents some activity which can be done during game play. Each command can be executed multiple times. How many times will be command executed depends on its return value. If command returns true it is considered as finished. Otherwise it will be called again.

type Demo

type Demo struct {
	// contains filtered or unexported fields
}

Demo is entity which when added to the game will play given Script. Use NewDemo to create new demo with some script. Use LoadFromFile to create new demo with script loaded from file.

func LoadFromFile

func LoadFromFile(path string) (*Demo, error)

LoadFromFile loads demo from file with demo script commands

func NewDemo

func NewDemo(script Script) *Demo

NewDemo creates new demo with given script.

func (*Demo) Draw

func (d *Demo) Draw(s *tl.Screen)

Draw invokes all demo script commands in order

func (*Demo) Restart

func (d *Demo) Restart()

Restart will make demo run all script commands from start again

func (*Demo) Tick

func (d *Demo) Tick(e tl.Event)

Tick does nothing now

type Exit

type Exit struct{}

Exit exits the game

func (*Exit) Eval

func (e *Exit) Eval(c *GameContext) bool

Eval evaluates command

type GameContext

type GameContext struct {
	// Controls refers to the game controls which can be used to do some action
	Controls *gorched.Controls
	// Round refers to current game round which can be used to getting the state information
	Round *gorched.Round
	// Dt is delta time between two executions (game screen redraws)
	Dt float64
	// Memory is local memory which "lives" during execution of single command
	Memory *Memory
}

GameContext holds access to the game controls and state.

type HideMessageBox

type HideMessageBox struct{}

HideMessageBox will hide any messagebox if visible

func (*HideMessageBox) Eval

func (t *HideMessageBox) Eval(c *GameContext) bool

Eval evaluates command

type Memory

type Memory struct {
	// contains filtered or unexported fields
}

Memory is simple memory which can be used by commands to store some values under string identifiers

func NewMemory

func NewMemory() *Memory

NewMemory creates new memory with no values

func (*Memory) AddFloat

func (m *Memory) AddFloat(id string, add float64) float64

AddFloat adds given add float to the float value stored in memory on given id.

func (*Memory) Clear

func (m *Memory) Clear(id string)

Clear removes value stored on given id.

func (*Memory) ClearAll

func (m *Memory) ClearAll()

ClearAll removes all values stored in memory

func (*Memory) Read

func (m *Memory) Read(id string) interface{}

Read reads value stored on given id from memory

func (*Memory) ReadFloat

func (m *Memory) ReadFloat(id string) float64

ReadFloat reads float64 value stored on given id from memory. If there is no float64 value stored on id 0 is returned.

func (*Memory) UpdateFloat

func (m *Memory) UpdateFloat(id string, f func(f float64) float64) float64

UpdateFloat reads value from memory stored on given id, applies function f to this value and writes result back on id. Result of function apply is returned too.

func (*Memory) Write

func (m *Memory) Write(id string, v interface{})

Write stores value to memory on given id

func (*Memory) WriteFloat

func (m *Memory) WriteFloat(id string, f float64)

WriteFloat stores float64 value to memory on given id. If there was already some value stored on id it will be overwritten.

type MoveFocus

type MoveFocus struct{}

MoveFocus moves focus on ui form to next component

func (*MoveFocus) Eval

func (m *MoveFocus) Eval(c *GameContext) bool

Eval evaluates command

type NextRound

type NextRound struct{}

NextRound will switch game to the next round.

func (*NextRound) Eval

func (n *NextRound) Eval(c *GameContext) bool

Eval evaluates command

type Parser

type Parser func(tokens []string) (Command, error)

Parser is function which parses Command from array of tokens

type PressButton

type PressButton struct{}

PressButton sends action event to currently focused ui component

func (*PressButton) Eval

func (p *PressButton) Eval(c *GameContext) bool

Eval evaluates command

type Script

type Script []Command

Script is just array of commands.

func ParseScript

func ParseScript(r io.Reader) (Script, error)

ParseScript will read all commands from reader r. Commands can be separated by newline character or by `;` (semicolon). Empty lines and lines starting with '#' (comments) are ignored.

type SetAngle

type SetAngle struct {
	Angle int
}

SetAngle will change tank's angle to specified Angle. Changing is not done immediately but it tries to simulate continuos change like when done via kayboard.

func (*SetAngle) Eval

func (a *SetAngle) Eval(c *GameContext) bool

Eval evaluates command

type Shoot

type Shoot struct {
	Power int
}

Shoot will make tank to load and shoot with given Power

func (*Shoot) Eval

func (s *Shoot) Eval(c *GameContext) bool

Eval evaluates command

type Wait

type Wait struct {
	Seconds float64
}

Wait will wait for given Seconds to be finished. It is not blocking all other entities during wait only next commands.

func (*Wait) Eval

func (w *Wait) Eval(c *GameContext) bool

Eval evaluates command

type WaitForFinishTurn

type WaitForFinishTurn struct{}

WaitForFinishTurn will wait until there are no bullets and explisions in the game world. Use it after Shoot if you want to invoke next actions for next player. It is not blocking all other entities during wait only next commands.

func (*WaitForFinishTurn) Eval

func (w *WaitForFinishTurn) Eval(c *GameContext) bool

Eval evaluates command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL