1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-12-26 20:16:45 -05:00

Prevent player movement and casting if current spell cast is not fini… (#729)

* Prevent player movement and casting if current spell cast is not finished yet

* Properly format the fields of Player struct

Co-authored-by: Presiyan Ivanov <presiyan-ivanov@users.noreply.github.com>
This commit is contained in:
presiyan-ivanov 2020-09-14 21:49:31 +03:00 committed by GitHub
parent 7f6ae1b785
commit 8a670d7482
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 38 deletions

View File

@ -453,9 +453,11 @@ func (a *App) allocRate(totalAlloc uint64, fps float64) float64 {
} }
func (a *App) dumpHeap() { func (a *App) dumpHeap() {
if _, err := os.Stat("./pprof/"); os.IsNotExist(err) {
if err := os.Mkdir("./pprof/", 0750); err != nil { if err := os.Mkdir("./pprof/", 0750); err != nil {
log.Fatal(err) log.Fatal(err)
} }
}
fileOut, _ := os.Create("./pprof/heap.pprof") fileOut, _ := os.Create("./pprof/heap.pprof")

View File

@ -57,6 +57,12 @@ func (m *mapEntity) ClearPath() {
m.path = nil m.path = nil
} }
// StopMoving will clear the path and target of the entity.
func (m *mapEntity) StopMoving() {
m.ClearPath()
m.setTarget(m.Position, nil)
}
// SetSpeed sets the entity movement speed. // SetSpeed sets the entity movement speed.
func (m *mapEntity) SetSpeed(speed float64) { func (m *mapEntity) SetSpeed(speed float64) {
m.Speed = speed m.Speed = speed

View File

@ -25,9 +25,7 @@ type Player struct {
isRunToggled bool isRunToggled bool
isRunning bool isRunning bool
isCasting bool isCasting bool
onFinishedCasting func()
// nameLabel d2ui.Label
} }
// run speed should be walkspeed * 1.5, since in the original game it is 6 yards walk and 9 yards run. // run speed should be walkspeed * 1.5, since in the original game it is 6 yards walk and 9 yards run.
@ -84,6 +82,11 @@ func (p *Player) Advance(tickTime float64) {
if p.IsCasting() && p.composite.GetPlayedCount() >= 1 { if p.IsCasting() && p.composite.GetPlayedCount() >= 1 {
p.isCasting = false p.isCasting = false
if p.onFinishedCasting != nil {
p.onFinishedCasting()
p.onFinishedCasting = nil
}
if err := p.SetAnimationMode(p.GetAnimationMode()); err != nil { if err := p.SetAnimationMode(p.GetAnimationMode()); err != nil {
fmt.Printf("failed to set animationMode to: %d, err: %v\n", p.GetAnimationMode(), err) fmt.Printf("failed to set animationMode to: %d, err: %v\n", p.GetAnimationMode(), err)
} }
@ -162,6 +165,11 @@ func (p *Player) rotate(direction int) {
} }
} }
// SetDirection will rotate the player and change the animation
func (p *Player) SetDirection(direction int) {
p.rotate(direction)
}
// Name returns the player name. // Name returns the player name.
func (p *Player) Name() string { func (p *Player) Name() string {
return p.name return p.name
@ -172,10 +180,11 @@ func (p *Player) IsCasting() bool {
return p.isCasting return p.isCasting
} }
// SetCasting sets a flag indicating the player is casting a skill and // StartCasting sets a flag indicating the player is casting a skill and
// sets the animation mode to the casting animation. // sets the animation mode to the casting animation.
func (p *Player) SetCasting() { func (p *Player) StartCasting(onFinishedCasting func()) {
p.isCasting = true p.isCasting = true
p.onFinishedCasting = onFinishedCasting
if err := p.SetAnimationMode(d2enum.PlayerAnimationModeCast); err != nil { if err := p.SetAnimationMode(d2enum.PlayerAnimationModeCast); err != nil {
fmtStr := "failed to set animationMode of player: %s to: %d, err: %v\n" fmtStr := "failed to set animationMode of player: %s to: %d, err: %v\n"
fmt.Printf(fmtStr, p.ID(), d2enum.PlayerAnimationModeCast, err) fmt.Printf(fmtStr, p.ID(), d2enum.PlayerAnimationModeCast, err)

View File

@ -293,7 +293,7 @@ func (g *GameControls) OnMouseButtonRepeat(event d2interface.MouseEvent) bool {
shouldDoLeft := lastLeft >= mouseBtnActionsTreshhold shouldDoLeft := lastLeft >= mouseBtnActionsTreshhold
shouldDoRight := lastRight >= mouseBtnActionsTreshhold shouldDoRight := lastRight >= mouseBtnActionsTreshhold
if isLeft && shouldDoLeft && inRect { if isLeft && shouldDoLeft && inRect && !g.hero.IsCasting() {
g.lastLeftBtnActionTime = now g.lastLeftBtnActionTime = now
g.inputListener.OnPlayerMove(px, py) g.inputListener.OnPlayerMove(px, py)
@ -315,7 +315,7 @@ func (g *GameControls) OnMouseButtonRepeat(event d2interface.MouseEvent) bool {
return true return true
} }
if isRight && shouldDoRight && inRect { if isRight && shouldDoRight && inRect && !g.hero.IsCasting() {
g.lastRightBtnActionTime = now g.lastRightBtnActionTime = now
g.inputListener.OnPlayerCast(g.missileID, px, py) g.inputListener.OnPlayerCast(g.missileID, px, py)
@ -360,7 +360,7 @@ func (g *GameControls) OnMouseButtonDown(event d2interface.MouseEvent) bool {
px = float64(int(px*10)) / 10.0 px = float64(int(px*10)) / 10.0
py = float64(int(py*10)) / 10.0 py = float64(int(py*10)) / 10.0
if event.Button() == d2enum.MouseButtonLeft && !g.isInActiveMenusRect(mx, my) { if event.Button() == d2enum.MouseButtonLeft && !g.isInActiveMenusRect(mx, my) && !g.hero.IsCasting() {
g.lastLeftBtnActionTime = d2util.Now() g.lastLeftBtnActionTime = d2util.Now()
g.inputListener.OnPlayerMove(px, py) g.inputListener.OnPlayerMove(px, py)
@ -368,7 +368,7 @@ func (g *GameControls) OnMouseButtonDown(event d2interface.MouseEvent) bool {
return true return true
} }
if event.Button() == d2enum.MouseButtonRight && !g.isInActiveMenusRect(mx, my) { if event.Button() == d2enum.MouseButtonRight && !g.isInActiveMenusRect(mx, my) && !g.hero.IsCasting() {
g.lastRightBtnActionTime = d2util.Now() g.lastRightBtnActionTime = d2util.Now()
g.inputListener.OnPlayerCast(g.missileID, px, py) g.inputListener.OnPlayerCast(g.missileID, px, py)

View File

@ -229,12 +229,7 @@ func (g *GameClient) handleMovePlayerPacket(packet d2netpacket.NetPacket) error
return return
} }
regionType := tile.RegionType player.SetIsInTown(tile.RegionType == d2enum.RegionAct1Town)
if regionType == d2enum.RegionAct1Town {
player.SetIsInTown(true)
} else {
player.SetIsInTown(false)
}
err := player.SetAnimationMode(player.GetAnimationMode()) err := player.SetAnimationMode(player.GetAnimationMode())
@ -255,9 +250,20 @@ func (g *GameClient) handleCastSkillPacket(packet d2netpacket.NetPacket) error {
} }
player := g.Players[playerCast.SourceEntityID] player := g.Players[playerCast.SourceEntityID]
player.StopMoving()
player.SetCasting() castX := playerCast.TargetX * numSubtilesPerTile
player.ClearPath() castY := playerCast.TargetY * numSubtilesPerTile
rads := d2math.GetRadiansBetween(
player.Position.X(),
player.Position.Y(),
castX,
castY,
)
direction := player.Position.DirectionTo(*d2vector.NewVector(castX, castY))
player.SetDirection(direction)
// currently hardcoded to missile skill // currently hardcoded to missile skill
missile, err := g.MapEngine.NewMissile( missile, err := g.MapEngine.NewMissile(
@ -270,18 +276,13 @@ func (g *GameClient) handleCastSkillPacket(packet d2netpacket.NetPacket) error {
return err return err
} }
rads := d2math.GetRadiansBetween(
player.Position.X(),
player.Position.Y(),
playerCast.TargetX*numSubtilesPerTile,
playerCast.TargetY*numSubtilesPerTile,
)
missile.SetRadians(rads, func() { missile.SetRadians(rads, func() {
g.MapEngine.RemoveEntity(missile) g.MapEngine.RemoveEntity(missile)
}) })
player.StartCasting(func() {
g.MapEngine.AddEntity(missile) g.MapEngine.AddEntity(missile)
})
return nil return nil
} }