2020-11-28 18:49:18 -05:00
|
|
|
package d2systems
|
|
|
|
|
|
|
|
import (
|
2020-12-02 01:24:31 -05:00
|
|
|
"math"
|
|
|
|
"time"
|
|
|
|
|
2020-11-28 18:49:18 -05:00
|
|
|
"github.com/gravestench/akara"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
sceneKeyMouseCursor = "Mouse Cursor"
|
|
|
|
)
|
|
|
|
|
2020-12-02 01:24:31 -05:00
|
|
|
const (
|
|
|
|
fadeTimeout = time.Second * 4
|
|
|
|
fadeTime = time.Second
|
|
|
|
)
|
|
|
|
|
2020-11-28 18:49:18 -05:00
|
|
|
// NewMouseCursorScene creates a new main menu scene. This is the first screen that the user
|
|
|
|
// will see when launching the game.
|
|
|
|
func NewMouseCursorScene() *MouseCursorScene {
|
|
|
|
scene := &MouseCursorScene{
|
|
|
|
BaseScene: NewBaseScene(sceneKeyMouseCursor),
|
|
|
|
}
|
|
|
|
|
|
|
|
return scene
|
|
|
|
}
|
|
|
|
|
|
|
|
// static check that MouseCursorScene implements the scene interface
|
|
|
|
var _ d2interface.Scene = &MouseCursorScene{}
|
|
|
|
|
|
|
|
// MouseCursorScene represents the game's main menu, where users can select single or multi player,
|
|
|
|
// or start the map engine test.
|
|
|
|
type MouseCursorScene struct {
|
|
|
|
*BaseScene
|
2020-12-02 01:24:31 -05:00
|
|
|
booted bool
|
|
|
|
cursor akara.EID
|
|
|
|
lastTimeMoved time.Time
|
2020-11-28 18:49:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init the main menu scene
|
|
|
|
func (s *MouseCursorScene) Init(world *akara.World) {
|
|
|
|
s.World = world
|
|
|
|
|
|
|
|
s.Info("initializing ...")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MouseCursorScene) boot() {
|
|
|
|
if !s.BaseScene.booted {
|
|
|
|
s.BaseScene.boot()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.createMouseCursor()
|
|
|
|
|
|
|
|
s.booted = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MouseCursorScene) createMouseCursor() {
|
|
|
|
s.Info("creating mouse cursor")
|
|
|
|
s.cursor = s.Add.Sprite(0, 0, d2resource.CursorDefault, d2resource.PaletteUnits)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the main menu scene
|
|
|
|
func (s *MouseCursorScene) Update() {
|
2020-12-01 05:19:03 -05:00
|
|
|
for _, id := range s.Viewports {
|
|
|
|
s.AddPriority(id).Priority = scenePriorityMouseCursor
|
|
|
|
}
|
|
|
|
|
2020-11-28 18:49:18 -05:00
|
|
|
if s.Paused() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.booted {
|
|
|
|
s.boot()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.updateCursorPosition()
|
2020-12-02 01:24:31 -05:00
|
|
|
s.handleCursorFade()
|
2020-11-28 18:49:18 -05:00
|
|
|
|
|
|
|
s.BaseScene.Update()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MouseCursorScene) updateCursorPosition() {
|
2020-12-01 05:19:03 -05:00
|
|
|
position, found := s.GetPosition(s.cursor)
|
2020-11-28 18:49:18 -05:00
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-02 01:24:31 -05:00
|
|
|
cx, cy := s.CursorPosition()
|
|
|
|
|
|
|
|
if int(position.X) != cx || int(position.Y) != cy {
|
|
|
|
s.lastTimeMoved = time.Now()
|
|
|
|
}
|
|
|
|
|
2020-12-01 05:19:03 -05:00
|
|
|
position.X, position.Y = float64(cx), float64(cy)
|
2020-11-28 18:49:18 -05:00
|
|
|
}
|
2020-12-02 01:24:31 -05:00
|
|
|
|
|
|
|
func (s *MouseCursorScene) handleCursorFade() {
|
|
|
|
alpha, found := s.GetAlpha(s.cursor)
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldFadeOut := time.Now().Sub(s.lastTimeMoved) > fadeTimeout
|
|
|
|
|
|
|
|
if shouldFadeOut {
|
|
|
|
alpha.Alpha = math.Max(alpha.Alpha*0.825, 0)
|
|
|
|
} else {
|
|
|
|
alpha.Alpha = math.Min(alpha.Alpha+0.125, 1)
|
|
|
|
}
|
|
|
|
}
|