gamesys

package module
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2020 License: GPL-3.0 Imports: 18 Imported by: 0

README

game-sys

Core Game Engine Functionality

Documentation

Index

Constants

View Source
const (
	// DegRad is a constant to convert to radians.
	DegRad float64 = math.Pi / 180

	// RadDeg is a constant to convert to degrees.
	RadDeg float64 = 180 / math.Pi
)

Variables

This section is empty.

Functions

func Contains

func Contains(container pixel.Rect, target pixel.Rect) bool

Contains will indicate if the rectangle is contained within another rectangle.

func LoadImage

func LoadImage(path string) (pixel.Picture, error)

LoadImage will give us some picture data.

func StrBool

func StrBool(s interface{}) bool

StrBool will return a string as a bool

func StrFloat

func StrFloat(s interface{}) float64

StrFloat will return a string as a float64

Types

type Action

type Action struct {
	// The action keyword
	Action string

	// The arguments for this command
	Args []interface{}
}

Action will hold the instructions on an action to perform

type Actor

type Actor struct {
	// XMLName is how we reference when loading xml information
	XMLName xml.Name `xml:"actor"`

	// Position of the actor, needs to be relative to map
	Position pixel.Vec

	// Destinations will be preset by running scripts.
	Destinations []pixel.Vec

	// Src is the source graphic
	Src pixel.Picture

	// Output should be the sprite
	Output *pixel.Sprite

	// Clip is the area of the actor relative to the map.
	Clip pixel.Rect

	// Speed will set a speed modifier for the actor.
	Speed float64 `xml:"speed,attr"`

	// Visible determines if we see the sprite or not.
	Visible bool

	// Collision determines if it collides with anything or not
	Collision bool
}

Actor is an element that will move around within the view.

func (*Actor) Collides

func (a *Actor) Collides(collision bool)

Collides will set if the actor reacts to the collision layer.

func (*Actor) Draw

func (a *Actor) Draw(v *View)

Draw will draw the respective actor to the provided destination.

func (*Actor) Hide

func (a *Actor) Hide()

Hide will hide the actor

func (*Actor) Move

func (a *Actor) Move(distance pixel.Vec)

Move will move the actor according to the provided vector.

func (*Actor) MoveTo

func (a *Actor) MoveTo(position pixel.Vec)

MoveTo will move the actor to an absolute map position.

func (*Actor) Render added in v0.19.0

func (a *Actor) Render()

Render will draw out the actor to the output. This often only will need to run on file load, not during running loops.

func (*Actor) SetClip

func (a *Actor) SetClip()

SetClip will create a clipping box based on the current actor position.

func (*Actor) Show

func (a *Actor) Show()

Show will show the actor

func (*Actor) Toggle

func (a *Actor) Toggle()

Toggle will toggle the visibility

type Configuration

type Configuration struct {
	XMLName xml.Name `xml:"configuration"`
	System  System   `xml:"system"`
	Default Default  `xml:"default"`
}

Configuration setting collection

func LoadConfiguration

func LoadConfiguration(file string) (*Configuration, error)

LoadConfiguration loads a configuration from the provided XML file.

type Controller

type Controller struct {
	// Handlers are the collections of Handler Sets
	Handlers map[string][]*Handler

	// Engine is the engine the controller is running on.
	Engine *Engine
}

Controller manages the key handlers, running handler methods as appropriate. The current state of the system will depend on what handlers are present. If there are any system handlers present, they will override the application handlers until there are not any system handlers present.

func (*Controller) AddHandler added in v0.19.0

func (c *Controller) AddHandler(class string, id string, button pixelgl.Button, sensitive bool, action func())

AddHandler will add the indicated type of handler to this control. `sensitive` will indicate if the JustPress method is used, which triggers the handler once. Otherwise the handler will act as a game button, allowing it to be held for repeated effect.

func (*Controller) Initialize

func (c *Controller) Initialize()

Initialize will setup any structure elements that require not being nil.

func (*Controller) RemoveHandler added in v0.19.0

func (c *Controller) RemoveHandler(class string, id string)

RemoveHandler will remove a handler from the provided handler list.

func (*Controller) Run

func (c *Controller) Run()

Run will loop through our controllers running any handlers that are setup.

type Default

type Default struct {
	XMLName    xml.Name   `xml:"default"`
	Scene      Scene      `xml:"scene"`
	Actor      Actor      `xml:"actor"`
	MessageBox MessageBox `xml:"messagebox"`
}

Default object values when not provided.

type Directory added in v0.19.0

type Directory struct {
	XMLName    xml.Name `xml:"directory"`
	Characters string   `xml:"characters,attr"`
}

Directory will set default directories not set elsewhere

type Engine added in v0.18.0

type Engine struct {

	// Config holds our main configuration values to run the system.
	Config *Configuration

	// PixelWindow is our graphics window configuration.
	PixelWindow pixelgl.WindowConfig

	// ScriptActions holds defined scripting actions.
	ScriptActions map[string]*ScriptAction

	// Font is our basic text atlas for system purposes.
	Font *text.Atlas

	// Scenes holds the various game scene contents.
	Scenes map[string]*Scene

	// ActiveScene is the currently running scene.
	ActiveScene *Scene

	// Actors holds the loaded actors for the game. They can be used across
	// scenes so it's not a good idea to tie them tightly to scenes. Scenes will
	// hold a list of actors that are visible or running on them.
	Actors map[string]*Actor

	// Control is the handlers that are loaded into the engine. Soon we should have global
	// control handlers, and scene independent handlers.
	Control *Controller

	// Logic is not used yet, but will be put in place to handle game logic functions that
	// should be run periodically, which are not tied to events.
	Logic func()

	// LastMove is the time of the last game cycle, used for managing game timing and motion.
	LastMove time.Time

	// Dt is used to calculate change in game cycle time, used for managing
	// game timing and motion.
	Dt float64
	// contains filtered or unexported fields
}

Engine is the core system that holds all running functionality.

func (*Engine) ActivateScene added in v0.18.0

func (e *Engine) ActivateScene(scene string)

ActivateScene will set the currently running scene.

func (*Engine) AddActor added in v0.18.0

func (e *Engine) AddActor(id string, actor *Actor)

AddActor will add an actor to the system.

func (*Engine) ConfigurePixel added in v0.18.0

func (e *Engine) ConfigurePixel()

ConfigurePixel will build up the pixel configuration from our game configuration. In this way if any window options are changed, we simply use ConfigurePixel to update the pixel window.

func (*Engine) CreateCoreActions added in v0.18.0

func (e *Engine) CreateCoreActions()

CreateCoreActions sets up the basic scripting actions that will always be included in the system.

func (*Engine) DisplayMessageBox added in v0.19.0

func (e *Engine) DisplayMessageBox(msg string)

DisplayMessageBox will display a message on screen and then wait for user input.

func (*Engine) GetScene added in v0.18.0

func (e *Engine) GetScene(id string) *Scene

GetScene should grab a scene for easy reference.

func (*Engine) Initialize added in v0.18.0

func (e *Engine) Initialize(file string)

Initialize starts up the RPG engine

func (*Engine) NewActor added in v0.18.0

func (e *Engine) NewActor(filename string, position pixel.Vec) *Actor

NewActor creates a new actor and returns it TODO: Allow for non image actors.

func (*Engine) NewScene added in v0.18.0

func (e *Engine) NewScene(id string, bgcolor string) error

NewScene will create a new scene. We use the already loaded configuration to initialize it. It should crash amazingly when there's no config loaded.

func (*Engine) Run added in v0.18.0

func (e *Engine) Run()

Run will run our main game processes

func (*Engine) RunScript added in v0.18.0

func (e *Engine) RunScript(script *Script) interface{}

RunScript will run a game script, by default using our game script collection.

func (*Engine) RunScriptAction added in v0.18.0

func (e *Engine) RunScriptAction(action *Action) interface{}

RunScriptAction will run the specified script action.

func (*Engine) RunScriptFile added in v0.18.0

func (e *Engine) RunScriptFile(file string)

RunScriptFile will load and run a script, presuming script directory and extension.

type Handler

type Handler struct {
	// ID will be a string we can use to identify a handler when we need to
	// remove it.
	ID string

	// The keypress we are checking
	Button pixelgl.Button

	// Sensitive will indicate if we JustPress...usually for menus
	Sensitive bool

	// The action to perform
	Action func()
}

Handler is our structure that we will create and add to the controller

type Map

type Map struct {
	// Basic map data as loaded from file
	Src *tiled.Map

	// Size will be the size of our map, pulled from our map data
	Size pixel.Vec

	// The rendered full map. We set this up as an array so that in future
	// we can process assorted layers independently if needed.
	Img []*pixel.PictureData

	// Our collision information, a collection of map objects.
	Collision []*pixel.Rect
}

Map will contain our map with some easy to use stuff, like for rendering

func NewMap

func NewMap(mapfile string) (*Map, error)

NewMap will load and initialize a map from a mapfile. If we need to change maps during a game, it makes sense to reset everything about a map so that we don't have any lingering artifacts. We will return errors in order to process them properly.

type MessageBox added in v0.18.0

type MessageBox struct {
	XMLName xml.Name `xml:"messagebox"`
	Color   string   `xml:"color,attr"`
	BGColor string   `xml:"bgcolor,attr"`
	X       float64  `xml:"x,attr"`
	Y       float64  `xml:"y,attr"`
	Height  float64  `xml:"height,attr"`
	Width   float64  `xml:"width,attr"`
}

MessageBox sets options for the system messagebox.

type Scene

type Scene struct {
	// XMLName is how we reference when loading xml information.
	XMLName xml.Name `xml:"scene"`

	// basespeed is the speed that this scene will run at.
	Basespeed float64 `xml:"basespeed,attr"`

	// Background is the background colour to clear this screen to.
	Background color.RGBA

	// Rendered is the canvas we draw to before flipping to screen.
	Rendered *pixelgl.Canvas

	// Views is the collection of views of the scene.
	Views map[string]*View

	// ViewOrder is the order the views are drawn down.
	ViewOrder []string

	// Actors is the collection of actors of the scene.
	Actors map[string]*Actor

	// MapData is the tiled data object.
	MapData *Map

	// Control is the collection of handlers specific to the scene.
	Control *Controller

	// Engine is the engine this scene belongs to.
	Engine *Engine
}

Scene holds the information for a combination of views and actors. A view should be able to have multiple actors and multiple outputs.

func (*Scene) CollisionFree

func (s *Scene) CollisionFree(clip pixel.Rect) bool

CollisionFree will indicate the space is free of collisions. It tests against the collision objects that are found in the map file.

func (*Scene) Contains

func (s *Scene) Contains(target pixel.Rect) bool

Contains will indicate if the rectangle is contained within this view's source map. Used for bounds checking against actors and the camera.

func (*Scene) Draw

func (s *Scene) Draw()

Draw will draw the scene out to the Engine win target. This should be the pixelgl.Window reference.

func (*Scene) GetView added in v0.19.0

func (s *Scene) GetView(id string) (*View, error)

GetView will return the requested view, if it exists.

func (*Scene) LoadActorsFromMapData added in v0.19.0

func (s *Scene) LoadActorsFromMapData()

LoadActorsFromMapData will return an array of actors that are present in the mapdata.

func (*Scene) LoadMap added in v0.19.0

func (s *Scene) LoadMap(file string) error

LoadMap will load a map into a scene. This needs to be called before we can start a map view.

func (*Scene) MoveActor

func (s *Scene) MoveActor(actor *Actor, direction int)

MoveActor will move an actor within the scene.

func (*Scene) NewView added in v0.18.0

func (s *Scene) NewView(id string, position pixel.Vec, camera pixel.Rect, bgcolor string)

NewView will create a new view and attach it to the scene.

func (*Scene) ProcessActorDestinations added in v0.16.4

func (s *Scene) ProcessActorDestinations()

ProcessActorDestinations will move the relevent actors towards their respective destinations

func (*Scene) RemoveView added in v0.18.0

func (s *Scene) RemoveView(id string)

RemoveView will destroy the view from the scene, also maintaining the vieworder.

func (*Scene) Render added in v0.18.0

func (s *Scene) Render()

Render will draw our views onto our scene canvas.

func (*Scene) SetBackground

func (s *Scene) SetBackground(bgcolor string)

SetBackground will set the background color of the scene.

func (*Scene) UseActor added in v0.19.0

func (s *Scene) UseActor(actor string)

UseActor will use the requested actor on this scene.

type Script

type Script struct {
	Actions []*Action
}

Script will hold a sequence or collection of commands. In theory this is is what a script file might get loaded into.

func NewScript added in v0.16.4

func NewScript() *Script

NewScript will return a new, empty script.

func (*Script) Add

func (s *Script) Add(action string, args ...interface{}) int

Add will add the given event onto the end of our collection. It will return the index of the added command. This is useful when maintaining a collection of custom actions as opposed to a sequence.

func (*Script) Load

func (s *Script) Load(file string, appendScript bool) error

Load will open up the requested script file and parse the actions into the script. We can either overwrite or append.

type ScriptAction

type ScriptAction struct {
	Action string
	Runner func([]interface{}) interface{}
}

ScriptAction will hold the implementation of script actions.

func NewScriptAction

func NewScriptAction(action string, runner func([]interface{}) interface{}) *ScriptAction

NewScriptAction will create and return a new ScriptAction.

type Scripting

type Scripting struct {
	XMLName   xml.Name `xml:"scripting"`
	Dir       string   `xml:"dir,attr"`
	Extension string   `xml:"extension,attr"`
}

Scripting sets customizable script options.

type System

type System struct {
	XMLName   xml.Name  `xml:"system"`
	Window    Window    `xml:"window"`
	Scripting Scripting `xml:"scripting"`
	Directory Directory `xml:"directory"`
}

System configuration setting.

type View

type View struct {
	// Visible indicates if the view should be rendered.
	Visible bool

	// Background is the background color of the view.
	Background color.RGBA

	// Src indicates the source picture data to draw from
	Src *pixel.PictureData

	// Focus is the actor that the view will follow. This actor will be
	// restricted to the bounds of the view.
	Focus *Actor

	// VisibleActors are the actors that are actually visible on this
	// view.
	VisibleActors []string

	// Output is the rendered map which is drawn to the screen.
	Output []*pixel.Sprite

	// Rendered is our background canvas to draw onto which will be
	// flipped to the screen.
	Rendered *pixelgl.Canvas

	// DesignView is the function that will be called to draw our view.
	// With consideration for views that don't focus on a map.
	DesignView func()

	// Position of our view on the window.
	Position pixel.Vec

	// Camera is the region of the map that is currently in view.
	// The camera also controls what is in display, period. So if we set it
	// once and don't change it, that's fine.
	Camera pixel.Rect

	// Speed is the speed modifier of this camera, not used yet.
	Speed float64

	// Scene will be the scene this view is a part of.
	Scene *Scene

	// Engine is passed through for ease of access
	Engine *Engine
}

View is the object that is rendered to the screen. It manages the associated graphics and actors, as well as any motion directly related to the view.

func (*View) CameraContains

func (v *View) CameraContains(target pixel.Rect) bool

CameraContains will ensure that the camera contains the given rectangle. Should be refactored soon too.

func (*View) CenterOn

func (v *View) CenterOn(movement pixel.Vec)

CenterOn will center the map on the actor who has the focus on that view.

func (*View) Draw

func (v *View) Draw()

Draw will draw the view to the scene, if it's visible.

func (*View) FocusOn

func (v *View) FocusOn(actor *Actor)

FocusOn will focus on a specific actor

func (*View) Hide

func (v *View) Hide()

Hide the view.

func (*View) Move

func (v *View) Move(position pixel.Vec)

Move will move view position within the window.

func (*View) Render

func (v *View) Render()

Render will setup the viewable portion of the view.

func (*View) SetBackground

func (v *View) SetBackground(bgcolor string)

SetBackground sets the background color of the view.

func (*View) Show

func (v *View) Show()

Show the view.

func (*View) Toggle

func (v *View) Toggle()

Toggle view

func (*View) UseMap added in v0.19.0

func (v *View) UseMap() error

UseMap will setup the view to use the scene map. It will error if there is no mapdata to use.

type Viewable

type Viewable interface {
	Show()
	Hide()
	Toggle()
	Render()
	Draw()
}

Viewable will be useful at some point.

type Window

type Window struct {
	XMLName xml.Name `xml:"window"`
	Width   float64  `xml:"width,attr"`
	Height  float64  `xml:"height,attr"`
	Title   string   `xml:"title,attr"`
}

Window is the options for starting pixel window.

Jump to

Keyboard shortcuts

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