1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-02 09:17:19 -04:00
OpenDiablo2/d2networking/d2client/game_client.go

104 lines
3.4 KiB
Go
Raw Normal View History

package d2client
import (
"fmt"
"log"
2020-06-18 14:11:04 -04:00
"github.com/OpenDiablo2/OpenDiablo2/d2game/d2player"
2020-06-18 14:11:04 -04:00
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"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"
"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
MapEngine *d2map.MapEngine
PlayerId string
Players map[string]*d2map.Player
2020-06-19 02:19:27 -04:00
Seed int64
}
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,
}
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)
case d2clientconnectiontype.Local:
2020-06-18 14:11:04 -04:00
result.clientConnection = d2localclient.Create(false)
default:
return nil, fmt.Errorf("unknown client connection type specified: %d", connectionType)
}
2020-06-18 14:11:04 -04:00
result.clientConnection.SetClientListener(result)
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)
}
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)
}
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
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)
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)
}