Improved player movement on ropes

This commit is contained in:
nefrace 2023-01-05 02:33:51 +03:00
parent b360d93333
commit 6119e288f8
3 changed files with 48 additions and 26 deletions

26
main.go
View File

@ -5,26 +5,18 @@ import (
"math/rand"
)
var smiley = [8]byte{
0b11000011,
0b10000001,
0b00100100,
0b00100100,
0b00000000,
0b00100100,
0b10011001,
0b11000011,
}
var gravity = 0.2
var points []*Point = []*Point{}
var sticks []*Stick = []*Stick{}
var player *Player
var frame uint64 = 0
var lightIndex uint64 = 0
var camX = 0
var camY = 0
//go:export start
func start() {
rand.Seed(654654321348654)
points = []*Point{}
sticks = []*Stick{}
@ -34,13 +26,23 @@ func start() {
w4.PALETTE[3] = 0xff4d6d
for i := 0; i < 4; i++ {
p, s := CreateRope(
Vector{0, rand.Float64()*40 + float64(i*40)},
Vector{160, rand.Float64()*40 + float64(i*40)},
Vector{0, rand.Float64()*40 + float64(i*40)},
10,
)
points = append(points, p...)
sticks = append(sticks, s...)
}
for i := 0; i < 3; i++ {
p, s := CreateRope(
Vector{float64(i*30 + 30), 0},
Vector{float64(i*30 + 40), 100},
10,
)
p[len(p)-1].IsLocked = false
points = append(points, p...)
sticks = append(sticks, s...)
}
player = &Player{
Position: Vector{80, 80},
Speed: Vector{},

View File

@ -3,6 +3,7 @@ package main
import (
"cart/w4"
"math"
"strconv"
)
type Player struct {
@ -13,30 +14,34 @@ type Player struct {
StickOffset float64
GrabTimeout uint
Gamepad *uint8
GamepadLast uint8
}
func (p *Player) Update() {
p.Speed.X = 0
lastGamepad := *p.Gamepad & (*p.Gamepad ^ p.GamepadLast)
p.GamepadLast = *p.Gamepad
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.Speed.X = 0
// p.Position = p.PointGrabbed.Position
if *p.Gamepad&w4.BUTTON_LEFT != 0 {
p.StickOffset -= 0.1
p.Speed.X -= 1
}
if *p.Gamepad&w4.BUTTON_RIGHT != 0 {
p.StickOffset += 0.1
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
}
p.MoveOnRope(p.Speed)
if p.StickOffset < 0 {
point := p.StickGrabbed.PointA
if len(point.Sticks) == 1 {
@ -56,11 +61,14 @@ func (p *Player) Update() {
}
}
p.Position = p.StickGrabbed.GetPosition(p.StickOffset)
if *p.Gamepad&w4.BUTTON_2 != 0 {
if lastGamepad&w4.BUTTON_2 != 0 {
p.GrabTimeout = 10
if isJumping {
p.GrabTimeout = 5
p.Speed.Y = -4.5
p.GrabTimeout = 10
p.Speed = p.Speed.MulScalar(2)
if p.Speed.Y <= 0 {
p.Speed.Y -= 1 * 2
}
impulse := p.Speed.MulScalar(-1)
p.StickGrabbed.PointA.Position.MoveVec(impulse)
p.StickGrabbed.PointB.Position.MoveVec(impulse)
@ -90,6 +98,7 @@ func (p *Player) Update() {
}
}
if stickDistance < 4 {
w4.Trace(strconv.FormatFloat(stickDistance, 'f', 3, 64))
p.StickGrabbed = selectedStick
p.StickOffset = selectedStick.GetOffset(p.Position)
p.StickGrabbed.PointA.Position.MoveVec(p.Speed)
@ -99,6 +108,12 @@ func (p *Player) Update() {
}
}
func (p *Player) MoveOnRope(motion Vector) {
newPos := p.Position.Sum(motion)
offset := p.StickGrabbed.GetOffset(newPos)
p.StickOffset = offset
}
func (p *Player) Draw() {
*w4.DRAW_COLORS = 0x34
w4.Rect(int(p.Position.X)-4, int(p.Position.Y)-4, 8, 8)

View File

@ -58,7 +58,7 @@ func (s *Stick) GetDistance(point Vector) float64 {
return ap.Len()
}
mod := ab.Len()
return math.Abs(ab.X*ap.Y-ab.Y*ab.X) / mod
return math.Abs(ab.X*ap.Y-ab.Y*ap.X) / mod
}
func (s *Stick) GetPosition(offset float64) Vector {
@ -70,7 +70,12 @@ func (s *Stick) GetOffset(p Vector) float64 {
ab := s.GetVector()
ap := p.Sub(s.PointA.Position)
projection := ap.ProjectTo(ab)
return projection.Len() / ab.Len()
dot := ab.Dot(ap)
result := projection.Len() / ab.Len()
if dot < 0 {
result *= -1
}
return result
}
func (s *Stick) Draw() {