2020-06-13 18:32:09 -04:00
|
|
|
package d2client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2game/d2player"
|
2020-06-13 18:32:09 -04:00
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-13 18:32:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2clientconnectiontype"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2localclient"
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2remoteclient"
|
2020-06-13 18:32:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GameClient struct {
|
|
|
|
clientConnection ClientConnection
|
2020-06-18 14:11:04 -04:00
|
|
|
connectionType d2clientconnectiontype.ClientConnectionType
|
|
|
|
GameState *d2player.PlayerState
|
2020-06-13 18:32:09 -04:00
|
|
|
MapEngine *d2map.MapEngine
|
|
|
|
PlayerId string
|
|
|
|
Players map[string]*d2map.Player
|
2020-06-19 02:19:27 -04:00
|
|
|
Seed int64
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Create(connectionType d2clientconnectiontype.ClientConnectionType) (*GameClient, error) {
|
|
|
|
result := &GameClient{
|
2020-06-19 02:19:27 -04:00
|
|
|
MapEngine: d2map.CreateMapEngine(0),
|
2020-06-18 14:11:04 -04:00
|
|
|
Players: make(map[string]*d2map.Player, 0),
|
|
|
|
connectionType: connectionType,
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch connectionType {
|
2020-06-18 14:11:04 -04:00
|
|
|
case d2clientconnectiontype.LANClient:
|
|
|
|
result.clientConnection = d2remoteclient.Create()
|
|
|
|
case d2clientconnectiontype.LANServer:
|
|
|
|
result.clientConnection = d2localclient.Create(true)
|
2020-06-13 18:32:09 -04:00
|
|
|
case d2clientconnectiontype.Local:
|
2020-06-18 14:11:04 -04:00
|
|
|
result.clientConnection = d2localclient.Create(false)
|
2020-06-13 18:32:09 -04:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown client connection type specified: %d", connectionType)
|
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
result.clientConnection.SetClientListener(result)
|
2020-06-13 18:32:09 -04:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
func (g *GameClient) Open(connectionString string, saveFilePath string) error {
|
|
|
|
return g.clientConnection.Open(connectionString, saveFilePath)
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GameClient) Close() error {
|
|
|
|
return g.clientConnection.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GameClient) Destroy() error {
|
|
|
|
return g.clientConnection.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GameClient) OnPacketReceived(packet d2netpacket.NetPacket) error {
|
|
|
|
switch packet.PacketType {
|
|
|
|
case d2netpackettype.GenerateMap:
|
|
|
|
mapData := packet.PacketData.(d2netpacket.GenerateMapPacket)
|
2020-06-19 02:19:27 -04:00
|
|
|
switch mapData.RegionType {
|
|
|
|
case d2enum.RegionAct1Town:
|
|
|
|
g.MapEngine.GenerateAct1Overworld(true)
|
|
|
|
}
|
2020-06-13 18:32:09 -04:00
|
|
|
break
|
|
|
|
case d2netpackettype.UpdateServerInfo:
|
|
|
|
serverInfo := packet.PacketData.(d2netpacket.UpdateServerInfoPacket)
|
|
|
|
g.MapEngine.SetSeed(serverInfo.Seed)
|
|
|
|
g.PlayerId = serverInfo.PlayerId
|
2020-06-19 02:19:27 -04:00
|
|
|
g.Seed = serverInfo.Seed
|
2020-06-13 18:32:09 -04:00
|
|
|
log.Printf("Player id set to %s", serverInfo.PlayerId)
|
|
|
|
break
|
|
|
|
case d2netpackettype.AddPlayer:
|
|
|
|
player := packet.PacketData.(d2netpacket.AddPlayerPacket)
|
2020-06-18 14:11:04 -04:00
|
|
|
newPlayer := d2map.CreatePlayer(player.Id, player.Name, player.X, player.Y, 0, player.HeroType, player.Equipment)
|
2020-06-13 18:32:09 -04:00
|
|
|
g.Players[newPlayer.Id] = newPlayer
|
|
|
|
g.MapEngine.AddEntity(newPlayer)
|
|
|
|
break
|
|
|
|
case d2netpackettype.MovePlayer:
|
|
|
|
movePlayer := packet.PacketData.(d2netpacket.MovePlayerPacket)
|
|
|
|
player := g.Players[movePlayer.PlayerId]
|
|
|
|
path, _, found := g.MapEngine.PathFind(movePlayer.StartX, movePlayer.StartY, movePlayer.DestX, movePlayer.DestY)
|
|
|
|
if found {
|
|
|
|
player.AnimatedComposite.SetPath(path, func() {
|
|
|
|
player.AnimatedComposite.SetAnimationMode(
|
|
|
|
d2enum.AnimationModeObjectNeutral.String(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
log.Fatalf("Invalid packet type: %d", packet.PacketType)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GameClient) SendPacketToServer(packet d2netpacket.NetPacket) error {
|
|
|
|
return g.clientConnection.SendPacketToServer(packet)
|
|
|
|
}
|