1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-05 09:47:18 -05:00
OpenDiablo2/d2game/d2gamescreen/game.go

114 lines
3.1 KiB
Go
Raw Normal View History

package d2gamescreen
import (
"image/color"
2020-06-18 14:11:04 -04:00
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
"github.com/OpenDiablo2/OpenDiablo2/d2game/d2player"
2020-06-19 02:19:27 -04:00
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2audio"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map"
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2input"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
)
type Game struct {
//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
}
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-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
}
2020-02-08 21:02:37 -05:00
func (v *Game) OnUnload() error {
d2input.UnbindHandler(v.gameControls) // TODO: hack
2020-02-08 21:02:37 -05:00
return nil
}
2020-02-08 21:02:37 -05:00
func (v *Game) Render(screen d2render.Surface) error {
screen.Clear(color.Black)
v.mapRenderer.Render(screen)
if v.gameControls != nil {
v.gameControls.Render(screen)
}
2020-02-08 21:02:37 -05:00
return nil
}
2020-02-08 21:02:37 -05:00
func (v *Game) Advance(tickTime float64) error {
v.gameClient.MapEngine.Advance(tickTime) // TODO: Hack
2020-06-19 02:19:27 -04:00
v.ticksSinceLevelCheck += tickTime
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
v.localPlayer.SetIsInTown(true)
2020-06-19 02:19:27 -04:00
d2audio.PlayBGM("/data/global/music/Act1/town1.wav")
case 2: // Blood Moore
v.localPlayer.SetIsInTown(false)
2020-06-19 02:19:27 -04:00
d2audio.PlayBGM("/data/global/music/Act1/wild.wav")
}
}
}
}
}
// 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)
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
}
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))
}