mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-11-19 02:46:35 -05:00
854fce3b14
* export d2asset singleton * add *d2asset.AssetManager to d2app - d2app now has a reference to an asset manager which it will use for loading - added asset loader methods to the asset manager - functions in d2asset are now wrappers for asset manager methods * add asset manager reference to audio provider - d2app asset manager reference is now passed to audio provider - asset manager is created in main.go for now to pass into audio provider - CreateSoundEffect is now a method, no longer exported, uses the asset manager reference * d2app passes asset manager refence to map engine test * in d2asset, all calls to LoadFile replaced with call to Singleton.Loadfile * blizzard intro and credits screen - d2app passes reference to the asset manager to these screens * asset manager for d2map - adding MapStampFactory, takes an asset manager reference - embedded MapStampFactory into the MapEngine - LoadStamp is now a method of the MapStampFactory * d2asset: removed LoadFileStream, LoadFile, and FileExists * d2gui changes - singleton now has an asset manager reference - calls to d2asset loader functions removed - createButton is now a method of LayoutManager - moved LayoutEntry to its own file * map entity factory - Map engine has an embedded map entity factory - Map stamp factory gets a reference to the map engine's entity factory - Stamps are given a reference to the map engine entity factory when created - Character select gets a map entity factory - Embedded the stamp factory into the MapEngine * asset manager for d2ui - d2ui is passed an asset manager reference when created - all calls to d2asset loader functions in d2ui now refer to the asset manager - d2gamescreen gets a ui manager when created - help overlay is now passed a ui manager when created * d2gamescreen + d2player: asset manager references added an asset manager reference to - inventory panel + inventory grid - mini panel - game controls - help overlay - character select - main menu - select hero class - hero stats panel * Removed d2asset.LoadAnimation all references to this function have been replaced with calls to the asset manager method * adding asset to help overlay, bugfix for 4d59c91 * Removed d2asset.LoadFont and d2asset.LoadAnimationWithEffect all references to these have been replaced with calls to the asset manager methods * MapRenderer now gets an asset manager reference * removed d2asset.LoadPalette all references have been replaced with calls to an asset manager instance * merged d2object with d2mapentity d2object was only being used to create objects in the map, so the provider function is now a method of the map entity factory. calls to d2asset have been removed. * removed d2asset.LoadComposite all calls are now made to the asset manager method * removed d2asset singleton all singleton references have been removed, a single instance of the asset manager is passed around the entire app * rename Initialize to NewAssetManager
268 lines
6.5 KiB
Go
268 lines
6.5 KiB
Go
package d2player
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
|
)
|
|
|
|
// images for 1x1 grid tile items (rings and stuff) are 28x28 pixel
|
|
// however, the grid cells are 29x29 pixels, this is for padding
|
|
// for each row in inventory, we need to account for this padding
|
|
const cellPadding = 1
|
|
|
|
type InventoryItem interface {
|
|
InventoryGridSize() (width int, height int)
|
|
GetItemCode() string
|
|
InventoryGridSlot() (x, y int)
|
|
SetInventoryGridSlot(x, y int)
|
|
GetItemDescription() []string
|
|
}
|
|
|
|
var errorInventoryFull = errors.New("inventory full")
|
|
|
|
// ItemGrid is a reusable grid for use with player and merchant inventory.
|
|
// Handles layout and rendering item icons based on code.
|
|
type ItemGrid struct {
|
|
asset *d2asset.AssetManager
|
|
uiManager *d2ui.UIManager
|
|
items []InventoryItem
|
|
equipmentSlots map[d2enum.EquippedSlot]EquipmentSlot
|
|
width int
|
|
height int
|
|
originX int
|
|
originY int
|
|
sprites map[string]*d2ui.Sprite
|
|
slotSize int
|
|
}
|
|
|
|
func NewItemGrid(asset *d2asset.AssetManager, ui *d2ui.UIManager,
|
|
record *d2datadict.InventoryRecord) *ItemGrid {
|
|
grid := record.Grid
|
|
|
|
return &ItemGrid{
|
|
asset: asset,
|
|
uiManager: ui,
|
|
width: grid.Box.Width,
|
|
height: grid.Box.Height,
|
|
originX: grid.Box.Left,
|
|
originY: grid.Box.Top + (grid.Rows * cellPadding),
|
|
slotSize: grid.CellWidth,
|
|
sprites: make(map[string]*d2ui.Sprite),
|
|
equipmentSlots: genEquipmentSlotsMap(record),
|
|
}
|
|
}
|
|
|
|
func (g *ItemGrid) SlotToScreen(slotX, slotY int) (screenX, screenY int) {
|
|
screenX = g.originX + slotX*g.slotSize
|
|
screenY = g.originY + slotY*g.slotSize
|
|
|
|
return screenX, screenY
|
|
}
|
|
|
|
func (g *ItemGrid) ScreenToSlot(screenX, screenY int) (slotX, slotY int) {
|
|
slotX = (screenX - g.originX) / g.slotSize
|
|
slotY = (screenY - g.originY) / g.slotSize
|
|
|
|
return slotX, slotY
|
|
}
|
|
|
|
func (g *ItemGrid) GetSlot(x, y int) InventoryItem {
|
|
for _, item := range g.items {
|
|
slotX, slotY := item.InventoryGridSlot()
|
|
width, height := item.InventoryGridSize()
|
|
|
|
if x >= slotX && x < slotX+width && y >= slotY && y < slotY+height {
|
|
return item
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (g *ItemGrid) ChangeEquippedSlot(slot d2enum.EquippedSlot, item InventoryItem) {
|
|
var curItem = g.equipmentSlots[slot]
|
|
curItem.item = item
|
|
g.equipmentSlots[slot] = curItem
|
|
}
|
|
|
|
// Add places a given set of items into the first available slots.
|
|
// Returns a count of the number of items which could be inserted.
|
|
func (g *ItemGrid) Add(items ...InventoryItem) (int, error) {
|
|
added := 0
|
|
|
|
var err error
|
|
|
|
for _, item := range items {
|
|
if g.add(item) {
|
|
added++
|
|
} else {
|
|
err = errorInventoryFull
|
|
break
|
|
}
|
|
}
|
|
|
|
g.Load(items...)
|
|
|
|
return added, err
|
|
}
|
|
|
|
func (g *ItemGrid) loadItem(item InventoryItem) {
|
|
if _, exists := g.sprites[item.GetItemCode()]; !exists {
|
|
var itemSprite *d2ui.Sprite
|
|
|
|
// TODO: Put the pattern into D2Shared
|
|
animation, err := g.asset.LoadAnimation(
|
|
fmt.Sprintf("/data/global/items/inv%s.dc6", item.GetItemCode()),
|
|
d2resource.PaletteSky,
|
|
)
|
|
|
|
if err != nil {
|
|
log.Printf("failed to load sprite for item (%s): %v", item.GetItemCode(), err)
|
|
return
|
|
}
|
|
|
|
itemSprite, err = g.uiManager.NewSprite(animation)
|
|
if err != nil {
|
|
log.Printf("Failed to load sprite, error: " + err.Error())
|
|
}
|
|
|
|
g.sprites[item.GetItemCode()] = itemSprite
|
|
}
|
|
}
|
|
|
|
// Load reads the inventory sprites for items into local cache for rendering.
|
|
func (g *ItemGrid) Load(items ...InventoryItem) {
|
|
for _, item := range items {
|
|
g.loadItem(item)
|
|
}
|
|
|
|
for _, eq := range g.equipmentSlots {
|
|
if eq.item != nil {
|
|
g.loadItem(eq.item)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Walk from top left to bottom right until a position large enough to hold the item is found.
|
|
// This is inefficient but simplifies the storage. At most a hundred or so cells will be looped, so impact is minimal.
|
|
func (g *ItemGrid) add(item InventoryItem) bool {
|
|
for y := 0; y < g.height; y++ {
|
|
for x := 0; x < g.width; x++ {
|
|
if !g.canFit(x, y, item) {
|
|
continue
|
|
}
|
|
|
|
g.set(x, y, item)
|
|
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// canFit loops over all items to determine if any other items would overlap the given position.
|
|
func (g *ItemGrid) canFit(x, y int, item InventoryItem) bool {
|
|
insertWidth, insertHeight := item.InventoryGridSize()
|
|
if x+insertWidth > g.width || y+insertHeight > g.height {
|
|
return false
|
|
}
|
|
|
|
for _, compItem := range g.items {
|
|
slotX, slotY := compItem.InventoryGridSlot()
|
|
compWidth, compHeight := compItem.InventoryGridSize()
|
|
|
|
if x+insertWidth >= slotX &&
|
|
x < slotX+compWidth &&
|
|
y+insertHeight >= slotY &&
|
|
y < slotY+compHeight {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (g *ItemGrid) Set(x, y int, item InventoryItem) error {
|
|
if !g.canFit(x, y, item) {
|
|
return fmt.Errorf("can not set item (%s) to position (%v, %v)", item.GetItemCode(), x, y)
|
|
}
|
|
|
|
g.set(x, y, item)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (g *ItemGrid) set(x, y int, item InventoryItem) {
|
|
item.SetInventoryGridSlot(x, y)
|
|
g.items = append(g.items, item)
|
|
|
|
g.Load(item)
|
|
}
|
|
|
|
// Remove does an in place filter to remove the element from the slice of items.
|
|
func (g *ItemGrid) Remove(item InventoryItem) {
|
|
n := 0
|
|
|
|
for _, compItem := range g.items {
|
|
if compItem == item {
|
|
continue
|
|
}
|
|
|
|
g.items[n] = compItem
|
|
n++
|
|
}
|
|
|
|
g.items = g.items[:n]
|
|
}
|
|
|
|
func (g *ItemGrid) renderItem(item InventoryItem, target d2interface.Surface, x, y int) {
|
|
itemSprite := g.sprites[item.GetItemCode()]
|
|
if itemSprite != nil {
|
|
itemSprite.SetPosition(x, y)
|
|
itemSprite.GetCurrentFrameSize()
|
|
_ = itemSprite.Render(target)
|
|
}
|
|
}
|
|
|
|
func (g *ItemGrid) Render(target d2interface.Surface) {
|
|
g.renderInventoryItems(target)
|
|
g.renderEquippedItems(target)
|
|
}
|
|
|
|
func (g *ItemGrid) renderInventoryItems(target d2interface.Surface) {
|
|
for _, item := range g.items {
|
|
itemSprite := g.sprites[item.GetItemCode()]
|
|
slotX, slotY := g.SlotToScreen(item.InventoryGridSlot())
|
|
_, h := itemSprite.GetCurrentFrameSize()
|
|
slotY += h
|
|
|
|
g.renderItem(item, target, slotX, slotY)
|
|
}
|
|
}
|
|
|
|
func (g *ItemGrid) renderEquippedItems(target d2interface.Surface) {
|
|
for _, eq := range g.equipmentSlots {
|
|
if eq.item == nil {
|
|
continue
|
|
}
|
|
|
|
itemSprite := g.sprites[eq.item.GetItemCode()]
|
|
itemWidth, itemHeight := itemSprite.GetCurrentFrameSize()
|
|
x := eq.x + ((eq.width - itemWidth) / 2)
|
|
y := eq.y - ((eq.height - itemHeight) / 2)
|
|
|
|
g.renderItem(eq.item, target, x, y)
|
|
}
|
|
}
|