npc

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2022 License: MIT Imports: 17 Imported by: 2

README

npc

NPC library for Dragonfly.

Getting started

The NPC library may be imported using go get:

go get github.com/df-mc/npc

Usage

Go Reference

Usage of the NPC library is simple. It relies on the Create method:

// var w *world.World

settings := npc.Settings{
    Name: "Example NPC",
    Scale: 2,
    Position: mgl64.Vec3{1, 2, 3},
    Skin   ...,
}
p := npc.Create(settings, w, nil)
p.SwingArm()

Instead of nil, an npc.HandlerFunc may be passed to handle the NPC being hit by other players.

Note that the npc.Settings passed initially may be overwritten by calling methods on the *player.Player returned by npc.Create().

The NPC library also contains convenience functions for reading/parsing skin data from files. The npc.Skin(), npc.*Model() and npc.*Texture() functions may be used to do so.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// DefaultModel is a Model for a default custom skin. It is a model with a 64x64 texture.
	DefaultModel, _ = ReadModel(bytes.NewReader(defaultModel))
)

Functions

func Create

func Create(s Settings, w *world.World, f HandlerFunc) *player.Player

Create creates a new NPC with the Settings passed. A world.Loader is spawned in the background which follows the NPC to prevent it from despawning. Create panics if the world passed is nil. The HandlerFunc passed handles a player interacting with the NPC. Nil may be passed to avoid calling any function when the entity is interacted with. Create returns the *player.Player created. This entity has been added to the world passed. It may be removed from the world like any other entity by calling (*player.Player).Close.

func MustSkin

func MustSkin(tex Texture, mod Model) skin.Skin

MustSkin creates a new skin.Skin using the Texture and Model passed. It panics if the dimensions as specified in the model don't match those of the Texture.

func Skin

func Skin(tex Texture, mod Model) (skin.Skin, error)

Skin creates a new skin.Skin using the Texture and Model passed. It returns an error if the dimensions as specified in the Model did not match those of the Texture.

Types

type HandlerFunc

type HandlerFunc func(p *player.Player)

HandlerFunc may be passed to Create to handle a *player.Player attacking an NPC.

type Model

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

Model is the model of a skin.Skin parsed with ParseModel or ReadModel.

func MustParseModel

func MustParseModel(path string) Model

MustParseModel parses a Model from a JSON file at a specific path. It panics if the file could not be opened or if the JSON contained was invalid or not a proper model.

func ParseModel

func ParseModel(path string) (Model, error)

ParseModel parses a Model from a JSON file at a specific path. An error is returned if the file could not be opened or if the JSON contained was invalid or not a proper model.

func ReadModel

func ReadModel(r io.Reader) (Model, error)

ReadModel parses a JSON model from a file at a path passed and returns a Model. It contains a parsed skin.ModelConfig and the bounds of the skin texture as specified in the model. If the file could not be parsed or if the model data was invalid, an error is returned.

type Settings

type Settings struct {
	Name       string
	Skin       skin.Skin
	Position   mgl64.Vec3
	Yaw, Pitch float64
	Scale      float64
	Immobile   bool
	Vulnerable bool
	MainHand,
	OffHand,
	Helmet,
	Chestplate,
	Leggings,
	Boots item.Stack
}

Settings holds different NPC settings such as the NPC's name, skin, position, etc. These values may be changed at runtime by calling the respective methods on the *player.Player returned by Create, the values passed in a Settings struct to Create are merely the initial values.

type Texture

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

Texture is the texture of a skin.Skin parsed with ParseTexture or ReadTexture.

func MustParseTexture

func MustParseTexture(path string) Texture

MustParseTexture parses a Texture from an image file at a specific path. It panics if the file could not be opened or if it did not contain a valid skin image.

func ParseTexture

func ParseTexture(path string) (Texture, error)

ParseTexture parses a Texture from an image file at a specific path. An error is returned if the file could not be opened or if it did not contain a valid skin image.

func ReadTexture

func ReadTexture(r io.Reader) (Texture, error)

ReadTexture parses a skin texture from the path passed and returns a Texture struct with data, where each pixel is represented by 4 bytes. An error is returned if the image could not be opened or was otherwise invalid as a skin.

Jump to

Keyboard shortcuts

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