systems

package
v0.0.0-...-2630cb0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DigSystem = func(e *ecs.ECS, win *pixelgl.Window) {
	ecs.BehaviorSystem(func(e *ecs.ECS, ev ecs.EventContainer, delta float64, entityID uint64, diggable eDiggable) {

		if win.Pressed(pixelgl.MouseButtonLeft) {
			mp := win.MousePosition()

			if mp.X > diggable.X+5 || mp.X < diggable.X-5 || mp.Y > diggable.Y+5 || mp.Y < diggable.Y-5 {
				return
			}

			diggable.Durability -= delta
			if diggable.Durability <= 0 {
				ev.Next <- ecs.EntityRemovedEvent{ID: entityID}
			}
		}
	})(e)
}

DigSystem provides the ability for the player to click over an entity and eventually break it.

View Source
var ParticleSystem = ecs.BehaviorSystem(func(e *ecs.ECS, ev ecs.EventContainer, delta float64, entityID uint64, particle eParticle) {

	particle.Lifetime -= delta
	if particle.Lifetime <= 0 {
		e.RemoveEntity(entityID)
	}

	ev.Next <- ApplyVelocityEvent{entityID, 0, -1500 * delta}
})

ParticleSystem deals with a very specific type of onscreen particle: A circle whose size decreases over time and which obeys some gravity.

View Source
var PlayerSystem = func(e *ecs.ECS, win *pixelgl.Window, pic *pixel.Picture) {
	ecs.BehaviorSystem(func(e *ecs.ECS, ev ecs.EventContainer, delta float64, entityID uint64, player ePlayer) {

		if player.Menu != nil {
			return
		}

		mousePos := win.MousePosition()
		playerPos := pixel.V(player.X, player.Y)
		diff := mousePos.To(playerPos).Unit().Rotated(math.Pi)
		player.Rotation = diff.Angle() - math.Pi/2

		// Apply velocity related to held arrow keys.
		var velX, velY float64
		if win.Pressed(pixelgl.KeyUp) {
			velY += 800 * delta
		}
		if win.Pressed(pixelgl.KeyDown) {
			velY -= 800 * delta
		}
		if win.Pressed(pixelgl.KeyLeft) {
			velX -= 800 * delta
		}
		if win.Pressed(pixelgl.KeyRight) {
			velX += 800 * delta
		}

		if win.JustPressed(pixelgl.MouseButtonLeft) {
			e.AddEntity(&Transform{X: player.X, Y: player.Y, Rotation: player.Rotation}, &Physics{VelX: diff.X * 200, VelY: diff.Y * 200, DragFactor: 1}, &Renderable{Sprite: pixel.NewSprite(*pic, pixel.R(69, 28, 69+8, 28+8))}, &Projectile{}, &Bullet{})
		}

		if velX != 0 || velY != 0 {
			ev.Next <- ApplyVelocityEvent{EntityID: entityID, VelX: velX, VelY: velY}
		}
	})(e)
}

PlayerSystem is a system which handles basic player controls

View Source
var ProjectileSystem = func(e *ecs.ECS, win *pixelgl.Window) {
	ecs.BehaviorSystem(func(e *ecs.ECS, ev ecs.EventContainer, delta float64, entityID uint64, projectile eProjectile) {
		if projectile.Y+projectile.VelY*delta < 20 {
			projectile.VelY = -projectile.VelY * 0.5
			projectile.Y = 20
			projectile.Bounces++
		}

		if projectile.Y+projectile.VelY*delta > win.Bounds().Max.Y-20 {
			projectile.VelY = -projectile.VelY
			projectile.Y = win.Bounds().Max.Y - 20
			projectile.Bounces++
		}

		if projectile.X+projectile.VelX*delta > win.Bounds().Max.X-20 {
			projectile.VelX = -projectile.VelX
			projectile.X = win.Bounds().Max.X - 20
			projectile.Bounces++
		}

		if projectile.X+projectile.VelX*delta < 20 {
			projectile.VelX = -projectile.VelX
			projectile.X = 20
			projectile.Bounces++
		}

		projectile.Rotation = pixel.V(projectile.VelX, projectile.VelY).Angle() + math.Pi/2

		if projectile.Bounces > 5 {
			e.RemoveEntity(entityID)
		}
	})(e)
}

ProjectileSystem handles projectile movement and rotation

Functions

func BalanceSystem

func BalanceSystem(e *ecs.ECS)

BalanceSystem handles wallets and balance change events, keeping track of in-game currency.

func BulletSystem

func BulletSystem(e *ecs.ECS)

BulletSystem handles gunshot collisions

func InteractiveSystem

func InteractiveSystem(e *ecs.ECS, win *pixelgl.Window)

InteractiveSystem handles interactive in-game menus.

func LoadPicture

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

From pixelGL tutorials

func PhysicsSystem

func PhysicsSystem(e *ecs.ECS, win *pixelgl.Window)

PhysicsSystem handles object physics (velocity, etc.)

func RenderSystem

func RenderSystem(e *ecs.ECS, win *pixelgl.Window, whenReady func())

RenderSystem is a system which draws to the screen. Unlike other systems, the RenderSystem does not run itself in a goroutine - because PixelGL requires rendering to occur in the main thread, RenderSystem takes it over and runs the passed whenReady function in a goroutine, where the user of the RenderSystem may continue setup.

func TransformSystem

func TransformSystem(e *ecs.ECS)

TransformSystem keeps track of the transformation of entities and parenting of entity positions to those of other entities.

Types

type ApplyVelocityEvent

type ApplyVelocityEvent struct {
	EntityID uint64
	VelX     float64
	VelY     float64
}

ApplyVelocityEvent is used to add instantaneous velocity to an entity.

type BalanceChangeEvent

type BalanceChangeEvent struct {
	ID     uint64
	Change int
}

BalanceChangeEvent represents a change in the balance of an entity's wallet.

type Bullet

type Bullet struct{}

Bullet is a component added to objects which can harm enemies upon collision.

type ChangeHUDPromptEvent

type ChangeHUDPromptEvent struct {
	ID     uint64
	Prompt string
}

ChangeHUDPromptEvent represents a request to change the prompt string of a HUDLine.

type Diggable

type Diggable struct {
	BaseDurability float64
	Durability     float64
}

Diggable is a component attached to objects which can be broken in a way which resembles mining in games like minecraft.

type Enemy

type Enemy struct {
	Health int
}

Enemy is a component added to objects which may take damage from bullets and eventually die.

type HUDLine

type HUDLine struct {
	Prompt   string  // The contents of the HUD line.
	Centered bool    // Whether or not the line is centered horizontally on the transform position.
	FontSize float64 // The font size as a multiplier.
	// contains filtered or unexported fields
}

HUDLine is a component which provides a line of text on-screen above all other content.

type InteractionMenu

type InteractionMenu struct {
	Prompt  string
	Choices []MenuChoice
}

InteractionMenu represents a menu with a prompt and several selectable options.

type Interactive

type Interactive struct {
	Prompt string                                    // The prompt line describes the action and trigger, e.g. "[space] Talk"
	Name   string                                    // The in-world name of the entity, e.g. "Jeff".
	Menu   func(ecs.EventContainer) *InteractionMenu // A function that performs some action and opens a menu.
}

Interactive is a component placed upon entities that can be interacted with by an interactor, resulting in some menu appearing.

type Interactor

type Interactor struct {
	InMenu            bool             // True if a menu is currently active.
	Menu              *InteractionMenu // A pointer to the currently active menu.
	NearbyInteractive uint64           // The ID of a nearby interactive, or 0 if nothing is in range
}

Interactor is a component placed upon entities that can interact with others, interrupting its flow with a menu.

type MenuChoice struct {
	Label  string
	Action func(ecs.EventContainer) *InteractionMenu
}

MenuChoice represents one choice in an interactive menu. It has a label to describe what the interactor is selecting / saying, and a function which performs some actions and then returns another menu, or nil to exit the menus. The Action function itself may also be nil to just exit when selected.

type Particle

type Particle struct {
	Lifetime float64
}

Particle is a component which labels an entity as being controlled by the particle system.

type Physics

type Physics struct {
	VelX       float64
	VelY       float64
	DragFactor float64
}

Physics is a component which specifies that an entity should be affected by the physics system.

type Player

type Player struct{}

Player is a component which signifies that an entity is the player.

type Projectile

type Projectile struct {
	Bounces int
}

Projectile is a component added to entities which fly around and bounce off walls, being removed after a particular number of bounces.

type Renderable

type Renderable struct {
	Sprite *pixel.Sprite
}

Renderable is a component which defines a colored circle to be drawn on-screen by the renderer.

type SetTransformParentEvent

type SetTransformParentEvent struct {
	EntityID uint64 // The entity whose parent should be changed.
	ParentID uint64 // The new parent for the entity.
}

SetTransformParentEvent changes which entity a transform is parented to. This does not change the current position of the entity.

type Transform

type Transform struct {
	X        float64
	Y        float64
	Rotation float64
	Width    float64
	Height   float64
	ParentID uint64 // This transform will follow all the same movements as its parent. Set to 0 for 'no parent'.
}

Transform is a component which represents the position of some entity.

type TransformEvent

type TransformEvent struct {
	EntityID uint64 // The entity to transform.
	OffsetX  float64
	OffsetY  float64
	Absolute bool // True if offsets are actually absolute screen coordinates.
}

TransformEvent represents a change in the position of an entity. This may then fire transform events for any transforms which use the given entity as a parent.

type Wallet

type Wallet struct {
	Balance int
}

Wallet is a component which stores the monetary balance of an entity.

Jump to

Keyboard shortcuts

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