✨ Slide and collide!
This commit is contained in:
@@ -19,6 +19,10 @@ type Player struct {
|
||||
}
|
||||
|
||||
func (p *Player) Update() {
|
||||
if !p.Velocity.IsZero() {
|
||||
p.SlideByVelocity()
|
||||
}
|
||||
|
||||
keys := sdl.GetKeyboardState()
|
||||
if keys[sdl.SCANCODE_UP] == 1 || keys[sdl.SCANCODE_W] == 1 {
|
||||
p.Move(0, -1)
|
||||
@@ -33,14 +37,15 @@ func (p *Player) Update() {
|
||||
p.Move(1, 0)
|
||||
}
|
||||
|
||||
p.Move(0, 0)
|
||||
p.BaseObject.Update()
|
||||
}
|
||||
|
||||
func (p *Player) Move(deltaX, deltaY float64) {
|
||||
p.Velocity.X += deltaX * Acceleration
|
||||
p.Velocity.Y += deltaY * Acceleration
|
||||
}
|
||||
|
||||
func (p *Player) SlideByVelocity() {
|
||||
p.Position.X += p.Velocity.X
|
||||
p.Position.Y += p.Velocity.Y
|
||||
|
||||
@@ -63,3 +68,29 @@ func (p *Player) Draw(pen *sdl.Renderer) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Player) GetPosition() land.Vector2D {
|
||||
return p.Position
|
||||
}
|
||||
|
||||
func (p *Player) GetSize() land.Vector2D {
|
||||
return p.Size
|
||||
}
|
||||
|
||||
func (p *Player) OnCollide(other land.CollidableObject) {
|
||||
if p.Position.X < other.GetPosition().X {
|
||||
p.Position.X = other.GetPosition().X - p.Size.X
|
||||
p.Velocity.X = 0
|
||||
} else if p.Position.X > other.GetPosition().X {
|
||||
p.Position.X = other.GetPosition().X + other.GetSize().X
|
||||
p.Velocity.X = 0
|
||||
}
|
||||
|
||||
if p.Position.Y < other.GetPosition().Y {
|
||||
p.Position.Y = other.GetPosition().Y - p.Size.Y
|
||||
p.Velocity.Y = 0
|
||||
} else if p.Position.Y > other.GetPosition().Y {
|
||||
p.Position.Y = other.GetPosition().Y + other.GetSize().Y
|
||||
p.Velocity.Y = 0
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user