1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-09 19:57:20 -05:00
OpenDiablo2/d2core/d2map/d2mapentity/factory.go
gravestench fc87b2be7a
Removing d2datadict singletons (#738)
* Remove weapons, armor, misc, itemCommon, itemTyps datadict singletons

- removed loader calls from d2app
- removed the HeroObjects singleton from `d2core/d2inventory`
- added an InventoryItemFactory in d2inventory
- package-level functions that use data records are now methods of the InventoryItemFactory
- renamed ItemGenerator in d2item to ItemFactory
- package-level functions that use records are now methods of ItemFactory
- d2map.MapEntityFactory now has an item factory instance for creating items
- fixed a bug in unique item record loader where it loaded an empty record
- added a PlayerStateFactory for creating a player state (uses the asset manager)
- updated the test inventory/equipment code in d2player to handle errors from the ItemFactory
- character select and character creation screens have a player state and inventory item factory
- updated item tests to use the item factory

* minor edit

* Removed d2datadict.Experience singleton

added a HeroStatsFactory, much like the other factories. The factory  gets an
asset manager reference in order to use data records.

* removed d2datadict.AutoMagic singleton

* removed d2datadict.AutoMap singleton

* removed d2datadict.BodyLocations singleton

* removed d2datadict.Books singleton

* Removed singletons for level records

- removed loader calls in d2app
- changed type references from d2datadict to d2records
- added a `MapGenerator` in d2mapgen which uses thew asset manager and map engine
- package-level map generation functions are now MapGenerator methods
- `d2datadict.GetLevelDetails(id int)` is now a method of the RecordManager

* remove SkillCalc and MissileCalc singletons

* Removed CharStats and ItemStatCost singletons

- added an ItemStatFactory which uses the asset manager to create stats
- package-level functions for stats in d2item are now StatFactory methods
- changed type references from d2datadict to d2records
- `d2player.GetAllPlayerStates` is now a method of the `PlayerStateFactory`

* Removed DkillDesc and Skills singletons from d2datadict

- removed loader calls from d2app
- diablo2stats.Stat instances are given a reference to the factory for doing record lookups

* update the stats test to use mock a asset manager and stat factory

* fixed diablo2stats tests and diablo2item tests

* removed CompCodes singleton from d2datadict

* remove cubemain singleton from d2datadict

* removed DifficultyLevels singleton from d2datadict

* removed ElemTypes singleton from d2datadict

* removed events.go loader from d2datadict (was unused)

* removed Gems singleton from d2datadict

* removed Hireling and Inventory singletons from d2datadict

* removed MagicPrefix and MagicSuffix singletons from d2datadict

* removed ItemRatios singleton from d2datadict

* removed Missiles singleton from d2datadict

* removed MonModes singleton

* Removed all monster and npc singletons from d2datadict

- MapStamp instances now get a reference to their factory for doing record lookups

* removed SoundEntry and SoundEnviron singletons from d2datadict
2020-09-20 17:52:01 -04:00

246 lines
7.2 KiB
Go

package d2mapentity
import (
"fmt"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2records"
uuid "github.com/satori/go.uuid"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2tbl"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2inventory"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2item/diablo2item"
)
// NewMapEntityFactory creates a MapEntityFactory instance with the given asset manager
func NewMapEntityFactory(asset *d2asset.AssetManager) (*MapEntityFactory, error) {
itemFactory, err := diablo2item.NewItemFactory(asset)
if err != nil {
return nil, err
}
stateFactory, err := d2hero.NewHeroStateFactory(asset)
if err != nil {
return nil, err
}
entityFactory := &MapEntityFactory{
stateFactory,
asset,
itemFactory,
}
return entityFactory, nil
}
// MapEntityFactory creates map entities for the MapEngine
type MapEntityFactory struct {
*d2hero.HeroStateFactory
asset *d2asset.AssetManager
item *diablo2item.ItemFactory
}
// NewAnimatedEntity creates an instance of AnimatedEntity
func NewAnimatedEntity(x, y int, animation d2interface.Animation) *AnimatedEntity {
entity := &AnimatedEntity{
mapEntity: newMapEntity(x, y),
animation: animation,
}
entity.mapEntity.directioner = entity.rotate
return entity
}
// NewPlayer creates a new player entity and returns a pointer to it.
func (f *MapEntityFactory) NewPlayer(id, name string, x, y, direction int, heroType d2enum.Hero,
stats *d2hero.HeroStatsState, skills map[int]*d2hero.HeroSkill, equipment *d2inventory.CharacterEquipment) *Player {
layerEquipment := &[d2enum.CompositeTypeMax]string{
d2enum.CompositeTypeHead: equipment.Head.GetArmorClass(),
d2enum.CompositeTypeTorso: equipment.Torso.GetArmorClass(),
d2enum.CompositeTypeLegs: equipment.Legs.GetArmorClass(),
d2enum.CompositeTypeRightArm: equipment.RightArm.GetArmorClass(),
d2enum.CompositeTypeLeftArm: equipment.LeftArm.GetArmorClass(),
d2enum.CompositeTypeRightHand: equipment.RightHand.GetItemCode(),
d2enum.CompositeTypeLeftHand: equipment.LeftHand.GetItemCode(),
d2enum.CompositeTypeShield: equipment.Shield.GetItemCode(),
}
composite, err := f.asset.LoadComposite(d2enum.ObjectTypePlayer, heroType.GetToken(),
d2resource.PaletteUnits)
if err != nil {
panic(err)
}
stats.NextLevelExp = f.asset.Records.GetExperienceBreakpoint(heroType, stats.Level)
stats.Stamina = stats.MaxStamina
defaultCharStats := f.asset.Records.Character.Stats[heroType]
statsState := f.HeroStateFactory.CreateHeroStatsState(heroType, defaultCharStats)
heroState, _ := f.CreateHeroState(name, heroType, statsState)
attackSkillID := 0
result := &Player{
mapEntity: newMapEntity(x, y),
composite: composite,
Equipment: equipment,
Stats: heroState.Stats,
Skills: heroState.Skills,
//TODO: active left & right skill should be loaded from save file instead
LeftSkill: heroState.Skills[attackSkillID],
RightSkill: heroState.Skills[attackSkillID],
name: name,
Class: heroType,
//nameLabel: d2ui.NewLabel(d2resource.FontFormal11, d2resource.PaletteStatic),
isRunToggled: true,
isInTown: true,
isRunning: true,
}
result.mapEntity.uuid = id
result.SetSpeed(baseRunSpeed)
result.mapEntity.directioner = result.rotate
err = composite.SetMode(d2enum.PlayerAnimationModeTownNeutral, equipment.RightHand.GetWeaponClass())
if err != nil {
panic(err)
}
composite.SetDirection(direction)
if err := composite.Equip(layerEquipment); err != nil {
fmt.Printf("failed to equip, err: %v\n", err)
}
return result
}
// NewMissile creates a new Missile and initializes it's animation.
func (f *MapEntityFactory) NewMissile(x, y int, record *d2records.MissileRecord) (*Missile, error) {
animation, err := f.asset.LoadAnimation(
fmt.Sprintf("%s/%s.dcc", d2resource.MissileData, record.Animation.CelFileName),
d2resource.PaletteUnits,
)
if err != nil {
return nil, err
}
if record.Animation.HasSubLoop {
animation.SetSubLoop(record.Animation.SubStartingFrame, record.Animation.SubEndingFrame)
}
animation.SetEffect(d2enum.DrawEffectModulate)
animation.SetPlayLoop(record.Animation.LoopAnimation)
animation.PlayForward()
entity := NewAnimatedEntity(x, y, animation)
result := &Missile{
AnimatedEntity: entity,
record: record,
}
result.Speed = float64(record.Velocity)
return result, nil
}
// NewItem creates an item map entity
func (f *MapEntityFactory) NewItem(x, y int, codes ...string) (*Item, error) {
item, err := f.item.NewItem(codes...)
if err != nil {
return nil, err
}
filename := item.CommonRecord().FlippyFile
filepath := fmt.Sprintf("%s/%s.DC6", d2resource.ItemGraphics, filename)
animation, err := f.asset.LoadAnimation(filepath, d2resource.PaletteUnits)
if err != nil {
return nil, err
}
animation.PlayForward()
animation.SetPlayLoop(false)
entity := NewAnimatedEntity(x*5, y*5, animation)
result := &Item{
AnimatedEntity: entity,
Item: item,
}
return result, nil
}
// NewNPC creates a new NPC and returns a pointer to it.
func (f *MapEntityFactory) NewNPC(x, y int, monstat *d2records.MonStatsRecord, direction int) (*NPC, error) {
result := &NPC{
mapEntity: newMapEntity(x, y),
HasPaths: false,
monstatRecord: monstat,
monstatEx: f.asset.Records.Monster.Stats2[monstat.ExtraDataKey],
}
var equipment [16]string
for compType, opts := range result.monstatEx.EquipmentOptions {
equipment[compType] = selectEquip(opts)
}
composite, _ := f.asset.LoadComposite(d2enum.ObjectTypeCharacter, monstat.AnimationDirectoryToken,
d2resource.PaletteUnits)
result.composite = composite
if err := composite.SetMode(d2enum.MonsterAnimationModeNeutral,
result.monstatEx.BaseWeaponClass); err != nil {
return nil, err
}
if err := composite.Equip(&equipment); err != nil {
return nil, err
}
result.SetSpeed(float64(monstat.SpeedBase))
result.mapEntity.directioner = result.rotate
result.composite.SetDirection(direction)
if result.monstatRecord != nil && result.monstatRecord.IsInteractable {
result.name = d2tbl.TranslateString(result.monstatRecord.NameString)
}
return result, nil
}
// NewObject creates an instance of AnimatedComposite
func (f *MapEntityFactory) NewObject(x, y int, objectRec *d2datadict.ObjectRecord,
palettePath string) (*Object, error) {
locX, locY := float64(x), float64(y)
entity := &Object{
uuid: uuid.NewV4().String(),
objectRecord: objectRec,
Position: d2vector.NewPosition(locX, locY),
name: d2tbl.TranslateString(objectRec.Name),
}
objectType := &d2datadict.ObjectTypes[objectRec.Index]
composite, err := f.asset.LoadComposite(d2enum.ObjectTypeItem, objectType.Token,
palettePath)
if err != nil {
return nil, err
}
entity.composite = composite
_ = entity.setMode(d2enum.ObjectAnimationModeNeutral, 0, false)
_, _ = initObject(entity)
return entity, nil
}