package main import ( "cart/w4" "math" ) type Player struct { Position Vector Speed Vector //PointGrabbed *Point StickGrabbed *Stick StickOffset float64 GrabTimeout uint Gamepad *uint8 } func (p *Player) Update() { p.Speed.X = 0 if p.GrabTimeout > 0 { p.GrabTimeout-- } if *p.Gamepad&w4.BUTTON_LEFT != 0 { p.Speed.X -= 1 } if *p.Gamepad&w4.BUTTON_RIGHT != 0 { p.Speed.X += 1 } isJumping := *p.Gamepad&w4.BUTTON_DOWN == 0 p.Speed.Y = math.Min(4, p.Speed.Y+gravity) if p.StickGrabbed != nil { p.Speed.Y = 0 // p.Position = p.PointGrabbed.Position if *p.Gamepad&w4.BUTTON_LEFT != 0 { p.StickOffset -= 0.1 } if *p.Gamepad&w4.BUTTON_RIGHT != 0 { p.StickOffset += 0.1 } 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) if *p.Gamepad&w4.BUTTON_2 != 0 { p.GrabTimeout = 10 if isJumping { p.GrabTimeout = 5 p.Speed.Y = -4.5 impulse := p.Speed.MulScalar(-1) p.StickGrabbed.PointA.Position.MoveVec(impulse) p.StickGrabbed.PointB.Position.MoveVec(impulse) } p.StickGrabbed = nil } } else { p.Position.Move(p.Speed.X, p.Speed.Y) if *p.Gamepad&w4.BUTTON_DOWN == 0 && p.GrabTimeout == 0 { distance := math.MaxFloat64 var selectedPoint *Point for _, point := range points { diff := p.Position.Sub(point.Position) 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 } } if stickDistance < 4 { p.StickGrabbed = selectedStick p.StickOffset = selectedStick.GetOffset(p.Position) p.StickGrabbed.PointA.Position.MoveVec(p.Speed) p.StickGrabbed.PointB.Position.MoveVec(p.Speed) } } } } func (p *Player) Draw() { *w4.DRAW_COLORS = 0x34 w4.Rect(int(p.Position.X)-4, int(p.Position.Y)-4, 8, 8) }