summaryrefslogtreecommitdiff
path: root/objects.go
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2024-04-22 18:36:22 +0100
committerdavidovski <david@davidovski.xyz>2024-04-22 18:36:22 +0100
commit0c46c616a126569f778b04ea2c7bf04e34664939 (patch)
tree1a2305eb0c413ac58c1ada4856fe0ec616e126e4 /objects.go
parente4c0edeacb2c5829f864e116b68f6cad175141b3 (diff)
add spring animation
Diffstat (limited to 'objects.go')
-rw-r--r--objects.go76
1 files changed, 57 insertions, 19 deletions
diff --git a/objects.go b/objects.go
index 88aa8ea..b26d1c8 100644
--- a/objects.go
+++ b/objects.go
@@ -9,7 +9,7 @@ import (
)
const (
- SPRING_FORCE = 10
+ SPRING_FORCE = 8
)
type Direction int
@@ -34,14 +34,17 @@ type GameObject struct {
friction float32
resistance float32
- image *ebiten.Image
+ images []*ebiten.Image
onGround bool
onCollideUp func(this, other *GameObject) bool
onCollideDown func(this, other *GameObject) bool
onCollideLeft func(this, other *GameObject) bool
onCollideRight func(this, other *GameObject) bool
Draw func (o * GameObject, screen *ebiten.Image, tilemap Tilemap)
+ UpdateFunc func (o * GameObject, tilemap Tilemap, others []*GameObject)
movable bool
+ state int
+ delta int
}
@@ -49,7 +52,22 @@ type Player struct {
GameObject
}
+func UpdateSpring(o * GameObject, tilemap Tilemap, others []*GameObject) {
+ if o.delta % 3 == 0 {
+ if o.state == 1 {
+ o.state = 0
+ } else if o.state == 2 {
+ o.state = 1
+ }
+ }
+}
+
func (o * GameObject) Update(tilemap Tilemap, others []*GameObject) {
+ if o.UpdateFunc != nil {
+ o.UpdateFunc(o, tilemap, others)
+ }
+ o.delta += 1
+
var direction Direction
o.vy += gravity
@@ -57,6 +75,7 @@ func (o * GameObject) Update(tilemap Tilemap, others []*GameObject) {
o.vy *= o.resistance
o.x += o.vx
+
if o.vx > 0 {
direction = RIGHT
@@ -98,8 +117,8 @@ func GetObjectAt(objects []*GameObject, x, y float32) *GameObject {
}
func (object * GameObject) CollidePoint(x, y float32) bool {
- maxX := object.x + float32(object.image.Bounds().Dx())
- maxY := object.y + float32(object.image.Bounds().Dy())
+ maxX := object.x + float32(object.images[0].Bounds().Dx())
+ maxY := object.y + float32(object.images[0].Bounds().Dy())
minX := object.x
minY := object.y
return x >= minX && x < maxX && y >= minY && y < maxY
@@ -149,21 +168,22 @@ func ShadowDraw(screen *ebiten.Image, image *ebiten.Image, x, y float32, alpha f
}
func DrawObject(o * GameObject, screen *ebiten.Image, tilemap Tilemap) {
- ShadowDraw(screen, o.image, o.x, o.y, o.alpha)
+ image := o.images[o.state]
+ ShadowDraw(screen, image, o.x, o.y, o.alpha)
if o.highlight {
- vector.StrokeRect(screen, o.x, o.y, float32(o.image.Bounds().Dx()), float32(o.image.Bounds().Dy()), hightlightBorder, color.RGBA{255, 100, 100, 255}, false)
+ vector.StrokeRect(screen, o.x, o.y, float32(image.Bounds().Dx()), float32(image.Bounds().Dy()), hightlightBorder, color.RGBA{255, 100, 100, 255}, false)
}
}
func (object * GameObject) Collide(other *GameObject) bool {
- maxX1 := object.x + float32(object.image.Bounds().Dx())
- maxY1 := object.y + float32(object.image.Bounds().Dy())
+ maxX1 := object.x + float32(object.images[0].Bounds().Dx())
+ maxY1 := object.y + float32(object.images[0].Bounds().Dy())
minX1 := object.x
minY1 := object.y
- maxX2 := other.x + float32(other.image.Bounds().Dx())
- maxY2 := other.y + float32(other.image.Bounds().Dy())
+ maxX2 := other.x + float32(other.images[0].Bounds().Dx())
+ maxY2 := other.y + float32(other.images[0].Bounds().Dy())
minX2 := other.x
minY2 := other.y
@@ -189,8 +209,8 @@ func (object * GameObject) MoveRight() {
func (object * GameObject) ToRect() image.Rectangle {
- width := object.image.Bounds().Dx()
- height := object.image.Bounds().Dy()
+ width := object.images[0].Bounds().Dx()
+ height := object.images[0].Bounds().Dy()
x := int(object.x)
y := int(object.y)
return image.Rect(x, y, x+width, y+height)
@@ -221,7 +241,9 @@ func NewPlayer(game *Game, x, y float32) *GameObject{
playerImage := ebiten.NewImageFromImage(characterImage)
- player.image = playerImage.SubImage(image.Rect(4, 8, 27, 32)).(*ebiten.Image)
+ player.images = []*ebiten.Image{
+ playerImage.SubImage(image.Rect(4, 8, 27, 32)).(*ebiten.Image),
+ }
player.Draw = DrawObject
player.movable = false
@@ -231,7 +253,9 @@ func NewPlayer(game *Game, x, y float32) *GameObject{
func NewExit(game *Game, x, y float32) *GameObject{
exit := NewObject(game, x, y)
- exit.image = tilesImage.SubImage(image.Rect(0, 16, 32, 48)).(*ebiten.Image)
+ exit.images = []*ebiten.Image{
+ tilesImage.SubImage(image.Rect(0, 16, 32, 48)).(*ebiten.Image),
+ }
exit.onCollideUp = OnCollideExit
exit.onCollideDown = OnCollideExit
exit.onCollideLeft = OnCollideExit
@@ -244,7 +268,9 @@ func NewExit(game *Game, x, y float32) *GameObject{
func NewBox(game *Game, x, y float32) *GameObject{
box := NewObject(game, x, y)
- box.image = tilesImage.SubImage(image.Rect(160, 0, 176, 16)).(*ebiten.Image)
+ box.images = []*ebiten.Image{
+ tilesImage.SubImage(image.Rect(160, 0, 176, 16)).(*ebiten.Image),
+ }
return box
}
@@ -252,8 +278,13 @@ func NewBox(game *Game, x, y float32) *GameObject{
func NewSpring(game *Game, x, y float32) *GameObject{
spring := NewObject(game, x, y)
- spring.image = tilesImage.SubImage(image.Rect(176, 0, 192, 16)).(*ebiten.Image)
+ spring.images = []*ebiten.Image{
+ tilesImage.SubImage(image.Rect(176, 0, 192, 16)).(*ebiten.Image),
+ tilesImage.SubImage(image.Rect(176, 16, 192, 32)).(*ebiten.Image),
+ tilesImage.SubImage(image.Rect(176, 32, 192, 48)).(*ebiten.Image),
+ }
spring.onCollideUp = OnCollideSpring
+ spring.UpdateFunc = UpdateSpring
return spring
}
@@ -262,7 +293,10 @@ func NewSpike(game *Game, x, y float32) *GameObject{
spike := NewObject(game, x, y)
spike.offsetY = 3
- spike.image = tilesImage.SubImage(image.Rect(192, 3, 208, 16)).(*ebiten.Image)
+ spike.images = []*ebiten.Image{
+ tilesImage.SubImage(image.Rect(192, 3, 208, 16)).(*ebiten.Image),
+ }
+
spike.onCollideUp = OnCollideSpike
return spike
@@ -272,7 +306,9 @@ func NewLeftSpike(game *Game, x, y float32) *GameObject{
spike := NewObject(game, x, y)
spike.offsetX = 3
- spike.image = tilesImage.SubImage(image.Rect(195, 16, 208, 32)).(*ebiten.Image)
+ spike.images = []*ebiten.Image{
+ tilesImage.SubImage(image.Rect(195, 16, 208, 32)).(*ebiten.Image),
+ }
spike.onCollideRight = OnCollideSpike
return spike
@@ -293,8 +329,10 @@ func OnCollideExit(this, other *GameObject) bool {
}
func OnCollideSpring(this, other *GameObject) bool {
- other.vy -= SPRING_FORCE
+ other.vy = -SPRING_FORCE
other.onGround = true
+ this.state = 2
+ this.delta = 1
return false
}