49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package types
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"io"
|
|
)
|
|
|
|
// EquipmentItem represents an equipment item with model and color information
|
|
// Matches EQ2_EquipmentItem from C++ code
|
|
type EquipmentItem struct {
|
|
Type uint16 // Model/item type ID
|
|
Color Color // RGB color for the item (3 bytes)
|
|
Highlight Color // RGB highlight color for the item (3 bytes)
|
|
}
|
|
|
|
// Serialize writes the equipment item to a writer
|
|
func (e *EquipmentItem) Serialize(w io.Writer) error {
|
|
if err := binary.Write(w, binary.LittleEndian, e.Type); err != nil {
|
|
return err
|
|
}
|
|
if err := e.Color.Serialize(w); err != nil {
|
|
return err
|
|
}
|
|
return e.Highlight.Serialize(w)
|
|
}
|
|
|
|
// SerializeToBytes writes the equipment item to a byte slice at the given offset
|
|
func (e *EquipmentItem) SerializeToBytes(dest []byte, offset *uint32) {
|
|
binary.LittleEndian.PutUint16(dest[*offset:], e.Type)
|
|
*offset += 2
|
|
e.Color.SerializeToBytes(dest, offset)
|
|
e.Highlight.SerializeToBytes(dest, offset)
|
|
}
|
|
|
|
// Size returns the serialized size of the equipment item
|
|
func (e *EquipmentItem) Size() uint32 {
|
|
return 2 + e.Color.Size() + e.Highlight.Size() // 2 bytes for type + 3 bytes color + 3 bytes highlight = 8 bytes total
|
|
}
|
|
|
|
// Deserialize reads an equipment item from a reader
|
|
func (e *EquipmentItem) Deserialize(r io.Reader) error {
|
|
if err := binary.Read(r, binary.LittleEndian, &e.Type); err != nil {
|
|
return err
|
|
}
|
|
if err := e.Color.Deserialize(r); err != nil {
|
|
return err
|
|
}
|
|
return e.Highlight.Deserialize(r)
|
|
} |