last_light/game/model/tile.go

157 lines
2.6 KiB
Go
Raw Normal View History

package model
2024-05-03 13:46:32 +03:00
import (
"mvvasilev/last_light/util"
"github.com/gdamore/tcell/v2"
)
type Material uint
const (
MaterialGround Material = iota
MaterialRock
2024-05-03 13:46:32 +03:00
MaterialWall
MaterialGrass
MaterialVoid
)
type TileType struct {
Material Material
Passable bool
Presentation rune
2024-05-03 13:46:32 +03:00
Transparent bool
Style tcell.Style
}
2024-05-03 13:46:32 +03:00
func TileTypeGround() TileType {
return TileType{
Material: MaterialGround,
Passable: true,
Presentation: '.',
2024-05-03 13:46:32 +03:00
Transparent: false,
Style: tcell.StyleDefault,
}
}
2024-05-03 13:46:32 +03:00
func TileTypeRock() TileType {
return TileType{
Material: MaterialRock,
Passable: false,
Presentation: '█',
2024-05-03 13:46:32 +03:00
Transparent: false,
Style: tcell.StyleDefault,
}
}
2024-05-03 13:46:32 +03:00
func TileTypeGrass() TileType {
return TileType{
Material: MaterialGrass,
Passable: true,
Presentation: ',',
2024-05-03 13:46:32 +03:00
Transparent: false,
Style: tcell.StyleDefault,
}
}
2024-05-03 13:46:32 +03:00
func TileTypeVoid() TileType {
return TileType{
Material: MaterialVoid,
Passable: false,
Presentation: ' ',
2024-05-03 13:46:32 +03:00
Transparent: true,
Style: tcell.StyleDefault,
}
}
func TileTypeWall() TileType {
return TileType{
Material: MaterialWall,
Passable: false,
Presentation: '#',
Transparent: false,
Style: tcell.StyleDefault.Background(tcell.ColorGray),
}
}
type Tile interface {
Position() util.Position
2024-05-03 13:46:32 +03:00
Presentation() (rune, tcell.Style)
Passable() bool
2024-05-03 13:46:32 +03:00
Transparent() bool
}
type StaticTile struct {
position util.Position
t TileType
}
func CreateStaticTile(x, y int, t TileType) Tile {
st := new(StaticTile)
st.position = util.PositionAt(x, y)
st.t = t
return st
}
func (st *StaticTile) Position() util.Position {
return st.position
}
2024-05-03 13:46:32 +03:00
func (st *StaticTile) Presentation() (rune, tcell.Style) {
return st.t.Presentation, st.t.Style
}
func (st *StaticTile) Passable() bool {
return st.t.Passable
}
2024-05-03 13:46:32 +03:00
func (st *StaticTile) Transparent() bool {
return st.t.Transparent
}
func (st *StaticTile) Type() TileType {
return st.t
}
2024-05-03 13:46:32 +03:00
type ItemTile struct {
position util.Position
itemType *ItemType
quantity int
}
func CreateItemTile(position util.Position, itemType *ItemType, quantity int) *ItemTile {
it := new(ItemTile)
it.position = position
it.itemType = itemType
it.quantity = quantity
return it
}
func (it *ItemTile) Type() *ItemType {
return it.itemType
}
func (it *ItemTile) Quantity() int {
return it.quantity
}
func (it *ItemTile) Position() util.Position {
return it.position
}
func (it *ItemTile) Presentation() (rune, tcell.Style) {
return it.itemType.tileIcon, it.itemType.style
}
func (it *ItemTile) Passable() bool {
return true
}
func (it *ItemTile) Transparent() bool {
return false
}