newyearkrendelki/player.go

122 lines
2.8 KiB
Go
Raw Normal View History

2023-01-02 07:58:11 +03:00
package main
2023-01-04 00:00:32 +03:00
import (
"cart/w4"
"math"
2023-01-05 02:33:51 +03:00
"strconv"
2023-01-04 00:00:32 +03:00
)
2023-01-02 07:58:11 +03:00
type Player struct {
2023-01-04 00:00:32 +03:00
Position Vector
Speed Vector
//PointGrabbed *Point
StickGrabbed *Stick
StickOffset float64
2023-01-02 07:58:11 +03:00
GrabTimeout uint
Gamepad *uint8
2023-01-05 02:33:51 +03:00
GamepadLast uint8
2023-01-02 07:58:11 +03:00
}
func (p *Player) Update() {
2023-01-05 02:33:51 +03:00
lastGamepad := *p.Gamepad & (*p.Gamepad ^ p.GamepadLast)
p.GamepadLast = *p.Gamepad
2023-01-02 07:58:11 +03:00
if p.GrabTimeout > 0 {
p.GrabTimeout--
}
isJumping := *p.Gamepad&w4.BUTTON_DOWN == 0
2023-01-04 00:00:32 +03:00
p.Speed.Y = math.Min(4, p.Speed.Y+gravity)
if p.StickGrabbed != nil {
2023-01-02 07:58:11 +03:00
p.Speed.Y = 0
2023-01-05 02:33:51 +03:00
p.Speed.X = 0
2023-01-04 00:00:32 +03:00
// p.Position = p.PointGrabbed.Position
if *p.Gamepad&w4.BUTTON_LEFT != 0 {
2023-01-05 02:33:51 +03:00
p.Speed.X -= 1
2023-01-04 00:00:32 +03:00
}
if *p.Gamepad&w4.BUTTON_RIGHT != 0 {
2023-01-05 02:33:51 +03:00
p.Speed.X += 1
}
if *p.Gamepad&w4.BUTTON_UP != 0 {
p.Speed.Y -= 1
}
if *p.Gamepad&w4.BUTTON_DOWN != 0 {
p.Speed.Y += 1
2023-01-04 00:00:32 +03:00
}
2023-01-05 02:33:51 +03:00
p.MoveOnRope(p.Speed)
2023-01-04 00:00:32 +03:00
if p.StickOffset < 0 {
point := p.StickGrabbed.PointA
if len(point.Sticks) == 1 {
p.StickOffset = 0
} else {
p.StickGrabbed = point.Sticks[0]
p.StickOffset += 1
}
}
if p.StickOffset > 1 {
point := p.StickGrabbed.PointB
if len(point.Sticks) == 1 {
p.StickOffset = 1
} else {
p.StickGrabbed = point.Sticks[1]
p.StickOffset -= 1
}
}
p.Position = p.StickGrabbed.GetPosition(p.StickOffset)
2023-01-05 02:33:51 +03:00
if lastGamepad&w4.BUTTON_2 != 0 {
2023-01-02 07:58:11 +03:00
p.GrabTimeout = 10
if isJumping {
2023-01-05 02:33:51 +03:00
p.GrabTimeout = 10
p.Speed = p.Speed.MulScalar(2)
if p.Speed.Y <= 0 {
p.Speed.Y -= 1 * 2
}
2023-01-04 00:00:32 +03:00
impulse := p.Speed.MulScalar(-1)
p.StickGrabbed.PointA.Position.MoveVec(impulse)
p.StickGrabbed.PointB.Position.MoveVec(impulse)
2023-01-02 07:58:11 +03:00
}
2023-01-04 00:00:32 +03:00
p.StickGrabbed = nil
2023-01-02 07:58:11 +03:00
}
} else {
p.Position.Move(p.Speed.X, p.Speed.Y)
if *p.Gamepad&w4.BUTTON_DOWN == 0 && p.GrabTimeout == 0 {
2023-01-04 00:00:32 +03:00
distance := math.MaxFloat64
var selectedPoint *Point
2023-01-02 07:58:11 +03:00
for _, point := range points {
diff := p.Position.Sub(point.Position)
2023-01-04 00:00:32 +03:00
dSquared := diff.LenSquared()
if dSquared < distance {
distance = dSquared
selectedPoint = point
}
}
stickDistance := math.MaxFloat64
var selectedStick *Stick
for _, stick := range selectedPoint.Sticks {
distance := stick.GetDistance(p.Position)
if distance < stickDistance {
stickDistance = distance
selectedStick = stick
2023-01-02 07:58:11 +03:00
}
}
2023-01-04 00:00:32 +03:00
if stickDistance < 4 {
2023-01-05 02:33:51 +03:00
w4.Trace(strconv.FormatFloat(stickDistance, 'f', 3, 64))
2023-01-04 00:00:32 +03:00
p.StickGrabbed = selectedStick
p.StickOffset = selectedStick.GetOffset(p.Position)
p.StickGrabbed.PointA.Position.MoveVec(p.Speed)
p.StickGrabbed.PointB.Position.MoveVec(p.Speed)
}
2023-01-02 07:58:11 +03:00
}
}
p.Position.X = math.Min(math.Max(0, p.Position.X), 320)
2023-01-02 07:58:11 +03:00
}
2023-01-05 02:33:51 +03:00
func (p *Player) MoveOnRope(motion Vector) {
newPos := p.Position.Sum(motion)
offset := p.StickGrabbed.GetOffset(newPos)
p.StickOffset = offset
}
2023-01-02 07:58:11 +03:00
func (p *Player) Draw() {
*w4.DRAW_COLORS = 0x34
w4.Rect(int(p.Position.X)-4-camX, int(p.Position.Y)-4-camY, 8, 8)
2023-01-02 07:58:11 +03:00
}