-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal.go
96 lines (75 loc) · 2.05 KB
/
portal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"image"
"math"
"github.com/hajimehoshi/ebiten"
)
// Portal is a portal to another world
type Portal struct {
position Vec2f
state int // 0 -> sucking tiles; 1 -> spewing tiles
sprite Sprite
image *ebiten.Image
}
func createPortal(position Vec2f) Portal {
image := itileSpritesheet
return Portal{
position: position,
state: -1,
sprite: createSprite(Vec2i{21, 302}, Vec2i{34, 328}, Vec2i{13, 26}, image),
image: image,
}
}
func (p *Portal) update(g *Game) {
if isAABBCollision(
image.Rect(int(p.position.x), int(p.position.y), int(p.position.x)+p.sprite.size.x, int(p.position.y)+p.sprite.size.y),
g.player.getBoundsStatic()) && p.state == -1 {
p.state = 0
}
switch p.state {
case 0:
p.eatTiles(g)
g.player.position = p.position
case 1:
}
}
func (p *Portal) eatTiles(g *Game) {
moveSpeed := 1.
for i := 0; i < len(g.tiles); i++ {
for j := 0; j < len(g.tiles[i]); j++ {
// Calculate movement using an imaginary vector :)
dx := p.position.x - g.tiles[i][j].position.x
dy := p.position.y - g.tiles[i][j].position.y
ln := math.Sqrt(dx*dx + dy*dy)
dx /= ln
dy /= ln
// Move towards portal
g.tiles[i][j].position.x += dx * moveSpeed
g.tiles[i][j].position.y += dy * moveSpeed
g.tiles[i][j].rotation = dx * dy // Rotation
if g.tiles[i][j].scale.x > 0 {
g.tiles[i][j].scale.x -= 0.004 // Scale
}
if g.tiles[i][j].scale.y > 0 {
g.tiles[i][j].scale.y -= 0.004 // Scale
}
if g.tiles[i][j].scale.x <= 0 || g.tiles[i][j].scale.y <= 0 {
p.state = 1
}
}
}
}
func (p *Portal) render(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(p.position.x, p.position.y)
op.Filter = ebiten.FilterNearest
// TODO: Animate
//currentFrame := w.animation.spritesheet.sprites[w.animation.currentFrame]
/*subImageRect := image.Rect(
currentFrame.startPosition.x,
currentFrame.startPosition.y,
currentFrame.endPosition.x,
currentFrame.endPosition.y,
)*/
screen.DrawImage(p.image.SubImage(p.sprite.getBounds()).(*ebiten.Image), op) // Draw player
}