game

package
v0.0.0-...-ffa1f1a Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2024 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BlockTypeDirt   byte = 0
	BlockTypeGrass  byte = 1
	BlockTypeTree   byte = 2
	BlockTypeStone  byte = 3
	BlockTypeCopper byte = 4

	BlockTypeDirtFrameDirt byte = 0

	BlockTypeGrassFrame1 byte = 0
	BlockTypeGrassFrame2 byte = 1
	BlockTypeGrassFrame3 byte = 2
	BlockTypeGrassFrame4 byte = 3

	BlockTypeTreeFrameSapling     byte = 0
	BlockTypeTreeFrameGrownTop    byte = 1
	BlockTypeTreeFrameGrownBottom byte = 2

	BlockTypeStoneFrame1 byte = 0

	BlockTypeCopperFrame1 byte = 0
)
View Source
const (
	CollideableTypeBlock   byte = 0
	CollideableTypePlayer  byte = 1
	CollideableTypeFloater byte = 2
)
View Source
const (
	CollisionDirectionUp    byte = 1
	CollisionDirectionDown  byte = 2
	CollisionDirectionLeft  byte = 3
	CollisionDirectionRight byte = 4
)
View Source
const (
	PlayerDirectionUp    byte = 0
	PlayerDirectionDown  byte = 1
	PlayerDirectionLeft  byte = 2
	PlayerDirectionRight byte = 3

	PlayerWalking byte = 0
	PlayerRunning byte = 1
)
View Source
const (
	FloaterTypeDirt byte = 0
)
View Source
const (
	UnderlyingTypePlaceableBlock byte = 0
)

Variables

View Source
var (
	DrawColor        = pixel.RGBA{R: 1, G: 1, B: 1}
	RectangleSprites = map[int]map[int]*pixel.Sprite{}
)
View Source
var (
	Font  font.Face
	Atlas *text.Atlas

	Tiles        map[byte]map[byte]*pixel.Sprite
	Floaters     []*Floater
	Collideables []Collideable // list of objects to check for collision

	FloaterBorderImage  *image.RGBA
	FloaterBorderSprite *pixel.Sprite
)

Functions

func AddCollideable

func AddCollideable(c Collideable)

func CollisionBBox

func CollisionBBox(pos1, size1, pos2, size2 pixel.Vec) bool

func DrawHLine

func DrawHLine(img *image.RGBA, x1, y, x2 int)

HLine draws a horizontal line

func DrawSetColor

func DrawSetColor(color pixel.RGBA)

func DrawVLine

func DrawVLine(img *image.RGBA, x, y1, y2 int)

VLine draws a veritcal line

func GetCollisionDirection

func GetCollisionDirection(c1, c2 Collideable) byte

func GetInventoryItemDrawPosition

func GetInventoryItemDrawPosition(win *opengl.Window, x, y int) pixel.Vec

func MakeDebugRect

func MakeDebugRect(win *opengl.Window, w, h int) *pixel.Sprite

Rect draws a rectangle utilizing HLine() and VLine()

func MakeRect

func MakeRect(w, h int, c color.Color) (*image.RGBA, *pixel.Sprite)

Types

type Block

type Block struct {
	Position  pixel.Vec
	Type      byte
	Frame     byte
	DebugRect *pixel.Sprite
}

func NewBlock

func NewBlock(win *opengl.Window, blockType, frame byte, pos pixel.Vec) *Block

func (*Block) Collide

func (b *Block) Collide(c Collideable)

func (*Block) DrawDebug

func (b *Block) DrawDebug(win *opengl.Window)

func (*Block) GetOldPosition

func (b *Block) GetOldPosition() pixel.Vec

func (*Block) GetPosition

func (b *Block) GetPosition() pixel.Vec

func (*Block) GetSize

func (b *Block) GetSize() pixel.Vec

func (*Block) GetType

func (b *Block) GetType() byte

func (*Block) IsSolid

func (b *Block) IsSolid() bool

type Camera

type Camera struct {
	Matrix    pixel.Matrix
	Position  pixel.Vec
	Speed     float64
	Zoom      float64
	ZoomSpeed float64
}

func NewCamera

func NewCamera() *Camera

func (*Camera) EndCamera

func (c *Camera) EndCamera(win *opengl.Window)

func (*Camera) StartCamera

func (c *Camera) StartCamera(win *opengl.Window)

func (*Camera) Update

func (c *Camera) Update(pos pixel.Vec)

type Chunk

type Chunk struct {
	X      int
	Y      int
	W      int
	H      int
	Blocks map[int]map[int][]*Block
}

func NewChunk

func NewChunk(win *opengl.Window, x, y, w, h int, chunkType string, g *Game) *Chunk

type Collideable

type Collideable interface {
	GetPosition() pixel.Vec
	GetSize() pixel.Vec
	Collide(Collideable)
	IsSolid() bool
	GetType() byte
	DrawDebug(*opengl.Window)
	GetOldPosition() pixel.Vec
}

type Floater

type Floater struct {
	Position       pixel.Vec
	Velocity       pixel.Vec
	OldPosition    pixel.Vec
	UnderlyingType byte
	ItemType       byte
	Frame          byte
	Size           pixel.Vec
	Scale          float64
	Solid          bool
	Sprite         *pixel.Sprite
	RotationSpeed  float64
	Rotation       float64
	ScaleSpeed     float64
	ScaleMax       float64
	ScaleMin       float64
	ScaleDir       float64
	DebugRect      *pixel.Sprite
	Deleted        bool
}

When an item is dropped, it "floats" and rotates around on the ground until someone picks it up

func NewFloater

func NewFloater(win *opengl.Window, underType, itemType, frame byte, position, velocity pixel.Vec) *Floater

func (*Floater) Collide

func (f *Floater) Collide(c Collideable)

func (*Floater) Draw

func (f *Floater) Draw(win *opengl.Window)

func (*Floater) DrawDebug

func (f *Floater) DrawDebug(win *opengl.Window)

func (*Floater) GetOldPosition

func (f *Floater) GetOldPosition() pixel.Vec

func (*Floater) GetPosition

func (f *Floater) GetPosition() pixel.Vec

func (*Floater) GetSize

func (f *Floater) GetSize() pixel.Vec

func (*Floater) GetType

func (f *Floater) GetType() byte

func (*Floater) IsSolid

func (f *Floater) IsSolid() bool

func (*Floater) Update

func (f *Floater) Update(dt float64)

type GUI

type GUI struct {
	Window      *opengl.Window
	Camera      *Camera
	Spritesheet *Spritesheet
	BarSprite   *pixel.Sprite

	OffsetX float64
	OffsetY float64
	Scale   float64

	Health            float64
	HealthBarPosition pixel.Vec
	HealthBarImage    *image.RGBA
	HealthBarSprite   *pixel.Sprite

	Hunger            float64
	HungerBarPosition pixel.Vec
	HungerBarImage    *image.RGBA
	HungerBarSprite   *pixel.Sprite

	Thirst            float64
	ThirstBarPosition pixel.Vec
	ThirstBarImage    *image.RGBA
	ThirstBarSprite   *pixel.Sprite

	BigSprite *pixel.Sprite
	BigOffset pixel.Vec

	ItemSprite *pixel.Sprite

	NeedsRedraw bool

	HotbarItems           []*InventoryItem
	HotbarX               int
	HotbarSelectionSprite *pixel.Sprite
	Inventory             [][]*InventoryItem
	ShouldDrawInventory   bool

	Tiles map[byte]map[byte]*pixel.Sprite

	HoldingInvItem *InventoryItem

	CraftingSlots [][]*InventoryItem
}

func NewGUI

func NewGUI(win *opengl.Window) (*GUI, error)

func (*GUI) ButtonCallback

func (g *GUI) ButtonCallback(btn pixel.Button, action pixel.Action)

func (*GUI) ClearCraftingItems

func (g *GUI) ClearCraftingItems()

func (*GUI) Draw

func (g *GUI) Draw(cam *Camera)

func (*GUI) DrawHotbar

func (g *GUI) DrawHotbar()

func (*GUI) DrawInventory

func (g *GUI) DrawInventory()

func (*GUI) HandleCraftingSlotLeftClick

func (g *GUI) HandleCraftingSlotLeftClick(x, y int)

func (*GUI) HandleCraftingSlotRightClick

func (g *GUI) HandleCraftingSlotRightClick(x, y int)

func (*GUI) HandleDeleteItemLeftClick

func (g *GUI) HandleDeleteItemLeftClick(x, y int)

func (*GUI) HandleInventoryLeftClick

func (g *GUI) HandleInventoryLeftClick(x, y int)

func (*GUI) HandleInventoryRightClick

func (g *GUI) HandleInventoryRightClick(x, y int)

func (*GUI) RedrawBars

func (g *GUI) RedrawBars()

func (*GUI) SetHotbarItems

func (g *GUI) SetHotbarItems(items []*InventoryItem, hotbarX int)

func (*GUI) SetInventoryItems

func (g *GUI) SetInventoryItems(items [][]*InventoryItem)

func (*GUI) Update

func (g *GUI) Update(dt float64)

func (*GUI) UpdateHealth

func (g *GUI) UpdateHealth(v float64)

func (*GUI) UpdateHunger

func (g *GUI) UpdateHunger(v float64)

func (*GUI) UpdateThirst

func (g *GUI) UpdateThirst(v float64)

type Game

type Game struct {
	Map                   *Map
	Player                *Player
	CollideablesDrawDebug bool
	GUI                   *GUI
	Window                *opengl.Window
	Camera                *Camera
}

func NewGame

func NewGame(name string, win *opengl.Window) (*Game, error)

func (*Game) ButtonCallback

func (g *Game) ButtonCallback(btn pixel.Button, action pixel.Action)

func (*Game) CharCallback

func (g *Game) CharCallback(r rune)

func (*Game) CheckCollisions

func (g *Game) CheckCollisions()

func (*Game) Draw

func (g *Game) Draw()

func (*Game) Init

func (g *Game) Init(win *opengl.Window)

func (*Game) Scroll

func (g *Game) Scroll(win *opengl.Window, scroll pixel.Vec)

func (*Game) Update

func (g *Game) Update(win *opengl.Window, dt float64)

type IntVec

type IntVec struct {
	X int
	Y int
}

func NewIntVec

func NewIntVec(x, y int) IntVec

func (IntVec) ToVec

func (i IntVec) ToVec() pixel.Vec

type InventoryItem

type InventoryItem struct {
	UnderlyingType        byte
	ItemType              byte
	Frame                 byte
	Amount                int
	InventoryPosition     pixel.Vec
	DrawPosition          pixel.Vec
	ShouldUseDrawPosition bool
	Count                 *text.Text
	Font                  font.Face
	Atlas                 *text.Atlas
}

func NewInventoryItem

func NewInventoryItem(underType, itemType, frame byte, amt int, inventoryPos pixel.Vec) *InventoryItem

func (*InventoryItem) Draw

func (i *InventoryItem) Draw(win *opengl.Window)

func (*InventoryItem) DrawCraftingItem

func (i *InventoryItem) DrawCraftingItem(win *opengl.Window, scale float64)

func (*InventoryItem) GetCraftingPosition

func (i *InventoryItem) GetCraftingPosition(win *opengl.Window, scale float64) pixel.Vec

func (*InventoryItem) GetDrawPosition

func (i *InventoryItem) GetDrawPosition(win *opengl.Window) pixel.Vec

type Map

type Map struct {
	Name            string
	Chunks          map[int]map[int]*Chunk
	Spritesheets    map[string]*Spritesheet
	FloorBatch      *pixel.Batch // the holder for batch drawing
	TreeBatchBottom *pixel.Batch
	TreeBatchTop    *pixel.Batch
	DrawRadius      float64   // how many chunks around the current center chunk should be drawn
	ChunkPosition   pixel.Vec // the current center chunk
}

func NewMap

func NewMap(name string, s *Spritesheet) (*Map, error)

func (*Map) BlockExists

func (m *Map) BlockExists(chunk, coords IntVec) bool

func (*Map) Draw

func (m *Map) Draw(win *opengl.Window)

func (*Map) GenerateAllDirtChunk

func (m *Map) GenerateAllDirtChunk(win *opengl.Window, x, y int, force bool, g *Game)

func (*Map) GenerateChunksAroundPlayer

func (m *Map) GenerateChunksAroundPlayer(g *Game, win *opengl.Window)

func (*Map) RefreshDrawBatch

func (m *Map) RefreshDrawBatch()

RefreshDrawBatch loads the chunks around the maps center chunk using

type Player

type Player struct {
	Position            pixel.Vec
	OldPosition         pixel.Vec
	Speed               map[byte]float64 // pixels per second
	WalkingOrRunning    byte
	Spritesheet         *Spritesheet
	Frames              map[byte][]*pixel.Sprite
	SwingFrames         map[byte][]*pixel.Sprite
	FrameSpeed          map[byte]float64
	Inventory           [][]*InventoryItem
	InventoryW          int
	InventoryH          int
	HotbarX             int
	ShouldDrawInventory bool
	CurrentFrame        float64
	MaxMovementFrame    float64
	MovementDirection   byte
	MovementDirections  []byte
	Solid               bool
	DebugRect           *pixel.Sprite
	IsSwinging          bool
	SwingFrameSpeed     float64
	InInventory         bool
	MouseX              int // block position from bottom left
	MouseY              int // block position from bottom left
	MouseRectImage      *image.RGBA
	MouseRectSprite     *pixel.Sprite
	MaxPlaceDistance    float64
}

func NewPlayer

func NewPlayer(win *opengl.Window) (*Player, error)

func (*Player) AddInventoryItem

func (p *Player) AddInventoryItem(i *InventoryItem)

func (*Player) AddItemToInventory

func (p *Player) AddItemToInventory(underType, itemType, frame byte)

func (*Player) AddMovementDirection

func (p *Player) AddMovementDirection(d byte)

func (*Player) ButtonCallback

func (p *Player) ButtonCallback(game *Game, btn pixel.Button, action pixel.Action)

func (*Player) CharCallback

func (p *Player) CharCallback(game *Game, r rune)

func (*Player) ClearInventory

func (p *Player) ClearInventory()

func (*Player) Collide

func (p *Player) Collide(c Collideable)

func (*Player) Draw

func (p *Player) Draw(game *Game)

func (*Player) DrawDebug

func (p *Player) DrawDebug(win *opengl.Window)

func (*Player) GetBlockPosition

func (p *Player) GetBlockPosition() IntVec

func (*Player) GetChunkPosition

func (p *Player) GetChunkPosition() pixel.Vec

func (*Player) GetHeldItem

func (p *Player) GetHeldItem() *InventoryItem

func (*Player) GetMouseMapBlockCoords

func (p *Player) GetMouseMapBlockCoords(game *Game) pixel.Vec

func (*Player) GetMouseMapBlockPosition

func (p *Player) GetMouseMapBlockPosition(game *Game) pixel.Vec

Returns the X and Y coordinates for the block the mouse is over on the map

func (*Player) GetMouseMapCoords

func (p *Player) GetMouseMapCoords(game *Game) (IntVec, IntVec)

func (*Player) GetMouseMapPosition

func (p *Player) GetMouseMapPosition(game *Game) pixel.Vec

func (*Player) GetOldPosition

func (p *Player) GetOldPosition() pixel.Vec

func (*Player) GetPosition

func (p *Player) GetPosition() pixel.Vec

func (*Player) GetSize

func (p *Player) GetSize() pixel.Vec

func (*Player) GetType

func (p *Player) GetType() byte

func (*Player) HandleRightClick

func (p *Player) HandleRightClick(game *Game)

func (*Player) IsMovingInDirection

func (p *Player) IsMovingInDirection(d byte) bool

func (*Player) IsSolid

func (p *Player) IsSolid() bool

func (*Player) PlaceBlock

func (p *Player) PlaceBlock(game *Game, item *InventoryItem)

func (*Player) RemoveInventoryItem

func (p *Player) RemoveInventoryItem(i *InventoryItem)

func (*Player) RemoveMovementDirection

func (p *Player) RemoveMovementDirection(d byte)

func (*Player) ThrowInventoryItem

func (p *Player) ThrowInventoryItem(game *Game)

func (*Player) Update

func (p *Player) Update(win *opengl.Window, dt float64)

type Spritesheet

type Spritesheet struct {
	Path    string
	Picture pixel.Picture
}

func NewSpritesheet

func NewSpritesheet(path string) (*Spritesheet, error)

Jump to

Keyboard shortcuts

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