cgame

package
v0.0.0-...-59848b6 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2022 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	InBoundsCheckPartiallyVisible = InBoundsCheckType(iota)
	InBoundsCheckFullyVisible
	InBoundsCheckNone
)
View Source
const (
	InBoundsCheckResultOK = InBoundsCheckResult(iota)
	InBoundsCheckResultN  // breach to the north
	InBoundsCheckResultE  // breach to the east
	InBoundsCheckResultS  // breach to the south
	InBoundsCheckResultW  // breach to the west
)
View Source
const (
	InBoundsCheckResponseAbandon = InBoundsCheckResponseType(iota)
	InBoundsCheckResponseJustDoIt
)
View Source
const (
	CollisionDetectionOn = CollisionDetectionType(iota)
	CollisionDetectionOff
)
View Source
const (
	CollisionResponseAbandon = CollisionResponseType(iota)
	CollisionResponseJustDoIt
)
View Source
const (
	CollidableRegistryMatchAll = "*"
)
View Source
const (
	InvalidSoundID = SoundID(-1)
)

Variables

This section is empty.

Functions

func CreateExplosion

func CreateExplosion(s Sprite, c ExplosionCfg)

func DetectCollision

func DetectCollision(r1 cwin.Rect, f1 Frame, r2 cwin.Rect, f2 Frame) bool

Be aware certain quirky situation: given this is a terminal/character based system it's possible to have two sprites actually cross/on top of each other without actually having any overlapping characters. Think the following simple example:

  • sprite 1 looks like this: \ \
  • sprite 2 looks like this: / /

They can be perfectly on top of each other/across, without causing an overlapping character situation:

\/
/\

Thus DetectCollision fails here. No good general solutions in mind yet, just need to be careful. For "enclosed" sprite, such as circle, one can fill the interior with characters not just TransparencyChx, that will alleviate the problem.

func FrameRect

func FrameRect(f Frame) cwin.Rect

func FrameToWin

func FrameToWin(f Frame, w cwin.Win)

func NewSimpleFrameProvider

func NewSimpleFrameProvider(frames Frames, t time.Duration, loop bool) *simpleFrameProvider

func NewWaypointProviderSimple

func NewWaypointProviderSimple(wps []Waypoint) *waypointProviderSimple

func NewWaypointProviderSimpleLoop

func NewWaypointProviderSimpleLoop(wps []Waypoint) *waypointProviderSimple

Types

type Animator

type Animator interface {
	Animate()
}

type AnimatorCfgCommon

type AnimatorCfgCommon struct {
	InBoundsCheckType        InBoundsCheckType      // partially visible check by default
	CollisionDetectionType   CollisionDetectionType // on by default
	KeepAliveWhenFinished    bool
	AfterUpdate, AfterFinish func()
}

type AnimatorFrame

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

func NewAnimatorFrame

func NewAnimatorFrame(s Sprite, c AnimatorFrameCfg) *AnimatorFrame

func (*AnimatorFrame) Animate

func (af *AnimatorFrame) Animate()

func (*AnimatorFrame) Cfg

func (af *AnimatorFrame) Cfg() AnimatorFrameCfg

type AnimatorFrameCfg

type AnimatorFrameCfg struct {
	Frames FrameProvider
	AnimatorCfgCommon
}

type AnimatorWaypoint

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

func NewAnimatorWaypoint

func NewAnimatorWaypoint(s Sprite, c AnimatorWaypointCfg) *AnimatorWaypoint

func (*AnimatorWaypoint) Animate

func (aw *AnimatorWaypoint) Animate()

func (*AnimatorWaypoint) Cfg

type AnimatorWaypointCfg

type AnimatorWaypointCfg struct {
	Waypoints WaypointProvider
	// instead of a "continuous" move, after the specified T of a waypoint, the move will be
	// completed in one go. In most cases, we like the smooth movement; but in some case, like
	// for example in tetris, we do want the tetris piece to drop in a discrete manner
	SingleMovePerWaypoint bool
	AnimatorCfgCommon
}

type Cell

type Cell struct {
	X, Y int
	Chx  cwin.Chx
}

type CharPerSec

type CharPerSec float64

type CharPerSecSec

type CharPerSecSec float64

type CollidableRegistry

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

func (*CollidableRegistry) Register

func (cd *CollidableRegistry) Register(spriteName1, spriteName2 string) *CollidableRegistry

supported:

  • name1, name2
  • name, name
  • name, * or *, name
  • *, *

func (*CollidableRegistry) RegisterBulk

func (cd *CollidableRegistry) RegisterBulk(
	spriteName1 string, otherSpriteNames []string) *CollidableRegistry

type CollisionDetectionType

type CollisionDetectionType int

type CollisionResponse

type CollisionResponse interface {
	CollisionNotify(initiator bool, collidedWith []Sprite) CollisionResponseType
}

type CollisionResponseType

type CollisionResponseType int

type Exchange

type Exchange struct {
	BoolData    map[interface{}]bool
	IntData     map[interface{}]int
	StringData  map[interface{}]string
	GenericData map[interface{}]interface{}
}

type ExplosionCfg

type ExplosionCfg struct {
	// required
	MaxDuration time.Duration

	// optional
	AfterFinish func()
	SpriteName  string
}

type Frame

type Frame []Cell

func CopyFrame

func CopyFrame(src Frame) Frame

func FrameFromString

func FrameFromString(s string, attr cwin.Attr) Frame

func FrameFromStringEx

func FrameFromStringEx(s string, attr cwin.Attr, spaceEqualsTransparency bool) Frame

func FrameFromWin

func FrameFromWin(w cwin.Win) Frame

func MultiColorFrameFromFile

func MultiColorFrameFromFile(filepath string, n int) (Frame, error)

func MultiColorFrameFromString

func MultiColorFrameFromString(s string, fg string, bg ...string) Frame

func SetAttrInFrame

func SetAttrInFrame(f Frame, attr cwin.Attr) Frame

type FrameProvider

type FrameProvider interface {
	Next() (Frame, time.Duration, bool)
}

type Frames

type Frames []Frame

func FramesFromString

func FramesFromString(ss []string, attr cwin.Attr) (Frames, cwin.Rect)

type Game

type Game struct {
	WinSys      *cwin.Sys
	MasterClock *cutil.Clock
	SpriteMgr   *SpriteManager
	SoundMgr    *SoundManager
	Exchange    *Exchange
	// contains filtered or unexported fields
}

func Init

func Init(provider cterm.Provider, seed ...int64) (*Game, error)

func (*Game) Close

func (g *Game) Close()

func (*Game) GameOver

func (g *Game) GameOver()

func (*Game) HeapUsageInBytes

func (g *Game) HeapUsageInBytes() int64

func (*Game) IsGameOver

func (g *Game) IsGameOver() bool

func (*Game) IsPaused

func (g *Game) IsPaused() bool

func (*Game) Pause

func (g *Game) Pause()

func (*Game) Resume

func (g *Game) Resume()

func (*Game) Run

func (g *Game) Run(
	gameOverKeys, pauseKeys []cterm.Event,
	gameEventHandler cwin.EventHandler,
	f ...cwin.EventLoopSleepDurationFunc)

type InBoundsCheckResponse

type InBoundsCheckResponse interface {
	InBoundsCheckNotify(result InBoundsCheckResult) InBoundsCheckResponseType
}

type InBoundsCheckResponseType

type InBoundsCheckResponseType int

type InBoundsCheckResult

type InBoundsCheckResult int

func InBoundsCheck

func InBoundsCheck(checkType InBoundsCheckType, r cwin.Rect, f Frame, parentR cwin.Rect) InBoundsCheckResult

type InBoundsCheckType

type InBoundsCheckType int

type SoundID

type SoundID int64

type SoundManager

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

func (*SoundManager) AvoidSameClipConcurrentPlaying

func (sm *SoundManager) AvoidSameClipConcurrentPlaying()

func (*SoundManager) Close

func (sm *SoundManager) Close()

func (*SoundManager) Init

func (sm *SoundManager) Init() error

func (*SoundManager) PauseAll

func (sm *SoundManager) PauseAll()

func (*SoundManager) PlayMP3

func (sm *SoundManager) PlayMP3(mp3FilePath string, vol float64, loop int) (SoundID, error)

func (*SoundManager) ResumeAll

func (sm *SoundManager) ResumeAll()

func (*SoundManager) Stop

func (sm *SoundManager) Stop(id SoundID)

type Sprite

type Sprite interface {
	Name() string

	// Base returns the embedded SpriteBase pointer for accessing its functionalities as well
	// as serving as an identity (by using its pointer address)
	Base() *SpriteBase
	// This returns the actual object that implements Sprite interface that is registered
	// with SpriteManager. Because SpriteBase implements Sprite interface, sometimes we go
	// into situation where a SpriteBase pointer is getting passed around but eventually
	// when deverloper wants to cast back to their own object (which embeds SpriteBase) they
	// get type assertion failure. As long as the object (that implements Sprite) passed into
	// SpriteManager.AddSprite is the "top-level" object, This() will always return that
	// registered object. Note if calling This() on a non SpriteManager managed (i.e. not
	// added with SpriteManager.AddSprite) object, it will panic.
	This() Sprite

	Mgr() *SpriteManager
	Game() *Game

	Rect() cwin.Rect
	ParentRect() cwin.Rect
	Frame() Frame
	Animators() []Animator

	Destroy()

	fmt.Stringer
}

type SpriteBase

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

func NewSpriteBase

func NewSpriteBase(g *Game, parent cwin.Win, name string, f Frame, x, y int) *SpriteBase

func NewSpriteBaseR

func NewSpriteBaseR(g *Game, parent cwin.Win, name string, f Frame, r cwin.Rect) *SpriteBase

func (*SpriteBase) AddAnimator

func (sb *SpriteBase) AddAnimator(as ...Animator)

func (*SpriteBase) Animators

func (sb *SpriteBase) Animators() []Animator

func (*SpriteBase) Base

func (sb *SpriteBase) Base() *SpriteBase

func (*SpriteBase) DeleteAnimator

func (sb *SpriteBase) DeleteAnimator(as ...Animator)

func (*SpriteBase) Destroy

func (sb *SpriteBase) Destroy()

func (*SpriteBase) Frame

func (sb *SpriteBase) Frame() Frame

func (*SpriteBase) Game

func (sb *SpriteBase) Game() *Game

func (*SpriteBase) IsDestroyed

func (sb *SpriteBase) IsDestroyed() bool

func (*SpriteBase) Mgr

func (sb *SpriteBase) Mgr() *SpriteManager

func (*SpriteBase) Name

func (sb *SpriteBase) Name() string

func (*SpriteBase) ParentRect

func (sb *SpriteBase) ParentRect() cwin.Rect

func (*SpriteBase) Rect

func (sb *SpriteBase) Rect() cwin.Rect

func (*SpriteBase) SendToBottom

func (sb *SpriteBase) SendToBottom()

func (*SpriteBase) SendToTop

func (sb *SpriteBase) SendToTop()

func (*SpriteBase) String

func (sb *SpriteBase) String() string

func (*SpriteBase) This

func (sb *SpriteBase) This() Sprite

func (*SpriteBase) Update

func (sb *SpriteBase) Update(arg UpdateArg) bool

returns false if any of the tests (bounds, collision) fails; true otherwise. Important, do not call Update with IBC/CD turned on from your InBoundsCheckResponse.Notify or CollisionResponse.Notify or it might cause infinite recursion.

type SpriteManager

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

func (*SpriteManager) AddSprite

func (sm *SpriteManager) AddSprite(s Sprite)

func (*SpriteManager) CheckCollision

func (sm *SpriteManager) CheckCollision(
	collider Sprite, colliderR cwin.Rect, colliderF Frame) []Sprite

CheckCollision does the collision detection with the collider against all the managed Sprites using the collider's Rect and Frame. Note that colliderR and colliderF are not necessarily the same as the collider's current position and frame.

func (*SpriteManager) CollidableRegistry

func (sm *SpriteManager) CollidableRegistry() *CollidableRegistry

func (*SpriteManager) DbgStats

func (sm *SpriteManager) DbgStats() string

func (*SpriteManager) DeleteAll

func (sm *SpriteManager) DeleteAll()

func (*SpriteManager) DeleteSprite

func (sm *SpriteManager) DeleteSprite(s Sprite)

func (*SpriteManager) Find

func (sm *SpriteManager) Find(s Sprite) Sprite

Find returns the unique sprite managed by the SpriteManager that is identified by its SpriteBase. If no Sprite is found, panic.

func (*SpriteManager) FindByName

func (sm *SpriteManager) FindByName(name string) Sprite

FindByName returns the first sprite managed by the SpriteManager that has the same name. If no match is found, panic.

func (*SpriteManager) Process

func (sm *SpriteManager) Process()

func (*SpriteManager) Sprites

func (sm *SpriteManager) Sprites() []Sprite

func (*SpriteManager) TryFind

func (sm *SpriteManager) TryFind(s Sprite) (Sprite, bool)

TryFind returns the unique sprite managed by the SpriteManager that is identified by its SpriteBase.

func (*SpriteManager) TryFindByName

func (sm *SpriteManager) TryFindByName(name string) (Sprite, bool)

TryFindByName returns the first sprite managed by the SpriteManager that has the same name.

type UpdateArg

type UpdateArg struct {
	DXY      *cwin.Point            // update the sprite position by (dx,dy), if non nil.
	F        Frame                  // If nil, no frame update; If empty (len=0), frame will be wiped clean
	IBC      InBoundsCheckType      // default to no in-bounds check.
	CD       CollisionDetectionType // default to collision detection
	TestOnly bool                   // if true, do the tests only, no actual update or notifications
}

type Waypoint

type Waypoint struct {
	DX, DY int
	T      time.Duration
}

From current position to (curX + DX, curY + DY) using time T. Note a trick here is to use DX=DY=0 to keep the sprite at the current location for time T.

type WaypointProvider

type WaypointProvider interface {
	Next() (Waypoint, bool)
}

type WaypointProviderAcceleration

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

func (*WaypointProviderAcceleration) Cfg

func (*WaypointProviderAcceleration) CurSpeed

func (s *WaypointProviderAcceleration) CurSpeed() (xSpeed, ySpeed CharPerSec)

func (*WaypointProviderAcceleration) Next

type WaypointProviderAccelerationCfg

type WaypointProviderAccelerationCfg struct {
	Clock      *cutil.Clock
	InitXSpeed CharPerSec
	InitYSpeed CharPerSec
	AccX       CharPerSecSec
	AccY       CharPerSecSec
	DeltaT     time.Duration // if 0, defaults to defaultDeltaT
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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