last_light/game/world/tile.go

275 lines
5.1 KiB
Go
Raw Normal View History

2024-05-06 18:59:14 +03:00
package world
2024-05-03 13:46:32 +03:00
import (
2024-05-06 21:19:08 +03:00
"mvvasilev/last_light/engine"
2024-05-21 23:08:51 +03:00
"mvvasilev/last_light/game/item"
2024-05-30 23:39:54 +03:00
"mvvasilev/last_light/game/npc"
2024-05-03 13:46:32 +03:00
"github.com/gdamore/tcell/v2"
)
type Material uint
const (
MaterialGround Material = iota
MaterialRock
2024-05-03 13:46:32 +03:00
MaterialWall
MaterialGrass
MaterialVoid
2024-05-06 18:59:14 +03:00
MaterialClosedDoor
MaterialOpenDoor
2024-05-12 23:22:39 +03:00
MaterialStaircaseDown
MaterialStaircaseUp
)
type TileType struct {
Material Material
Passable bool
Presentation rune
2024-05-03 13:46:32 +03:00
Transparent bool
2024-05-21 23:08:51 +03:00
Opaque bool
2024-05-03 13:46:32 +03:00
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,
2024-05-21 23:08:51 +03:00
Opaque: false,
2024-05-03 13:46:32 +03:00
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,
2024-05-21 23:08:51 +03:00
Opaque: true,
2024-05-03 13:46:32 +03:00
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,
2024-05-21 23:08:51 +03:00
Opaque: false,
2024-05-03 13:46:32 +03:00
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,
2024-05-21 23:08:51 +03:00
Opaque: true,
2024-05-03 13:46:32 +03:00
Style: tcell.StyleDefault,
}
}
func TileTypeWall() TileType {
return TileType{
Material: MaterialWall,
Passable: false,
Presentation: '#',
Transparent: false,
2024-05-21 23:08:51 +03:00
Opaque: true,
2024-05-03 13:46:32 +03:00
Style: tcell.StyleDefault.Background(tcell.ColorGray),
}
}
2024-05-06 18:59:14 +03:00
func TileTypeClosedDoor() TileType {
return TileType{
Material: MaterialClosedDoor,
Passable: false,
Transparent: false,
Presentation: '[',
2024-05-21 23:08:51 +03:00
Opaque: true,
2024-05-06 18:59:14 +03:00
Style: tcell.StyleDefault.Foreground(tcell.ColorLightSteelBlue).Background(tcell.ColorSaddleBrown),
}
}
func TileTypeOpenDoor() TileType {
return TileType{
Material: MaterialClosedDoor,
Passable: false,
Transparent: false,
Presentation: '_',
2024-05-21 23:08:51 +03:00
Opaque: false,
2024-05-06 18:59:14 +03:00
Style: tcell.StyleDefault.Foreground(tcell.ColorLightSteelBlue),
}
}
2024-05-12 23:22:39 +03:00
func TileTypeStaircaseDown() TileType {
return TileType{
Material: MaterialStaircaseDown,
Passable: true,
Transparent: false,
Presentation: '≡',
2024-05-21 23:08:51 +03:00
Opaque: false,
2024-05-12 23:22:39 +03:00
Style: tcell.StyleDefault.Foreground(tcell.ColorDarkSlateGray).Attributes(tcell.AttrBold),
}
}
func TileTypeStaircaseUp() TileType {
return TileType{
Material: MaterialStaircaseUp,
Passable: true,
Transparent: false,
Presentation: '^',
2024-05-21 23:08:51 +03:00
Opaque: false,
2024-05-12 23:22:39 +03:00
Style: tcell.StyleDefault.Foreground(tcell.ColorDarkSlateGray).Attributes(tcell.AttrBold),
}
}
type Tile interface {
2024-05-06 21:19:08 +03:00
Position() engine.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
2024-05-21 23:08:51 +03:00
Opaque() bool
Type() TileType
}
type StaticTile struct {
2024-05-06 21:19:08 +03:00
position engine.Position
t TileType
2024-05-21 23:08:51 +03:00
style tcell.Style
}
func CreateStaticTile(x, y int, t TileType) Tile {
st := new(StaticTile)
2024-05-06 21:19:08 +03:00
st.position = engine.PositionAt(x, y)
st.t = t
2024-05-21 23:08:51 +03:00
st.style = t.Style
return st
}
2024-05-21 23:08:51 +03:00
func CreateStaticTileWithStyleOverride(x, y int, t TileType, style tcell.Style) Tile {
return &StaticTile{
position: engine.PositionAt(x, y),
t: t,
style: style,
}
}
2024-05-06 21:19:08 +03:00
func (st *StaticTile) Position() engine.Position {
return st.position
}
2024-05-03 13:46:32 +03:00
func (st *StaticTile) Presentation() (rune, tcell.Style) {
2024-05-21 23:08:51 +03:00
return st.t.Presentation, st.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
}
2024-05-21 23:08:51 +03:00
func (st *StaticTile) Opaque() bool {
return st.t.Opaque
}
func (st *StaticTile) Type() TileType {
return st.t
}
2024-05-03 13:46:32 +03:00
type ItemTile struct {
2024-05-06 21:19:08 +03:00
position engine.Position
2024-05-21 23:08:51 +03:00
item item.Item
2024-05-03 13:46:32 +03:00
}
2024-05-21 23:08:51 +03:00
func CreateItemTile(position engine.Position, item item.Item) *ItemTile {
2024-05-03 13:46:32 +03:00
it := new(ItemTile)
it.position = position
2024-05-21 23:08:51 +03:00
it.item = item
2024-05-03 13:46:32 +03:00
return it
}
2024-05-21 23:08:51 +03:00
func (it *ItemTile) Item() item.Item {
return it.item
2024-05-03 13:46:32 +03:00
}
2024-05-06 21:19:08 +03:00
func (it *ItemTile) Position() engine.Position {
2024-05-03 13:46:32 +03:00
return it.position
}
func (it *ItemTile) Presentation() (rune, tcell.Style) {
2024-05-21 23:08:51 +03:00
return it.item.Type().TileIcon(), it.item.Type().Style()
2024-05-03 13:46:32 +03:00
}
func (it *ItemTile) Passable() bool {
return true
}
func (it *ItemTile) Transparent() bool {
return false
}
2024-05-06 18:59:14 +03:00
2024-05-21 23:08:51 +03:00
func (it *ItemTile) Opaque() bool {
return false
}
func (it *ItemTile) Type() TileType {
return TileType{}
}
2024-05-06 18:59:14 +03:00
type EntityTile interface {
2024-05-30 23:39:54 +03:00
Entity() npc.MovableEntity
2024-05-06 18:59:14 +03:00
Tile
}
type BasicEntityTile struct {
2024-05-30 23:39:54 +03:00
entity npc.MovableEntity
2024-05-06 18:59:14 +03:00
}
2024-06-01 11:20:51 +03:00
func CreateBasicEntityTile(entity npc.MovableEntity) *BasicEntityTile {
2024-05-06 18:59:14 +03:00
return &BasicEntityTile{
2024-06-01 11:20:51 +03:00
entity: entity,
2024-05-06 18:59:14 +03:00
}
}
2024-05-30 23:39:54 +03:00
func (bet *BasicEntityTile) Entity() npc.MovableEntity {
2024-05-06 18:59:14 +03:00
return bet.entity
}
2024-05-06 21:19:08 +03:00
func (bet *BasicEntityTile) Position() engine.Position {
2024-05-06 18:59:14 +03:00
return bet.entity.Position()
}
func (bet *BasicEntityTile) Presentation() (rune, tcell.Style) {
2024-06-01 11:20:51 +03:00
return bet.entity.Presentation()
2024-05-06 18:59:14 +03:00
}
func (bet *BasicEntityTile) Passable() bool {
return false
}
func (bet *BasicEntityTile) Transparent() bool {
return false
}
2024-05-21 23:08:51 +03:00
func (bet *BasicEntityTile) Opaque() bool {
return false
}
func (bet *BasicEntityTile) Type() TileType {
return TileType{}
}