You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.9 KiB
Go
100 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"log"
|
|
"math"
|
|
"os"
|
|
|
|
"github.com/disintegration/imaging"
|
|
)
|
|
|
|
type Mario struct {
|
|
position image.Point
|
|
dir image.Point
|
|
images map[string]image.Image
|
|
updown string
|
|
}
|
|
|
|
func loadMario(file string) image.Image {
|
|
|
|
reader, err := os.Open(file)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
rawMario, _, err := image.Decode(reader)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
mario := imaging.Resize(rawMario, 16, 16, imaging.Lanczos)
|
|
return mario
|
|
}
|
|
|
|
func initialMap() map[string]image.Image {
|
|
imageMap := make(map[string]image.Image)
|
|
imageMap["marioUp"] = loadMario("marioUp.png")
|
|
imageMap["marioDown"] = loadMario("marioDown.png")
|
|
return imageMap
|
|
}
|
|
|
|
// initializes the struct for the an play animation function, this could all be dumped into function that's wrapping go routine if I wanted
|
|
|
|
// this assumes mario context is up
|
|
func (a *Animation) animateMario() {
|
|
defer a.updateMarioPosition()
|
|
a.ctx.SetColor(color.Black)
|
|
a.ctx.Clear()
|
|
if a.mario.dir.X == 1 {
|
|
a.ctx.DrawImageAnchored(a.mario.images[a.mario.updown], a.mario.position.X, a.mario.position.Y, 0.5, 0.5)
|
|
} else {
|
|
a.ctx.DrawImageAnchored(imaging.FlipH(a.mario.images[a.mario.updown]), a.mario.position.X, a.mario.position.Y, 0.5, 0.5)
|
|
}
|
|
}
|
|
|
|
// what mario does every frame
|
|
func (a *Animation) updateMarioPosition() {
|
|
/* a.mario.position.X += 1 * a.mario.dir.X
|
|
a.mario.position.Y += 1 * a.mario.dir.Y
|
|
|
|
if a.mario.position.Y+a.height > a.ctx.Height() {
|
|
a.mario.dir.Y = -1
|
|
a.mario.updown = "marioUp"
|
|
} else if a.mario.position.Y-a.height < 0 {
|
|
a.mario.updown = "marioDown"
|
|
a.mario.dir.Y = 1
|
|
}
|
|
|
|
if a.mario.position.X+a.width > a.ctx.Width() {
|
|
a.mario.dir.X = -1
|
|
} else if a.mario.position.X-a.width < 0 {
|
|
a.mario.dir.X = 1
|
|
}*/
|
|
const (
|
|
wavelength = 20 // Wavelength of the sine wave
|
|
amplitude = 4 // Amplitude of the sine wave
|
|
)
|
|
|
|
currentTime := float64(a.mario.position.X) / wavelength // Calculate current time based on Mario's X position
|
|
|
|
a.mario.position.X += int(amplitude*math.Sin(2*math.Pi*currentTime/wavelength)) + a.mario.dir.X
|
|
a.mario.position.Y += a.mario.dir.Y
|
|
|
|
// Ensure Mario stays within the display boundaries and reverses direction when necessary.
|
|
if a.mario.position.Y+a.height > a.ctx.Height() {
|
|
a.mario.position.Y = a.ctx.Height() - a.height // Reset Y position to the top boundary
|
|
a.mario.updown = "marioUp"
|
|
a.mario.dir.Y = -1 // Reverse direction when hitting bottom
|
|
} else if a.mario.position.Y < 0 {
|
|
a.mario.position.Y = 0 // Reset Y position to the bottom boundary
|
|
a.mario.updown = "marioDown"
|
|
a.mario.dir.Y = 1 // Reverse direction when hitting top
|
|
}
|
|
|
|
if a.mario.position.X+a.width > a.ctx.Width() {
|
|
a.mario.position.X -= (a.mario.position.X + a.width - a.ctx.Width()) // Wrap around to the left side
|
|
} else if a.mario.position.X < 0 {
|
|
a.mario.position.X = -(a.mario.position.X) // Wrap around to the right side
|
|
}
|
|
}
|