last_light/game/world/tile.go

242 lines
4.6 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-06 18:59:14 +03:00
"mvvasilev/last_light/game/model"
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
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),
}
}
2024-05-06 18:59:14 +03:00
func TileTypeClosedDoor() TileType {
return TileType{
Material: MaterialClosedDoor,
Passable: false,
Transparent: false,
Presentation: '[',
Style: tcell.StyleDefault.Foreground(tcell.ColorLightSteelBlue).Background(tcell.ColorSaddleBrown),
}
}
func TileTypeOpenDoor() TileType {
return TileType{
Material: MaterialClosedDoor,
Passable: false,
Transparent: false,
Presentation: '_',
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: '≡',
Style: tcell.StyleDefault.Foreground(tcell.ColorDarkSlateGray).Attributes(tcell.AttrBold),
}
}
func TileTypeStaircaseUp() TileType {
return TileType{
Material: MaterialStaircaseUp,
Passable: true,
Transparent: false,
Presentation: '^',
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
}
type StaticTile struct {
2024-05-06 21:19:08 +03:00
position engine.Position
t TileType
}
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
return st
}
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) {
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 {
2024-05-06 21:19:08 +03:00
position engine.Position
2024-05-06 18:59:14 +03:00
itemType *model.ItemType
2024-05-03 13:46:32 +03:00
quantity int
}
2024-05-06 21:19:08 +03:00
func CreateItemTile(position engine.Position, itemType *model.ItemType, quantity int) *ItemTile {
2024-05-03 13:46:32 +03:00
it := new(ItemTile)
it.position = position
it.itemType = itemType
it.quantity = quantity
return it
}
2024-05-06 18:59:14 +03:00
func (it *ItemTile) Type() *model.ItemType {
2024-05-03 13:46:32 +03:00
return it.itemType
}
func (it *ItemTile) Quantity() int {
return it.quantity
}
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-06 18:59:14 +03:00
return it.itemType.TileIcon(), it.itemType.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
type EntityTile interface {
Entity() model.MovableEntity
Tile
}
type BasicEntityTile struct {
entity model.MovableEntity
presentation rune
style tcell.Style
}
func CreateBasicEntityTile(entity model.MovableEntity, presentation rune, style tcell.Style) *BasicEntityTile {
return &BasicEntityTile{
entity: entity,
presentation: presentation,
style: style,
}
}
func (bet *BasicEntityTile) Entity() model.MovableEntity {
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) {
return bet.presentation, bet.style
}
func (bet *BasicEntityTile) Passable() bool {
return false
}
func (bet *BasicEntityTile) Transparent() bool {
return false
}