2019-04-10 02:17:56 +00:00
|
|
|
package uix
|
|
|
|
|
|
|
|
import (
|
2019-04-19 01:15:05 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-04-03 06:09:46 +00:00
|
|
|
"sort"
|
|
|
|
"sync"
|
2019-04-19 01:15:05 +00:00
|
|
|
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/collision"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/doodads"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/level"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/physics"
|
2019-12-28 03:16:34 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2022-01-17 04:09:27 +00:00
|
|
|
"github.com/dop251/goja"
|
2019-12-22 22:11:01 +00:00
|
|
|
"github.com/google/uuid"
|
2019-04-10 02:17:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Actor is an object that marries together the three things that make a
|
|
|
|
// Doodad instance "tick" while inside a Canvas:
|
|
|
|
//
|
|
|
|
// - uix.Actor is a doodads.Drawing so it fulfills doodads.Actor to be a
|
|
|
|
// dynamic object during gameplay.
|
|
|
|
// - It has a pointer to the level.Actor indicating its static level data
|
|
|
|
// as defined in the map: its spawn coordinate and configuration.
|
|
|
|
// - A uix.Canvas that can present the actor's graphics to the screen.
|
|
|
|
type Actor struct {
|
2020-04-05 04:00:32 +00:00
|
|
|
Drawing *doodads.Drawing
|
|
|
|
Actor *level.Actor
|
|
|
|
Canvas *Canvas
|
2019-04-19 01:15:05 +00:00
|
|
|
|
|
|
|
activeLayer int // active drawing frame for display
|
|
|
|
flagDestroy bool // flag the actor for destruction
|
2021-01-03 23:19:21 +00:00
|
|
|
flagUsing bool // flag that the (player) has pressed the Use key.
|
2019-05-05 23:32:30 +00:00
|
|
|
|
2019-05-06 02:04:02 +00:00
|
|
|
// Actor runtime variables.
|
Thief and Inventory APIs
This commit adds the Thief character with starter graphics
(no animations).
The Thief walks back and forth and will steal items from other
doodads, including the player. For singleton items that have no
quantity, like the Colored Keys, the Thief will only steal one
if he does not already have it. Quantitied items like the
Small Key are always stolen.
Flexibility in the playable character is introduced: Boy,
Azulian, Bird, and Thief all respond to playable controls.
There is not currently a method to enable these apart from
modifying balance.PlayerCharacterDoodad at compile time.
New and Changed Doodads
* Thief: new doodad that walks back and forth and will steal
items from other characters inventory.
* Bird: has no inventory and cannot pick up items, unless player
controlled. Its hitbox has also been fixed so it collides with
floors correctly - not something normally seen in the Bird.
* Boy: opts in to have inventory.
* Keys (all): only gives themselves to actors having inventories.
JavaScript API - New functions available
* Self.IsPlayer() - returns if the current actor IS the player.
* Self.SetInventory(bool) - doodads must opt-in to having an
inventory. Keys should only give themselves to doodads having
an inventory.
* Self.HasInventory() bool
* Self.AddItem(filename, qty)
* Self.RemoveItem(filename, qty)
* Self.HasItem(filename)
* Self.Inventory() - returns map[string]int
* Self.ClearInventory()
* Self.OnLeave(func(e)) now receives a CollideEvent as parameter
instead of the useless actor ID. Notably, e.Actor is the
leaving actor and e.Settled is always true.
Other Changes
* Play Mode: if playing as a character which doesn't obey gravity,
such as the bird, antigravity controls are enabled by default.
If you `import antigravity` you can turn gravity back on.
* Doodad collision scripts are no longer run in parallel
goroutines. It made the Thief's job difficult trying to steal
items in many threads simultaneously!
2021-08-10 05:42:22 +00:00
|
|
|
hasGravity bool
|
|
|
|
hasInventory bool
|
2022-05-06 04:35:32 +00:00
|
|
|
wet bool
|
Thief and Inventory APIs
This commit adds the Thief character with starter graphics
(no animations).
The Thief walks back and forth and will steal items from other
doodads, including the player. For singleton items that have no
quantity, like the Colored Keys, the Thief will only steal one
if he does not already have it. Quantitied items like the
Small Key are always stolen.
Flexibility in the playable character is introduced: Boy,
Azulian, Bird, and Thief all respond to playable controls.
There is not currently a method to enable these apart from
modifying balance.PlayerCharacterDoodad at compile time.
New and Changed Doodads
* Thief: new doodad that walks back and forth and will steal
items from other characters inventory.
* Bird: has no inventory and cannot pick up items, unless player
controlled. Its hitbox has also been fixed so it collides with
floors correctly - not something normally seen in the Bird.
* Boy: opts in to have inventory.
* Keys (all): only gives themselves to actors having inventories.
JavaScript API - New functions available
* Self.IsPlayer() - returns if the current actor IS the player.
* Self.SetInventory(bool) - doodads must opt-in to having an
inventory. Keys should only give themselves to doodads having
an inventory.
* Self.HasInventory() bool
* Self.AddItem(filename, qty)
* Self.RemoveItem(filename, qty)
* Self.HasItem(filename)
* Self.Inventory() - returns map[string]int
* Self.ClearInventory()
* Self.OnLeave(func(e)) now receives a CollideEvent as parameter
instead of the useless actor ID. Notably, e.Actor is the
leaving actor and e.Settled is always true.
Other Changes
* Play Mode: if playing as a character which doesn't obey gravity,
such as the bird, antigravity controls are enabled by default.
If you `import antigravity` you can turn gravity back on.
* Doodad collision scripts are no longer run in parallel
goroutines. It made the Thief's job difficult trying to steal
items in many threads simultaneously!
2021-08-10 05:42:22 +00:00
|
|
|
isMobile bool // Mobile character, such as the player or an enemy
|
|
|
|
noclip bool // Disable collision detection
|
|
|
|
hidden bool // invisible, via Hide() and Show()
|
|
|
|
frozen bool // Frozen, via Freeze() and Unfreeze()
|
2022-02-20 19:48:36 +00:00
|
|
|
immortal bool // Invulnerable to damage
|
Thief and Inventory APIs
This commit adds the Thief character with starter graphics
(no animations).
The Thief walks back and forth and will steal items from other
doodads, including the player. For singleton items that have no
quantity, like the Colored Keys, the Thief will only steal one
if he does not already have it. Quantitied items like the
Small Key are always stolen.
Flexibility in the playable character is introduced: Boy,
Azulian, Bird, and Thief all respond to playable controls.
There is not currently a method to enable these apart from
modifying balance.PlayerCharacterDoodad at compile time.
New and Changed Doodads
* Thief: new doodad that walks back and forth and will steal
items from other characters inventory.
* Bird: has no inventory and cannot pick up items, unless player
controlled. Its hitbox has also been fixed so it collides with
floors correctly - not something normally seen in the Bird.
* Boy: opts in to have inventory.
* Keys (all): only gives themselves to actors having inventories.
JavaScript API - New functions available
* Self.IsPlayer() - returns if the current actor IS the player.
* Self.SetInventory(bool) - doodads must opt-in to having an
inventory. Keys should only give themselves to doodads having
an inventory.
* Self.HasInventory() bool
* Self.AddItem(filename, qty)
* Self.RemoveItem(filename, qty)
* Self.HasItem(filename)
* Self.Inventory() - returns map[string]int
* Self.ClearInventory()
* Self.OnLeave(func(e)) now receives a CollideEvent as parameter
instead of the useless actor ID. Notably, e.Actor is the
leaving actor and e.Settled is always true.
Other Changes
* Play Mode: if playing as a character which doesn't obey gravity,
such as the bird, antigravity controls are enabled by default.
If you `import antigravity` you can turn gravity back on.
* Doodad collision scripts are no longer run in parallel
goroutines. It made the Thief's job difficult trying to steal
items in many threads simultaneously!
2021-08-10 05:42:22 +00:00
|
|
|
hitbox render.Rect
|
|
|
|
inventory map[string]int // item inventory. doodad name -> quantity, 0 for key item.
|
|
|
|
data map[string]string // arbitrary key/value store. DEPRECATED ??
|
2019-05-06 02:04:02 +00:00
|
|
|
|
2020-04-05 04:00:32 +00:00
|
|
|
// Movement data.
|
|
|
|
position render.Point
|
|
|
|
velocity physics.Vector
|
|
|
|
grounded bool
|
|
|
|
|
2019-05-05 23:32:30 +00:00
|
|
|
// Animation variables.
|
|
|
|
animations map[string]*Animation
|
|
|
|
activeAnimation *Animation
|
2022-01-17 04:09:27 +00:00
|
|
|
animationCallback goja.Value
|
2020-04-03 06:09:46 +00:00
|
|
|
|
|
|
|
// Mutex.
|
|
|
|
muInventory sync.RWMutex
|
|
|
|
muData sync.RWMutex
|
2019-04-10 02:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewActor sets up a uix.Actor.
|
|
|
|
// If the id is blank, a new UUIDv4 is generated.
|
|
|
|
func NewActor(id string, levelActor *level.Actor, doodad *doodads.Doodad) *Actor {
|
|
|
|
if id == "" {
|
2021-08-12 03:40:31 +00:00
|
|
|
id = uuid.Must(uuid.NewUUID()).String()
|
2019-04-10 02:17:56 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 03:16:34 +00:00
|
|
|
size := doodad.Layers[0].Chunker.Size
|
2019-04-10 02:17:56 +00:00
|
|
|
can := NewCanvas(int(size), false)
|
|
|
|
can.Name = id
|
|
|
|
|
|
|
|
// TODO: if the Background is render.Invisible it gets defaulted to
|
|
|
|
// White somewhere and the Doodad masks the level drawing behind it.
|
|
|
|
can.SetBackground(render.RGBA(0, 0, 1, 0))
|
|
|
|
|
|
|
|
can.LoadDoodad(doodad)
|
|
|
|
can.Resize(render.NewRect(size, size))
|
|
|
|
|
2019-04-14 22:25:03 +00:00
|
|
|
actor := &Actor{
|
2019-05-05 23:32:30 +00:00
|
|
|
Drawing: doodads.NewDrawing(id, doodad),
|
|
|
|
Actor: levelActor,
|
|
|
|
Canvas: can,
|
|
|
|
animations: map[string]*Animation{},
|
2020-04-03 06:09:46 +00:00
|
|
|
inventory: map[string]int{},
|
2019-04-10 02:17:56 +00:00
|
|
|
}
|
2019-04-14 22:25:03 +00:00
|
|
|
|
|
|
|
// Give the Canvas a pointer to its (parent) Actor so it can draw its debug
|
|
|
|
// label and show the World Position of the actor within the world.
|
|
|
|
can.actor = actor
|
|
|
|
|
|
|
|
return actor
|
2019-04-10 02:17:56 +00:00
|
|
|
}
|
2019-04-19 01:15:05 +00:00
|
|
|
|
2020-04-05 04:00:32 +00:00
|
|
|
// ID returns the actor's ID. This is the underlying doodle.Drawing.ID().
|
|
|
|
func (a *Actor) ID() string {
|
|
|
|
return a.Drawing.ID()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doodad offers access to the underlying Doodad object.
|
|
|
|
// Shortcut to the `.Drawing.Doodad` property path.
|
|
|
|
func (a *Actor) Doodad() *doodads.Doodad {
|
|
|
|
return a.Drawing.Doodad
|
|
|
|
}
|
|
|
|
|
2019-05-06 02:04:02 +00:00
|
|
|
// SetGravity configures whether the actor is affected by gravity.
|
|
|
|
func (a *Actor) SetGravity(v bool) {
|
|
|
|
a.hasGravity = v
|
|
|
|
}
|
|
|
|
|
2019-12-31 02:13:28 +00:00
|
|
|
// SetMobile configures whether the actor is a mobile character (i.e. is the
|
|
|
|
// player or a mobile enemy). Mobile characters can set off certain traps when
|
|
|
|
// touched but non-mobile actors don't set each other off if touching.
|
|
|
|
func (a *Actor) SetMobile(v bool) {
|
|
|
|
a.isMobile = v
|
|
|
|
}
|
|
|
|
|
Thief and Inventory APIs
This commit adds the Thief character with starter graphics
(no animations).
The Thief walks back and forth and will steal items from other
doodads, including the player. For singleton items that have no
quantity, like the Colored Keys, the Thief will only steal one
if he does not already have it. Quantitied items like the
Small Key are always stolen.
Flexibility in the playable character is introduced: Boy,
Azulian, Bird, and Thief all respond to playable controls.
There is not currently a method to enable these apart from
modifying balance.PlayerCharacterDoodad at compile time.
New and Changed Doodads
* Thief: new doodad that walks back and forth and will steal
items from other characters inventory.
* Bird: has no inventory and cannot pick up items, unless player
controlled. Its hitbox has also been fixed so it collides with
floors correctly - not something normally seen in the Bird.
* Boy: opts in to have inventory.
* Keys (all): only gives themselves to actors having inventories.
JavaScript API - New functions available
* Self.IsPlayer() - returns if the current actor IS the player.
* Self.SetInventory(bool) - doodads must opt-in to having an
inventory. Keys should only give themselves to doodads having
an inventory.
* Self.HasInventory() bool
* Self.AddItem(filename, qty)
* Self.RemoveItem(filename, qty)
* Self.HasItem(filename)
* Self.Inventory() - returns map[string]int
* Self.ClearInventory()
* Self.OnLeave(func(e)) now receives a CollideEvent as parameter
instead of the useless actor ID. Notably, e.Actor is the
leaving actor and e.Settled is always true.
Other Changes
* Play Mode: if playing as a character which doesn't obey gravity,
such as the bird, antigravity controls are enabled by default.
If you `import antigravity` you can turn gravity back on.
* Doodad collision scripts are no longer run in parallel
goroutines. It made the Thief's job difficult trying to steal
items in many threads simultaneously!
2021-08-10 05:42:22 +00:00
|
|
|
// SetInventory configures whether the actor is capable of carrying items.
|
|
|
|
func (a *Actor) SetInventory(v bool) {
|
|
|
|
a.hasInventory = true
|
|
|
|
}
|
|
|
|
|
2019-12-31 02:13:28 +00:00
|
|
|
// IsMobile returns whether the actor is a mobile character.
|
|
|
|
func (a *Actor) IsMobile() bool {
|
|
|
|
return a.isMobile
|
|
|
|
}
|
|
|
|
|
2020-12-30 04:31:35 +00:00
|
|
|
// IsPlayer returns whether the actor is the player character.
|
|
|
|
// It's true when the Actor ID is "PLAYER"
|
|
|
|
func (a *Actor) IsPlayer() bool {
|
|
|
|
return a.Canvas.Name == "PLAYER"
|
|
|
|
}
|
|
|
|
|
Thief and Inventory APIs
This commit adds the Thief character with starter graphics
(no animations).
The Thief walks back and forth and will steal items from other
doodads, including the player. For singleton items that have no
quantity, like the Colored Keys, the Thief will only steal one
if he does not already have it. Quantitied items like the
Small Key are always stolen.
Flexibility in the playable character is introduced: Boy,
Azulian, Bird, and Thief all respond to playable controls.
There is not currently a method to enable these apart from
modifying balance.PlayerCharacterDoodad at compile time.
New and Changed Doodads
* Thief: new doodad that walks back and forth and will steal
items from other characters inventory.
* Bird: has no inventory and cannot pick up items, unless player
controlled. Its hitbox has also been fixed so it collides with
floors correctly - not something normally seen in the Bird.
* Boy: opts in to have inventory.
* Keys (all): only gives themselves to actors having inventories.
JavaScript API - New functions available
* Self.IsPlayer() - returns if the current actor IS the player.
* Self.SetInventory(bool) - doodads must opt-in to having an
inventory. Keys should only give themselves to doodads having
an inventory.
* Self.HasInventory() bool
* Self.AddItem(filename, qty)
* Self.RemoveItem(filename, qty)
* Self.HasItem(filename)
* Self.Inventory() - returns map[string]int
* Self.ClearInventory()
* Self.OnLeave(func(e)) now receives a CollideEvent as parameter
instead of the useless actor ID. Notably, e.Actor is the
leaving actor and e.Settled is always true.
Other Changes
* Play Mode: if playing as a character which doesn't obey gravity,
such as the bird, antigravity controls are enabled by default.
If you `import antigravity` you can turn gravity back on.
* Doodad collision scripts are no longer run in parallel
goroutines. It made the Thief's job difficult trying to steal
items in many threads simultaneously!
2021-08-10 05:42:22 +00:00
|
|
|
// HasInventory returns if the actor is capable of carrying items.
|
|
|
|
func (a *Actor) HasInventory() bool {
|
|
|
|
return a.hasInventory
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasGravity returns if gravity applies to the actor.
|
|
|
|
func (a *Actor) HasGravity() bool {
|
|
|
|
return a.hasGravity
|
|
|
|
}
|
|
|
|
|
2022-02-20 19:48:36 +00:00
|
|
|
// Invulnerable returns whether the actor is marked as immortal.
|
|
|
|
func (a *Actor) Invulnerable() bool {
|
|
|
|
return a.immortal
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetInvulnerable sets the actor's immortal flag.
|
|
|
|
func (a *Actor) SetInvulnerable(v bool) {
|
|
|
|
a.immortal = v
|
|
|
|
}
|
|
|
|
|
2022-05-06 04:35:32 +00:00
|
|
|
// Wet returns whether the actor is in contact with water pixels in a level.
|
|
|
|
func (a *Actor) IsWet() bool {
|
|
|
|
return a.wet
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWet updates the state of the actor's wet-ness.
|
|
|
|
func (a *Actor) SetWet(v bool) {
|
|
|
|
a.wet = v
|
|
|
|
}
|
|
|
|
|
2020-04-05 04:00:32 +00:00
|
|
|
// Size returns the size of the actor, from the underlying doodads.Drawing.
|
|
|
|
func (a *Actor) Size() render.Rect {
|
|
|
|
return a.Drawing.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Velocity returns the actor's current velocity vector.
|
|
|
|
func (a *Actor) Velocity() physics.Vector {
|
|
|
|
return a.velocity
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVelocity updates the actor's velocity vector.
|
|
|
|
func (a *Actor) SetVelocity(v physics.Vector) {
|
|
|
|
a.velocity = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position returns the actor's position.
|
|
|
|
func (a *Actor) Position() render.Point {
|
|
|
|
return a.position
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoveTo sets the actor's position.
|
|
|
|
func (a *Actor) MoveTo(p render.Point) {
|
|
|
|
a.position = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoveBy adjusts the actor's position.
|
|
|
|
func (a *Actor) MoveBy(p render.Point) {
|
|
|
|
a.position.Add(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grounded returns if the actor is touching a floor.
|
|
|
|
func (a *Actor) Grounded() bool {
|
|
|
|
return a.grounded
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGrounded sets the actor's grounded value.
|
|
|
|
func (a *Actor) SetGrounded(v bool) {
|
|
|
|
a.grounded = v
|
|
|
|
}
|
|
|
|
|
2020-12-30 04:31:35 +00:00
|
|
|
// Hide makes the actor invisible.
|
|
|
|
func (a *Actor) Hide() {
|
|
|
|
a.hidden = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show a hidden actor.
|
|
|
|
func (a *Actor) Show() {
|
|
|
|
a.hidden = false
|
|
|
|
}
|
|
|
|
|
2021-01-03 23:19:21 +00:00
|
|
|
// Freeze an actor. For the player character, this means arrow key inputs
|
|
|
|
// will stop moving the actor.
|
|
|
|
func (a *Actor) Freeze() {
|
|
|
|
a.frozen = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unfreeze an actor.
|
|
|
|
func (a *Actor) Unfreeze() {
|
|
|
|
a.frozen = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsFrozen returns true if the actor is frozen.
|
|
|
|
func (a *Actor) IsFrozen() bool {
|
|
|
|
return a.frozen
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetUsing enables the "Use Key" flag, mainly for the player character to activate
|
|
|
|
// certain doodads in the level.
|
|
|
|
func (a *Actor) SetUsing(v bool) {
|
|
|
|
a.flagUsing = v
|
|
|
|
}
|
|
|
|
|
2020-01-03 04:23:27 +00:00
|
|
|
// SetNoclip sets the noclip setting for an actor. If true, the actor can
|
|
|
|
// clip through level geometry.
|
|
|
|
func (a *Actor) SetNoclip(v bool) {
|
|
|
|
a.noclip = v
|
|
|
|
}
|
|
|
|
|
2020-04-03 06:09:46 +00:00
|
|
|
// AddItem adds an item doodad to the actor's inventory.
|
|
|
|
// Item name is usually the doodad filename.
|
|
|
|
func (a *Actor) AddItem(itemName string, quantity int) {
|
|
|
|
a.muInventory.Lock()
|
2021-01-04 01:06:33 +00:00
|
|
|
if _, ok := a.inventory[itemName]; ok {
|
|
|
|
a.inventory[itemName] += quantity
|
|
|
|
} else {
|
|
|
|
a.inventory[itemName] = quantity
|
|
|
|
}
|
2020-04-03 06:09:46 +00:00
|
|
|
a.muInventory.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveItem removes a quantity of an item from the actor's inventory.
|
|
|
|
//
|
|
|
|
// Provide a quantity of 0 to remove the item completely.
|
|
|
|
// Otherwise provides a number greater than zero and you will subtract this
|
|
|
|
// quantity from the item. If the item then is at <= zero, it is removed from
|
|
|
|
// inventory.
|
|
|
|
func (a *Actor) RemoveItem(itemName string, quantity int) bool {
|
|
|
|
a.muInventory.RLock()
|
|
|
|
defer a.muInventory.RUnlock()
|
|
|
|
|
|
|
|
if _, ok := a.inventory[itemName]; ok {
|
|
|
|
// If quantity is zero, remove the item entirely.
|
|
|
|
if quantity <= 0 {
|
|
|
|
delete(a.inventory, itemName)
|
|
|
|
} else {
|
|
|
|
// Subtract the quantity from inventory. If we have run down to
|
|
|
|
// zero left, remove the item entirely.
|
|
|
|
a.inventory[itemName] -= quantity
|
|
|
|
if a.inventory[itemName] <= 0 {
|
|
|
|
delete(a.inventory, itemName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-07 06:21:17 +00:00
|
|
|
// ClearInventory removes all items from the actor's inventory.
|
|
|
|
func (a *Actor) ClearInventory() {
|
|
|
|
a.muInventory.Lock()
|
|
|
|
a.inventory = map[string]int{}
|
|
|
|
a.muInventory.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-04-03 06:09:46 +00:00
|
|
|
// HasItem checks the actor's inventory for the item and returns the quantity.
|
|
|
|
//
|
|
|
|
// A return value of -1 means the item was not found.
|
|
|
|
// The value 0 indicates a key item (one with no quantity).
|
|
|
|
// Values >= 1 would be consumable items.
|
|
|
|
func (a *Actor) HasItem(itemName string) int {
|
|
|
|
a.muInventory.RLock()
|
|
|
|
defer a.muInventory.RUnlock()
|
|
|
|
|
|
|
|
if quantity, ok := a.inventory[itemName]; ok {
|
|
|
|
return quantity
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListItems returns a sorted list of the items in the actor's inventory.
|
|
|
|
func (a *Actor) ListItems() []string {
|
|
|
|
a.muInventory.RLock()
|
|
|
|
defer a.muInventory.RUnlock()
|
|
|
|
|
|
|
|
var (
|
|
|
|
result = make([]string, len(a.inventory))
|
|
|
|
i = 0
|
|
|
|
)
|
|
|
|
for k := range a.inventory {
|
|
|
|
result[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(result)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inventory returns a copy of the actor's inventory struct.
|
|
|
|
func (a *Actor) Inventory() map[string]int {
|
|
|
|
a.muInventory.RLock()
|
|
|
|
defer a.muInventory.RUnlock()
|
|
|
|
|
|
|
|
var result = map[string]int{}
|
|
|
|
for k, v := range a.inventory {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-05-07 05:57:32 +00:00
|
|
|
// GetBoundingRect gets the bounding box of the actor's doodad.
|
|
|
|
func (a *Actor) GetBoundingRect() render.Rect {
|
2020-04-05 04:00:32 +00:00
|
|
|
return collision.GetBoundingRect(a)
|
2019-05-07 05:57:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 04:43:30 +00:00
|
|
|
// SetHitbox sets the actor's elected hitbox.
|
|
|
|
func (a *Actor) SetHitbox(x, y, w, h int) {
|
|
|
|
a.hitbox = render.Rect{
|
2019-12-28 03:16:34 +00:00
|
|
|
X: x,
|
|
|
|
Y: y,
|
|
|
|
W: w,
|
|
|
|
H: h,
|
2019-05-29 04:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:39:44 +00:00
|
|
|
// Hitbox returns the actor's elected hitbox. If the JavaScript did not set
|
|
|
|
// a hitbox, it defers to the Doodad's metadata hitbox.
|
2019-05-29 04:43:30 +00:00
|
|
|
func (a *Actor) Hitbox() render.Rect {
|
2021-09-04 03:39:44 +00:00
|
|
|
if a.hitbox.IsZero() && !a.Drawing.Doodad.Hitbox.IsZero() {
|
|
|
|
return a.Drawing.Doodad.Hitbox
|
|
|
|
}
|
2019-05-29 04:43:30 +00:00
|
|
|
return a.hitbox
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetData sets an arbitrary field in the actor's K/V storage.
|
|
|
|
func (a *Actor) SetData(key, value string) {
|
|
|
|
if a.data == nil {
|
|
|
|
a.data = map[string]string{}
|
|
|
|
}
|
2020-04-03 06:09:46 +00:00
|
|
|
|
|
|
|
a.muData.Lock()
|
2019-05-29 04:43:30 +00:00
|
|
|
a.data[key] = value
|
2020-04-03 06:09:46 +00:00
|
|
|
a.muData.Unlock()
|
2019-05-29 04:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetData gets an arbitrary field from the actor's K/V storage.
|
|
|
|
// Missing keys just return a blank string (friendly to the JavaScript
|
|
|
|
// environment).
|
|
|
|
func (a *Actor) GetData(key string) string {
|
|
|
|
if a.data == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-04-03 06:09:46 +00:00
|
|
|
a.muData.RLock()
|
2019-05-29 04:43:30 +00:00
|
|
|
v, _ := a.data[key]
|
2020-04-03 06:09:46 +00:00
|
|
|
a.muData.RUnlock()
|
|
|
|
|
2019-05-29 04:43:30 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
// LayerCount returns the number of layers in this actor's drawing.
|
|
|
|
func (a *Actor) LayerCount() int {
|
2020-04-05 04:00:32 +00:00
|
|
|
return len(a.Doodad().Layers)
|
2019-04-19 01:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ShowLayer sets the actor's ActiveLayer to the index given.
|
|
|
|
func (a *Actor) ShowLayer(index int) error {
|
|
|
|
if index < 0 {
|
|
|
|
return errors.New("layer index must be 0 or greater")
|
2020-04-05 04:00:32 +00:00
|
|
|
} else if index > len(a.Doodad().Layers) {
|
2019-04-19 01:15:05 +00:00
|
|
|
return fmt.Errorf("layer %d out of range for doodad's layers", index)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.activeLayer = index
|
2020-04-05 04:00:32 +00:00
|
|
|
a.Canvas.Load(a.Doodad().Palette, a.Doodad().Layers[index].Chunker)
|
2019-04-19 01:15:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-31 02:13:28 +00:00
|
|
|
// ShowLayerNamed sets the actor's ActiveLayer to the one named.
|
|
|
|
func (a *Actor) ShowLayerNamed(name string) error {
|
|
|
|
// Find the layer.
|
2020-04-05 04:00:32 +00:00
|
|
|
for i, layer := range a.Doodad().Layers {
|
2019-12-31 02:13:28 +00:00
|
|
|
if layer.Name == name {
|
|
|
|
return a.ShowLayer(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Warn("Actor(%s) ShowLayerNamed(%s): layer not found",
|
|
|
|
a.Actor.Filename,
|
|
|
|
name,
|
|
|
|
)
|
2022-09-25 06:54:51 +00:00
|
|
|
|
|
|
|
// XX: returning an error raises a JavaScript exception in doodads. :/ Warning log is enough.
|
|
|
|
return nil
|
2019-12-31 02:13:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
// Destroy deletes the actor from the running level.
|
|
|
|
func (a *Actor) Destroy() {
|
|
|
|
a.flagDestroy = true
|
|
|
|
}
|