last_light/game/model/item.go

222 lines
4.5 KiB
Go
Raw Permalink Normal View History

2024-06-06 23:17:22 +03:00
package model
import (
"mvvasilev/last_light/engine"
"github.com/gdamore/tcell/v2"
)
type ItemMetaType int
const (
MetaItemType_Physical_Weapon ItemMetaType = iota
MetaItemType_Magic_Weapon
MetaItemType_Weapon
MetaItemType_Physical_Armour
MetaItemType_Magic_Armour
MetaItemType_Armour
MetaItemType_Consumable
MetaItemType_Potion
)
2024-06-06 23:28:06 +03:00
type Item interface {
2024-06-06 23:17:22 +03:00
TileIcon() rune
Icon() string
Style() tcell.Style
Type() ItemType
Quantifiable() *Item_QuantifiableComponent
Usable() *Item_UsableComponent
Equippable() *Item_EquippableComponent
Named() *Item_NamedComponent
Described() *Item_DescribedComponent
Damaging() *Item_DamagingComponent
StatModifier() *Item_StatModifierComponent
MetaTypes() *Item_MetaTypesComponent
}
type Item_QuantifiableComponent struct {
MaxQuantity int
CurrentQuantity int
}
type Item_UsableComponent struct {
2024-06-06 23:28:06 +03:00
IsUsableBy func(Entity) bool
Use func(*engine.GameEventLog, *Dungeon, Entity)
2024-06-06 23:17:22 +03:00
}
type Item_EquippableComponent struct {
Slot EquippedSlot
}
type Item_NamedComponent struct {
Name string
Style tcell.Style
}
type Item_DescribedComponent struct {
Description string
Style tcell.Style
}
type Item_DamagingComponent struct {
IsRanged bool
2024-06-06 23:17:22 +03:00
DamageRoll func() (damage int, dmgType DamageType)
}
type Item_StatModifierComponent struct {
StatModifiers []StatModifier
}
type Item_MetaTypesComponent struct {
Types []ItemMetaType
}
2024-06-06 23:28:06 +03:00
type BaseItem struct {
2024-06-06 23:17:22 +03:00
tileIcon rune
icon string
style tcell.Style
itemType ItemType
quantifiable *Item_QuantifiableComponent
usable *Item_UsableComponent
equippable *Item_EquippableComponent
named *Item_NamedComponent
described *Item_DescribedComponent
damaging *Item_DamagingComponent
statModifier *Item_StatModifierComponent
metaTypes *Item_MetaTypesComponent
}
2024-06-06 23:28:06 +03:00
func (i *BaseItem) TileIcon() rune {
2024-06-06 23:17:22 +03:00
return i.tileIcon
}
2024-06-06 23:28:06 +03:00
func (i *BaseItem) Icon() string {
2024-06-06 23:17:22 +03:00
return i.icon
}
2024-06-06 23:28:06 +03:00
func (i *BaseItem) Style() tcell.Style {
2024-06-06 23:17:22 +03:00
return i.style
}
2024-06-06 23:28:06 +03:00
func (i *BaseItem) Type() ItemType {
2024-06-06 23:17:22 +03:00
return i.itemType
}
2024-06-06 23:28:06 +03:00
func (i *BaseItem) Quantifiable() *Item_QuantifiableComponent {
2024-06-06 23:17:22 +03:00
return i.quantifiable
}
2024-06-06 23:28:06 +03:00
func (i *BaseItem) Usable() *Item_UsableComponent {
2024-06-06 23:17:22 +03:00
return i.usable
}
2024-06-06 23:28:06 +03:00
func (i *BaseItem) Equippable() *Item_EquippableComponent {
2024-06-06 23:17:22 +03:00
return i.equippable
}
2024-06-06 23:28:06 +03:00
func (i *BaseItem) Named() *Item_NamedComponent {
2024-06-06 23:17:22 +03:00
return i.named
}
2024-06-06 23:28:06 +03:00
func (i *BaseItem) Described() *Item_DescribedComponent {
2024-06-06 23:17:22 +03:00
return i.described
}
2024-06-06 23:28:06 +03:00
func (i *BaseItem) Damaging() *Item_DamagingComponent {
2024-06-06 23:17:22 +03:00
return i.damaging
}
2024-06-06 23:28:06 +03:00
func (i *BaseItem) StatModifier() *Item_StatModifierComponent {
2024-06-06 23:17:22 +03:00
return i.statModifier
}
2024-06-06 23:28:06 +03:00
func (i *BaseItem) MetaTypes() *Item_MetaTypesComponent {
2024-06-06 23:17:22 +03:00
return i.metaTypes
}
2024-06-06 23:28:06 +03:00
func createBaseItem(itemType ItemType, tileIcon rune, icon string, style tcell.Style, components ...func(*BaseItem)) *BaseItem {
i := &BaseItem{
2024-06-06 23:17:22 +03:00
itemType: itemType,
tileIcon: tileIcon,
icon: icon,
style: style,
}
for _, comp := range components {
comp(i)
}
return i
}
2024-06-06 23:28:06 +03:00
func item_WithQuantity(quantity, maxQuantity int) func(*BaseItem) {
return func(bi *BaseItem) {
2024-06-06 23:17:22 +03:00
bi.quantifiable = &Item_QuantifiableComponent{
CurrentQuantity: quantity,
MaxQuantity: maxQuantity,
}
}
}
2024-06-06 23:28:06 +03:00
func item_WithUsable(usabilityCheck func(Entity) bool, useFunc func(*engine.GameEventLog, *Dungeon, Entity)) func(*BaseItem) {
return func(bi *BaseItem) {
2024-06-06 23:17:22 +03:00
bi.usable = &Item_UsableComponent{
IsUsableBy: usabilityCheck,
Use: useFunc,
}
}
}
2024-06-06 23:28:06 +03:00
func item_WithEquippable(slot EquippedSlot) func(*BaseItem) {
return func(bi *BaseItem) {
2024-06-06 23:17:22 +03:00
bi.equippable = &Item_EquippableComponent{
Slot: slot,
}
}
}
func item_WithDamaging(isRanged bool, damageFunc func() (damage int, dmgType DamageType)) func(*BaseItem) {
2024-06-06 23:28:06 +03:00
return func(bi *BaseItem) {
2024-06-06 23:17:22 +03:00
bi.damaging = &Item_DamagingComponent{
IsRanged: isRanged,
2024-06-06 23:17:22 +03:00
DamageRoll: damageFunc,
}
}
}
2024-06-06 23:28:06 +03:00
func item_WithName(name string, style tcell.Style) func(*BaseItem) {
return func(bi *BaseItem) {
2024-06-06 23:17:22 +03:00
bi.named = &Item_NamedComponent{
Name: name,
Style: style,
}
}
}
2024-06-06 23:28:06 +03:00
func item_WithDescription(description string, style tcell.Style) func(*BaseItem) {
return func(bi *BaseItem) {
2024-06-06 23:17:22 +03:00
bi.described = &Item_DescribedComponent{
Description: description,
Style: style,
}
}
}
2024-06-06 23:28:06 +03:00
func item_WithStatModifiers(statModifiers []StatModifier) func(*BaseItem) {
return func(bi *BaseItem) {
2024-06-06 23:17:22 +03:00
bi.statModifier = &Item_StatModifierComponent{
StatModifiers: statModifiers,
}
}
}
2024-06-06 23:28:06 +03:00
func item_WithMetaTypes(metaTypes []ItemMetaType) func(*BaseItem) {
return func(bi *BaseItem) {
2024-06-06 23:17:22 +03:00
bi.metaTypes = &Item_MetaTypesComponent{
Types: metaTypes,
}
}
}