-
Notifications
You must be signed in to change notification settings - Fork 0
/
troll.go
59 lines (49 loc) · 1.29 KB
/
troll.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
package ebitentest
import (
"ebiten-test/assets"
"github.com/hajimehoshi/ebiten/v2"
"log"
"math"
"math/rand"
)
type Troll struct {
*Rectangle
img *ebiten.Image
}
func NewTroll() *Troll {
return &Troll{
Rectangle: &Rectangle{
Width: 64,
Height: 64,
},
img: assets.Troll(),
}
}
func (t *Troll) Move(x, y float64) {
log.Printf("Moving troll to (%f, %f)\n", x, y)
t.PosX, t.PosY = x, y
}
func (t *Troll) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
w, h := t.img.Size()
scaleX, scaleY := t.Rectangle.Width / float64(w), t.Rectangle.Height / float64(h)
op.GeoM.Scale(scaleX, scaleY)
op.GeoM.Translate(t.PosX, t.PosY)
screen.DrawImage(t.img, op)
// Reference rect
//ebitenutil.DrawRect(screen, t.Rectangle.PosX, t.Rectangle.PosY, t.Rectangle.Width, t.Rectangle.Height, color.NRGBA{R: 0xff, A: 0xff})
}
func (t *Troll) Update(outsideWidth, outsideHeight float64) error {
curX, curY := ebiten.CursorPosition()
fcurX, fcurY := float64(curX), float64(curY)
if !t.PointCollides(fcurX, fcurY) {
return nil
}
maxX, maxY := outsideWidth - t.Width, outsideHeight - t.Height
newX, newY := rand.Float64() * outsideWidth, rand.Float64() * outsideHeight
newX = math.Min(newX, maxX)
newY = math.Min(newY, maxY)
t.Move(newX, newY)
//alert("Click the troll")
return nil
}