forked from Meshiest/dungeon-game
/
enemy.go
33 lines (28 loc) · 842 Bytes
/
enemy.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
package main
import (
"github.com/meshiest/go-dungeon/dungeon"
"math"
)
type Enemy struct {
X, Y, Size float64
Health int
}
func (e *Enemy) CollideWithDungeon(dungeon *dungeon.Dungeon) {
dist := e.Size/2.0
x := int(math.Floor(e.X+0.5))
y := int(math.Floor(e.Y+0.5))
if x >= 0 && y >= 0 && x < len(dungeon.Grid) && y < len(dungeon.Grid) {
if (y == 0 || dungeon.Grid[y-1][x] == 0) && e.Y - float64(y) < -dist {
e.Y = float64(y)-dist
}
if (y == len(dungeon.Grid) - 1 || dungeon.Grid[y+1][x] == 0) && e.Y - float64(y) > dist {
e.Y = float64(y)+dist
}
if (x == 0 || dungeon.Grid[y][x-1] == 0) && e.X - float64(x) < -dist {
e.X = float64(x)-dist
}
if (x == len(dungeon.Grid) - 1 || dungeon.Grid[y][x+1] == 0) && e.X - float64(x) > dist {
e.X = float64(x)+dist
}
}
}