2019-12-16 11:04:39 -05:00
|
|
|
package d2player
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common/d2resource"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2render"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2render/d2mapengine"
|
2019-12-28 16:46:08 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2render/d2surface"
|
2019-12-16 11:04:39 -05:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
2019-12-28 23:32:53 -05:00
|
|
|
"github.com/hajimehoshi/ebiten/inpututil"
|
2019-12-16 11:04:39 -05:00
|
|
|
)
|
|
|
|
|
2019-12-28 23:32:53 -05:00
|
|
|
type Panel interface {
|
|
|
|
IsOpen() bool
|
|
|
|
Toggle()
|
|
|
|
Open()
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2019-12-16 11:04:39 -05:00
|
|
|
type GameControls struct {
|
2019-12-21 20:53:18 -05:00
|
|
|
hero *d2core.Hero
|
|
|
|
mapEngine *d2mapengine.MapEngine
|
2019-12-28 23:32:53 -05:00
|
|
|
inventory *Inventory
|
2019-12-16 11:04:39 -05:00
|
|
|
|
|
|
|
// UI
|
|
|
|
globeSprite *d2render.Sprite
|
|
|
|
mainPanel *d2render.Sprite
|
|
|
|
menuButton *d2render.Sprite
|
|
|
|
skillIcon *d2render.Sprite
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func NewGameControls(hero *d2core.Hero, mapEngine *d2mapengine.MapEngine) *GameControls {
|
2019-12-16 11:04:39 -05:00
|
|
|
return &GameControls{
|
2019-12-21 20:53:18 -05:00
|
|
|
hero: hero,
|
|
|
|
mapEngine: mapEngine,
|
2019-12-28 23:32:53 -05:00
|
|
|
inventory: NewInventory(),
|
2019-12-16 11:04:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:53 -05:00
|
|
|
func (g *GameControls) Update(tickTime float64) {
|
2019-12-16 11:04:39 -05:00
|
|
|
|
|
|
|
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
|
|
|
px, py := g.mapEngine.ScreenToWorld(ebiten.CursorPosition())
|
|
|
|
g.hero.AnimatedEntity.SetTarget(px*5, py*5, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
arrowDistance := 1.0
|
|
|
|
moveX := 0.0
|
|
|
|
moveY := 0.0
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyW) || ebiten.IsKeyPressed(ebiten.KeyUp) {
|
|
|
|
moveY -= arrowDistance
|
|
|
|
moveX -= arrowDistance
|
|
|
|
}
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyS) || ebiten.IsKeyPressed(ebiten.KeyDown) {
|
|
|
|
moveY += arrowDistance
|
|
|
|
moveX += arrowDistance
|
|
|
|
}
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyA) || ebiten.IsKeyPressed(ebiten.KeyLeft) {
|
|
|
|
moveY += arrowDistance
|
|
|
|
moveX -= arrowDistance
|
|
|
|
}
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyD) || ebiten.IsKeyPressed(ebiten.KeyRight) {
|
|
|
|
moveY -= arrowDistance
|
|
|
|
moveX += arrowDistance
|
|
|
|
}
|
|
|
|
|
|
|
|
if moveY != 0 || moveX != 0 {
|
|
|
|
g.hero.AnimatedEntity.SetTarget(g.hero.AnimatedEntity.LocationX+moveX, g.hero.AnimatedEntity.LocationY+moveY, 1)
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:53 -05:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyI) {
|
|
|
|
g.inventory.Toggle()
|
|
|
|
}
|
|
|
|
|
2019-12-16 11:04:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GameControls) Load() {
|
2019-12-21 20:53:18 -05:00
|
|
|
g.globeSprite, _ = d2render.LoadSprite(d2resource.GameGlobeOverlap, d2resource.PaletteSky)
|
|
|
|
g.mainPanel, _ = d2render.LoadSprite(d2resource.GamePanels, d2resource.PaletteSky)
|
|
|
|
g.menuButton, _ = d2render.LoadSprite(d2resource.MenuButton, d2resource.PaletteSky)
|
|
|
|
g.skillIcon, _ = d2render.LoadSprite(d2resource.GenericSkills, d2resource.PaletteSky)
|
2019-12-28 23:32:53 -05:00
|
|
|
g.inventory.Load()
|
2019-12-16 11:04:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: consider caching the panels to single image that is reused.
|
2019-12-28 16:46:08 -05:00
|
|
|
func (g *GameControls) Render(target *d2surface.Surface) {
|
2019-12-28 23:32:53 -05:00
|
|
|
g.inventory.Render(target)
|
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
width, height := target.GetSize()
|
2019-12-21 20:53:18 -05:00
|
|
|
offset := int(0)
|
2019-12-16 11:04:39 -05:00
|
|
|
|
|
|
|
// Left globe holder
|
2019-12-21 20:53:18 -05:00
|
|
|
g.mainPanel.SetCurrentFrame(0)
|
|
|
|
w, _ := g.mainPanel.GetCurrentFrameSize()
|
2019-12-28 23:32:53 -05:00
|
|
|
g.mainPanel.SetPosition(offset, height)
|
2019-12-21 20:53:18 -05:00
|
|
|
g.mainPanel.Render(target)
|
2019-12-16 11:04:39 -05:00
|
|
|
|
|
|
|
// Left globe
|
2019-12-21 20:53:18 -05:00
|
|
|
g.globeSprite.SetCurrentFrame(0)
|
2019-12-28 23:32:53 -05:00
|
|
|
g.globeSprite.SetPosition(offset+28, height-5)
|
2019-12-21 20:53:18 -05:00
|
|
|
g.globeSprite.Render(target)
|
2019-12-16 11:04:39 -05:00
|
|
|
offset += w
|
|
|
|
|
|
|
|
// Left skill
|
2019-12-21 20:53:18 -05:00
|
|
|
g.skillIcon.SetCurrentFrame(2)
|
|
|
|
w, _ = g.skillIcon.GetCurrentFrameSize()
|
2019-12-28 23:32:53 -05:00
|
|
|
g.skillIcon.SetPosition(offset, height)
|
2019-12-21 20:53:18 -05:00
|
|
|
g.skillIcon.Render(target)
|
2019-12-16 11:04:39 -05:00
|
|
|
offset += w
|
|
|
|
|
|
|
|
// Left skill selector
|
2019-12-21 20:53:18 -05:00
|
|
|
g.mainPanel.SetCurrentFrame(1)
|
|
|
|
w, _ = g.mainPanel.GetCurrentFrameSize()
|
2019-12-28 23:32:53 -05:00
|
|
|
g.mainPanel.SetPosition(offset, height)
|
2019-12-21 20:53:18 -05:00
|
|
|
g.mainPanel.Render(target)
|
2019-12-16 11:04:39 -05:00
|
|
|
offset += w
|
|
|
|
|
|
|
|
// Stamina
|
2019-12-21 20:53:18 -05:00
|
|
|
g.mainPanel.SetCurrentFrame(2)
|
|
|
|
w, _ = g.mainPanel.GetCurrentFrameSize()
|
2019-12-28 23:32:53 -05:00
|
|
|
g.mainPanel.SetPosition(offset, height)
|
2019-12-21 20:53:18 -05:00
|
|
|
g.mainPanel.Render(target)
|
2019-12-16 11:04:39 -05:00
|
|
|
offset += w
|
|
|
|
|
|
|
|
// Center menu button
|
2019-12-21 20:53:18 -05:00
|
|
|
g.menuButton.SetCurrentFrame(0)
|
|
|
|
w, _ = g.mainPanel.GetCurrentFrameSize()
|
|
|
|
g.menuButton.SetPosition((width/2)-8, height-16)
|
|
|
|
g.menuButton.Render(target)
|
2019-12-16 11:04:39 -05:00
|
|
|
|
|
|
|
// Potions
|
2019-12-21 20:53:18 -05:00
|
|
|
g.mainPanel.SetCurrentFrame(3)
|
|
|
|
w, _ = g.mainPanel.GetCurrentFrameSize()
|
2019-12-28 23:32:53 -05:00
|
|
|
g.mainPanel.SetPosition(offset, height)
|
2019-12-21 20:53:18 -05:00
|
|
|
g.mainPanel.Render(target)
|
2019-12-16 11:04:39 -05:00
|
|
|
offset += w
|
|
|
|
|
|
|
|
// Right skill selector
|
2019-12-21 20:53:18 -05:00
|
|
|
g.mainPanel.SetCurrentFrame(4)
|
|
|
|
w, _ = g.mainPanel.GetCurrentFrameSize()
|
2019-12-28 23:32:53 -05:00
|
|
|
g.mainPanel.SetPosition(offset, height)
|
2019-12-21 20:53:18 -05:00
|
|
|
g.mainPanel.Render(target)
|
2019-12-16 11:04:39 -05:00
|
|
|
offset += w
|
|
|
|
|
|
|
|
// Right skill
|
2019-12-21 20:53:18 -05:00
|
|
|
g.skillIcon.SetCurrentFrame(10)
|
|
|
|
w, _ = g.skillIcon.GetCurrentFrameSize()
|
2019-12-28 23:32:53 -05:00
|
|
|
g.skillIcon.SetPosition(offset, height)
|
2019-12-21 20:53:18 -05:00
|
|
|
g.skillIcon.Render(target)
|
2019-12-16 11:04:39 -05:00
|
|
|
offset += w
|
|
|
|
|
|
|
|
// Right globe holder
|
2019-12-21 20:53:18 -05:00
|
|
|
g.mainPanel.SetCurrentFrame(5)
|
|
|
|
w, _ = g.mainPanel.GetCurrentFrameSize()
|
2019-12-28 23:32:53 -05:00
|
|
|
g.mainPanel.SetPosition(offset, height)
|
2019-12-21 20:53:18 -05:00
|
|
|
g.mainPanel.Render(target)
|
2019-12-16 11:04:39 -05:00
|
|
|
|
|
|
|
// Right globe
|
2019-12-21 20:53:18 -05:00
|
|
|
g.globeSprite.SetCurrentFrame(1)
|
2019-12-28 23:32:53 -05:00
|
|
|
g.globeSprite.SetPosition(offset+8, height-8)
|
2019-12-21 20:53:18 -05:00
|
|
|
g.globeSprite.Render(target)
|
2019-12-16 11:04:39 -05:00
|
|
|
|
|
|
|
}
|