2020-06-13 20:36:20 -04:00
|
|
|
package d2gamescreen
|
2019-11-11 23:48:55 -05:00
|
|
|
|
|
|
|
import (
|
2019-12-21 20:53:18 -05:00
|
|
|
"image/color"
|
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
|
|
|
|
|
2020-06-13 18:32:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2game/d2player"
|
2020-02-01 18:55:56 -05:00
|
|
|
|
2020-06-19 02:19:27 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2audio"
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map"
|
2020-06-13 18:32:09 -04:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2input"
|
2020-02-01 20:39:28 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
|
2019-11-11 23:48:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Game struct {
|
2020-06-13 18:32:09 -04:00
|
|
|
//pentSpinLeft *d2ui.Sprite
|
|
|
|
//pentSpinRight *d2ui.Sprite
|
|
|
|
//testLabel d2ui.Label
|
2020-06-19 02:19:27 -04:00
|
|
|
gameClient *d2client.GameClient
|
|
|
|
mapRenderer *d2map.MapRenderer
|
|
|
|
gameControls *d2player.GameControls // TODO: Hack
|
|
|
|
localPlayer *d2map.Player
|
|
|
|
lastLevelType int
|
|
|
|
ticksSinceLevelCheck float64
|
2019-11-11 23:48:55 -05:00
|
|
|
}
|
|
|
|
|
2020-06-13 18:32:09 -04:00
|
|
|
func CreateGame(gameClient *d2client.GameClient) *Game {
|
|
|
|
return &Game{
|
2020-06-19 02:19:27 -04:00
|
|
|
gameClient: gameClient,
|
|
|
|
gameControls: nil,
|
|
|
|
localPlayer: nil,
|
|
|
|
lastLevelType: -1,
|
|
|
|
ticksSinceLevelCheck: 0,
|
|
|
|
mapRenderer: d2map.CreateMapRenderer(gameClient.MapEngine),
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
2019-11-11 23:48:55 -05:00
|
|
|
}
|
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
func (v *Game) OnLoad() error {
|
2020-06-19 02:19:27 -04:00
|
|
|
d2audio.PlayBGM("")
|
2020-02-08 21:02:37 -05:00
|
|
|
return nil
|
2019-11-11 23:48:55 -05:00
|
|
|
}
|
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
func (v *Game) OnUnload() error {
|
2020-06-13 18:32:09 -04:00
|
|
|
d2input.UnbindHandler(v.gameControls) // TODO: hack
|
2020-02-08 21:02:37 -05:00
|
|
|
return nil
|
2019-11-11 23:48:55 -05:00
|
|
|
}
|
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
func (v *Game) Render(screen d2render.Surface) error {
|
2019-12-28 16:46:08 -05:00
|
|
|
screen.Clear(color.Black)
|
2020-06-13 18:32:09 -04:00
|
|
|
v.mapRenderer.Render(screen)
|
|
|
|
if v.gameControls != nil {
|
|
|
|
v.gameControls.Render(screen)
|
|
|
|
}
|
2020-02-08 21:02:37 -05:00
|
|
|
return nil
|
2019-11-11 23:48:55 -05:00
|
|
|
}
|
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
func (v *Game) Advance(tickTime float64) error {
|
2020-06-13 18:32:09 -04:00
|
|
|
v.gameClient.MapEngine.Advance(tickTime) // TODO: Hack
|
|
|
|
|
2020-06-19 02:19:27 -04:00
|
|
|
v.ticksSinceLevelCheck += tickTime
|
2020-06-20 00:40:49 -04:00
|
|
|
if v.ticksSinceLevelCheck > 1.0 {
|
2020-06-19 02:19:27 -04:00
|
|
|
v.ticksSinceLevelCheck = 0
|
|
|
|
if v.localPlayer != nil {
|
|
|
|
region := v.gameClient.MapEngine.GetRegionAtTile(v.localPlayer.TileX, v.localPlayer.TileY)
|
|
|
|
if region != nil {
|
|
|
|
levelType := region.GetLevelType().Id
|
|
|
|
if levelType != v.lastLevelType {
|
|
|
|
v.lastLevelType = levelType
|
|
|
|
switch levelType {
|
|
|
|
case 1: // Rogue encampent
|
2020-06-20 00:40:49 -04:00
|
|
|
v.localPlayer.SetIsInTown(true)
|
2020-06-19 02:19:27 -04:00
|
|
|
d2audio.PlayBGM("/data/global/music/Act1/town1.wav")
|
|
|
|
case 2: // Blood Moore
|
2020-06-20 00:40:49 -04:00
|
|
|
v.localPlayer.SetIsInTown(false)
|
2020-06-19 02:19:27 -04:00
|
|
|
d2audio.PlayBGM("/data/global/music/Act1/wild.wav")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 18:32:09 -04:00
|
|
|
// Bind the game controls to the player once it exists
|
|
|
|
if v.gameControls == nil {
|
|
|
|
for _, player := range v.gameClient.Players {
|
|
|
|
if player.Id != v.gameClient.PlayerId {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v.localPlayer = player
|
2020-06-18 14:11:04 -04:00
|
|
|
v.gameControls = d2player.NewGameControls(player, v.gameClient.MapEngine, v.mapRenderer, v)
|
2020-06-13 18:32:09 -04:00
|
|
|
v.gameControls.Load()
|
|
|
|
d2input.BindHandler(v.gameControls)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the camera to focus on the player
|
|
|
|
if v.localPlayer != nil {
|
|
|
|
rx, ry := v.mapRenderer.WorldToOrtho(v.localPlayer.AnimatedComposite.LocationX/5, v.localPlayer.AnimatedComposite.LocationY/5)
|
|
|
|
v.mapRenderer.MoveCameraTo(rx, ry)
|
|
|
|
}
|
2020-02-08 21:02:37 -05:00
|
|
|
return nil
|
2019-11-11 23:48:55 -05:00
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
|
|
|
|
func (v *Game) OnPlayerMove(x, y float64) {
|
|
|
|
heroPosX := v.localPlayer.AnimatedComposite.LocationX / 5.0
|
|
|
|
heroPosY := v.localPlayer.AnimatedComposite.LocationY / 5.0
|
|
|
|
v.gameClient.SendPacketToServer(d2netpacket.CreateMovePlayerPacket(v.gameClient.PlayerId, heroPosX, heroPosY, x, y))
|
|
|
|
}
|