replace complex128 Vec with a struct
This commit is contained in:
parent
d8c9a533fa
commit
b4ca4ea17d
|
@ -51,7 +51,7 @@ func run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
camPos = pixel.V(0, 0)
|
camPos = ZV
|
||||||
camSpeed = 500.0
|
camSpeed = 500.0
|
||||||
camZoom = 1.0
|
camZoom = 1.0
|
||||||
camZoomSpeed = 1.2
|
camZoomSpeed = 1.2
|
||||||
|
|
|
@ -53,7 +53,7 @@ func run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
camPos = pixel.V(0, 0)
|
camPos = pixel.ZV
|
||||||
camSpeed = 500.0
|
camSpeed = 500.0
|
||||||
camZoom = 1.0
|
camZoom = 1.0
|
||||||
camZoomSpeed = 1.2
|
camZoomSpeed = 1.2
|
||||||
|
|
|
@ -44,17 +44,17 @@ func (cl *colorlight) apply(dst pixel.ComposeTarget, center pixel.Vec, src, nois
|
||||||
if cl.imd == nil {
|
if cl.imd == nil {
|
||||||
imd := imdraw.New(nil)
|
imd := imdraw.New(nil)
|
||||||
imd.Color = pixel.Alpha(1)
|
imd.Color = pixel.Alpha(1)
|
||||||
imd.Push(0)
|
imd.Push(pixel.ZV)
|
||||||
imd.Color = pixel.Alpha(0)
|
imd.Color = pixel.Alpha(0)
|
||||||
for angle := -cl.spread / 2; angle <= cl.spread/2; angle += cl.spread / 64 {
|
for angle := -cl.spread / 2; angle <= cl.spread/2; angle += cl.spread / 64 {
|
||||||
imd.Push(pixel.X(1).Rotated(angle))
|
imd.Push(pixel.V(1, 0).Rotated(angle))
|
||||||
}
|
}
|
||||||
imd.Polygon(0)
|
imd.Polygon(0)
|
||||||
cl.imd = imd
|
cl.imd = imd
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw the light arc
|
// draw the light arc
|
||||||
dst.SetMatrix(pixel.IM.Scaled(0, cl.radius).Rotated(0, cl.angle).Moved(cl.point))
|
dst.SetMatrix(pixel.IM.Scaled(pixel.ZV, cl.radius).Rotated(pixel.ZV, cl.angle).Moved(cl.point))
|
||||||
dst.SetColorMask(pixel.Alpha(1))
|
dst.SetColorMask(pixel.Alpha(1))
|
||||||
dst.SetComposeMethod(pixel.ComposePlus)
|
dst.SetComposeMethod(pixel.ComposePlus)
|
||||||
cl.imd.Draw(dst)
|
cl.imd.Draw(dst)
|
||||||
|
@ -70,7 +70,7 @@ func (cl *colorlight) apply(dst pixel.ComposeTarget, center pixel.Vec, src, nois
|
||||||
src.Draw(dst, pixel.IM.Moved(center))
|
src.Draw(dst, pixel.IM.Moved(center))
|
||||||
|
|
||||||
// draw the light reflected from the dust
|
// draw the light reflected from the dust
|
||||||
dst.SetMatrix(pixel.IM.Scaled(0, cl.radius).Rotated(0, cl.angle).Moved(cl.point))
|
dst.SetMatrix(pixel.IM.Scaled(pixel.ZV, cl.radius).Rotated(pixel.ZV, cl.angle).Moved(cl.point))
|
||||||
dst.SetColorMask(cl.color.Mul(pixel.Alpha(cl.dust)))
|
dst.SetColorMask(cl.color.Mul(pixel.Alpha(cl.dust)))
|
||||||
dst.SetComposeMethod(pixel.ComposePlus)
|
dst.SetComposeMethod(pixel.ComposePlus)
|
||||||
cl.imd.Draw(dst)
|
cl.imd.Draw(dst)
|
||||||
|
@ -107,10 +107,10 @@ func run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
points := []pixel.Vec{
|
points := []pixel.Vec{
|
||||||
pixel.V(win.Bounds().Min.X(), win.Bounds().Min.Y()),
|
{X: win.Bounds().Min.X, Y: win.Bounds().Min.Y},
|
||||||
pixel.V(win.Bounds().Max.X(), win.Bounds().Min.Y()),
|
{X: win.Bounds().Max.X, Y: win.Bounds().Min.Y},
|
||||||
pixel.V(win.Bounds().Max.X(), win.Bounds().Max.Y()),
|
{X: win.Bounds().Max.X, Y: win.Bounds().Max.Y},
|
||||||
pixel.V(win.Bounds().Min.X(), win.Bounds().Max.Y()),
|
{X: win.Bounds().Min.X, Y: win.Bounds().Max.Y},
|
||||||
}
|
}
|
||||||
|
|
||||||
angles := []float64{
|
angles := []float64{
|
||||||
|
|
|
@ -42,7 +42,7 @@ func loadAnimationSheet(sheetPath, descPath string, frameWidth float64) (sheet p
|
||||||
|
|
||||||
// create a slice of frames inside the spritesheet
|
// create a slice of frames inside the spritesheet
|
||||||
var frames []pixel.Rect
|
var frames []pixel.Rect
|
||||||
for x := 0.0; x+frameWidth <= sheet.Bounds().Max.X(); x += frameWidth {
|
for x := 0.0; x+frameWidth <= sheet.Bounds().Max.X; x += frameWidth {
|
||||||
frames = append(frames, pixel.R(
|
frames = append(frames, pixel.R(
|
||||||
x,
|
x,
|
||||||
0,
|
0,
|
||||||
|
@ -104,37 +104,37 @@ type gopherPhys struct {
|
||||||
func (gp *gopherPhys) update(dt float64, ctrl pixel.Vec, platforms []platform) {
|
func (gp *gopherPhys) update(dt float64, ctrl pixel.Vec, platforms []platform) {
|
||||||
// apply controls
|
// apply controls
|
||||||
switch {
|
switch {
|
||||||
case ctrl.X() < 0:
|
case ctrl.X < 0:
|
||||||
gp.vel = gp.vel.WithX(-gp.runSpeed)
|
gp.vel.X = -gp.runSpeed
|
||||||
case ctrl.X() > 0:
|
case ctrl.X > 0:
|
||||||
gp.vel = gp.vel.WithX(+gp.runSpeed)
|
gp.vel.X = +gp.runSpeed
|
||||||
default:
|
default:
|
||||||
gp.vel = gp.vel.WithX(0)
|
gp.vel.X = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply gravity and velocity
|
// apply gravity and velocity
|
||||||
gp.vel += pixel.Y(gp.gravity).Scaled(dt)
|
gp.vel.Y += gp.gravity * dt
|
||||||
gp.rect = gp.rect.Moved(gp.vel.Scaled(dt))
|
gp.rect = gp.rect.Moved(gp.vel.Scaled(dt))
|
||||||
|
|
||||||
// check collisions against each platform
|
// check collisions against each platform
|
||||||
gp.ground = false
|
gp.ground = false
|
||||||
if gp.vel.Y() <= 0 {
|
if gp.vel.Y <= 0 {
|
||||||
for _, p := range platforms {
|
for _, p := range platforms {
|
||||||
if gp.rect.Max.X() <= p.rect.Min.X() || gp.rect.Min.X() >= p.rect.Max.X() {
|
if gp.rect.Max.X <= p.rect.Min.X || gp.rect.Min.X >= p.rect.Max.X {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if gp.rect.Min.Y() > p.rect.Max.Y() || gp.rect.Min.Y() < p.rect.Max.Y()+gp.vel.Y()*dt {
|
if gp.rect.Min.Y > p.rect.Max.Y || gp.rect.Min.Y < p.rect.Max.Y+gp.vel.Y*dt {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
gp.vel = gp.vel.WithY(0)
|
gp.vel.Y = 0
|
||||||
gp.rect = gp.rect.Moved(pixel.Y(p.rect.Max.Y() - gp.rect.Min.Y()))
|
gp.rect = gp.rect.Moved(pixel.V(0, p.rect.Max.Y-gp.rect.Min.Y))
|
||||||
gp.ground = true
|
gp.ground = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// jump if on the ground and the player wants to jump
|
// jump if on the ground and the player wants to jump
|
||||||
if gp.ground && ctrl.Y() > 0 {
|
if gp.ground && ctrl.Y > 0 {
|
||||||
gp.vel = gp.vel.WithY(gp.jumpSpeed)
|
gp.vel.Y = gp.jumpSpeed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,7 +188,7 @@ func (ga *gopherAnim) update(dt float64, phys *gopherPhys) {
|
||||||
i := int(math.Floor(ga.counter / ga.rate))
|
i := int(math.Floor(ga.counter / ga.rate))
|
||||||
ga.frame = ga.anims["Run"][i%len(ga.anims["Run"])]
|
ga.frame = ga.anims["Run"][i%len(ga.anims["Run"])]
|
||||||
case jumping:
|
case jumping:
|
||||||
speed := phys.vel.Y()
|
speed := phys.vel.Y
|
||||||
i := int((-speed/phys.jumpSpeed + 1) / 2 * float64(len(ga.anims["Jump"])))
|
i := int((-speed/phys.jumpSpeed + 1) / 2 * float64(len(ga.anims["Jump"])))
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
i = 0
|
i = 0
|
||||||
|
@ -200,8 +200,8 @@ func (ga *gopherAnim) update(dt float64, phys *gopherPhys) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the facing direction of the gopher
|
// set the facing direction of the gopher
|
||||||
if phys.vel.X() != 0 {
|
if phys.vel.X != 0 {
|
||||||
if phys.vel.X() > 0 {
|
if phys.vel.X > 0 {
|
||||||
ga.dir = +1
|
ga.dir = +1
|
||||||
} else {
|
} else {
|
||||||
ga.dir = -1
|
ga.dir = -1
|
||||||
|
@ -216,11 +216,11 @@ func (ga *gopherAnim) draw(t pixel.Target, phys *gopherPhys) {
|
||||||
// draw the correct frame with the correct position and direction
|
// draw the correct frame with the correct position and direction
|
||||||
ga.sprite.Set(ga.sheet, ga.frame)
|
ga.sprite.Set(ga.sheet, ga.frame)
|
||||||
ga.sprite.Draw(t, pixel.IM.
|
ga.sprite.Draw(t, pixel.IM.
|
||||||
ScaledXY(0, pixel.V(
|
ScaledXY(pixel.ZV, pixel.V(
|
||||||
phys.rect.W()/ga.sprite.Frame().W(),
|
phys.rect.W()/ga.sprite.Frame().W(),
|
||||||
phys.rect.H()/ga.sprite.Frame().H(),
|
phys.rect.H()/ga.sprite.Frame().H(),
|
||||||
)).
|
)).
|
||||||
ScaledXY(0, pixel.V(-ga.dir, 1)).
|
ScaledXY(pixel.ZV, pixel.V(-ga.dir, 1)).
|
||||||
Moved(phys.rect.Center()),
|
Moved(phys.rect.Center()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -326,7 +326,7 @@ func run() {
|
||||||
imd := imdraw.New(sheet)
|
imd := imdraw.New(sheet)
|
||||||
imd.Precision = 32
|
imd.Precision = 32
|
||||||
|
|
||||||
camPos := pixel.V(0, 0)
|
camPos := pixel.ZV
|
||||||
|
|
||||||
last := time.Now()
|
last := time.Now()
|
||||||
for !win.Closed() {
|
for !win.Closed() {
|
||||||
|
@ -335,7 +335,7 @@ func run() {
|
||||||
|
|
||||||
// lerp the camera position towards the gopher
|
// lerp the camera position towards the gopher
|
||||||
camPos = pixel.Lerp(camPos, phys.rect.Center(), 1-math.Pow(1.0/128, dt))
|
camPos = pixel.Lerp(camPos, phys.rect.Center(), 1-math.Pow(1.0/128, dt))
|
||||||
cam := pixel.IM.Moved(-camPos)
|
cam := pixel.IM.Moved(camPos.Scaled(-1))
|
||||||
canvas.SetMatrix(cam)
|
canvas.SetMatrix(cam)
|
||||||
|
|
||||||
// slow motion with tab
|
// slow motion with tab
|
||||||
|
@ -345,20 +345,20 @@ func run() {
|
||||||
|
|
||||||
// restart the level on pressing enter
|
// restart the level on pressing enter
|
||||||
if win.JustPressed(pixelgl.KeyEnter) {
|
if win.JustPressed(pixelgl.KeyEnter) {
|
||||||
phys.rect = phys.rect.Moved(-phys.rect.Center())
|
phys.rect = phys.rect.Moved(phys.rect.Center().Scaled(-1))
|
||||||
phys.vel = 0
|
phys.vel = pixel.ZV
|
||||||
}
|
}
|
||||||
|
|
||||||
// control the gopher with keys
|
// control the gopher with keys
|
||||||
ctrl := pixel.V(0, 0)
|
ctrl := pixel.ZV
|
||||||
if win.Pressed(pixelgl.KeyLeft) {
|
if win.Pressed(pixelgl.KeyLeft) {
|
||||||
ctrl -= pixel.X(1)
|
ctrl.X--
|
||||||
}
|
}
|
||||||
if win.Pressed(pixelgl.KeyRight) {
|
if win.Pressed(pixelgl.KeyRight) {
|
||||||
ctrl += pixel.X(1)
|
ctrl.X++
|
||||||
}
|
}
|
||||||
if win.JustPressed(pixelgl.KeyUp) {
|
if win.JustPressed(pixelgl.KeyUp) {
|
||||||
ctrl = ctrl.WithY(1)
|
ctrl.Y = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// update the physics and animation
|
// update the physics and animation
|
||||||
|
@ -378,7 +378,7 @@ func run() {
|
||||||
|
|
||||||
// stretch the canvas to the window
|
// stretch the canvas to the window
|
||||||
win.Clear(colornames.White)
|
win.Clear(colornames.White)
|
||||||
win.SetMatrix(pixel.IM.Scaled(0,
|
win.SetMatrix(pixel.IM.Scaled(pixel.ZV,
|
||||||
math.Min(
|
math.Min(
|
||||||
win.Bounds().W()/canvas.Bounds().W(),
|
win.Bounds().W()/canvas.Bounds().W(),
|
||||||
win.Bounds().H()/canvas.Bounds().H(),
|
win.Bounds().H()/canvas.Bounds().H(),
|
||||||
|
|
|
@ -57,8 +57,8 @@ func (p *particles) DrawAll(t pixel.Target) {
|
||||||
part.Sprite.DrawColorMask(
|
part.Sprite.DrawColorMask(
|
||||||
t,
|
t,
|
||||||
pixel.IM.
|
pixel.IM.
|
||||||
Scaled(0, part.Scale).
|
Scaled(pixel.ZV, part.Scale).
|
||||||
Rotated(0, part.Rot).
|
Rotated(pixel.ZV, part.Rot).
|
||||||
Moved(part.Pos),
|
Moved(part.Pos),
|
||||||
part.Mask,
|
part.Mask,
|
||||||
)
|
)
|
||||||
|
@ -86,7 +86,7 @@ func (ss *smokeSystem) Generate() *particle {
|
||||||
sd := new(smokeData)
|
sd := new(smokeData)
|
||||||
for _, base := range ss.VelBasis {
|
for _, base := range ss.VelBasis {
|
||||||
c := math.Max(0, 1+rand.NormFloat64()*ss.VelDist)
|
c := math.Max(0, 1+rand.NormFloat64()*ss.VelDist)
|
||||||
sd.Vel += base.Scaled(c)
|
sd.Vel = sd.Vel.Add(base.Scaled(c))
|
||||||
}
|
}
|
||||||
sd.Vel = sd.Vel.Scaled(1 / float64(len(ss.VelBasis)))
|
sd.Vel = sd.Vel.Scaled(1 / float64(len(ss.VelBasis)))
|
||||||
sd.Life = math.Max(0, ss.LifeAvg+rand.NormFloat64()*ss.LifeDist)
|
sd.Life = math.Max(0, ss.LifeAvg+rand.NormFloat64()*ss.LifeDist)
|
||||||
|
@ -108,7 +108,7 @@ func (ss *smokeSystem) Update(dt float64, p *particle) bool {
|
||||||
|
|
||||||
frac := sd.Time / sd.Life
|
frac := sd.Time / sd.Life
|
||||||
|
|
||||||
p.Pos += sd.Vel.Scaled(dt)
|
p.Pos = p.Pos.Add(sd.Vel.Scaled(dt))
|
||||||
p.Scale = 0.5 + frac*1.5
|
p.Scale = 0.5 + frac*1.5
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -188,7 +188,7 @@ func run() {
|
||||||
|
|
||||||
ss := &smokeSystem{
|
ss := &smokeSystem{
|
||||||
Rects: rects,
|
Rects: rects,
|
||||||
Orig: 0,
|
Orig: pixel.ZV,
|
||||||
VelBasis: []pixel.Vec{pixel.V(-100, 100), pixel.V(100, 100), pixel.V(0, 100)},
|
VelBasis: []pixel.Vec{pixel.V(-100, 100), pixel.V(100, 100), pixel.V(0, 100)},
|
||||||
VelDist: 0.1,
|
VelDist: 0.1,
|
||||||
LifeAvg: 7,
|
LifeAvg: 7,
|
||||||
|
@ -212,7 +212,10 @@ func run() {
|
||||||
p.UpdateAll(dt)
|
p.UpdateAll(dt)
|
||||||
|
|
||||||
win.Clear(colornames.Aliceblue)
|
win.Clear(colornames.Aliceblue)
|
||||||
win.SetMatrix(pixel.IM.Moved(win.Bounds().Center() - pixel.Y(win.Bounds().H()/2)))
|
|
||||||
|
orig := win.Bounds().Center()
|
||||||
|
orig.Y -= win.Bounds().H() / 2
|
||||||
|
win.SetMatrix(pixel.IM.Moved(orig))
|
||||||
|
|
||||||
batch.Clear()
|
batch.Clear()
|
||||||
p.DrawAll(batch)
|
p.DrawAll(batch)
|
||||||
|
|
10
xor/main.go
10
xor/main.go
|
@ -40,28 +40,28 @@ func run() {
|
||||||
// red circle
|
// red circle
|
||||||
imd.Clear()
|
imd.Clear()
|
||||||
imd.Color = pixel.RGB(1, 0, 0)
|
imd.Color = pixel.RGB(1, 0, 0)
|
||||||
imd.Push(win.Bounds().Center() - pixel.X(offset))
|
imd.Push(win.Bounds().Center().Add(pixel.V(-offset, 0)))
|
||||||
imd.Circle(200, 0)
|
imd.Circle(200, 0)
|
||||||
imd.Draw(canvas)
|
imd.Draw(canvas)
|
||||||
|
|
||||||
// blue circle
|
// blue circle
|
||||||
imd.Clear()
|
imd.Clear()
|
||||||
imd.Color = pixel.RGB(0, 0, 1)
|
imd.Color = pixel.RGB(0, 0, 1)
|
||||||
imd.Push(win.Bounds().Center() + pixel.X(offset))
|
imd.Push(win.Bounds().Center().Add(pixel.V(offset, 0)))
|
||||||
imd.Circle(150, 0)
|
imd.Circle(150, 0)
|
||||||
imd.Draw(canvas)
|
imd.Draw(canvas)
|
||||||
|
|
||||||
// yellow circle
|
// yellow circle
|
||||||
imd.Clear()
|
imd.Clear()
|
||||||
imd.Color = pixel.RGB(1, 1, 0)
|
imd.Color = pixel.RGB(1, 1, 0)
|
||||||
imd.Push(win.Bounds().Center() - pixel.Y(offset))
|
imd.Push(win.Bounds().Center().Add(pixel.V(0, -offset)))
|
||||||
imd.Circle(100, 0)
|
imd.Circle(100, 0)
|
||||||
imd.Draw(canvas)
|
imd.Draw(canvas)
|
||||||
|
|
||||||
// magenta circle
|
// magenta circle
|
||||||
imd.Clear()
|
imd.Clear()
|
||||||
imd.Color=pixel.RGB(1, 0, 1)
|
imd.Color = pixel.RGB(1, 0, 1)
|
||||||
imd.Push(win.Bounds().Center() + pixel.Y(offset))
|
imd.Push(win.Bounds().Center().Add(pixel.V(0, offset)))
|
||||||
imd.Circle(50, 0)
|
imd.Circle(50, 0)
|
||||||
imd.Draw(canvas)
|
imd.Draw(canvas)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue