-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.py
More file actions
91 lines (73 loc) · 2.85 KB
/
enemy.py
File metadata and controls
91 lines (73 loc) · 2.85 KB
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
from mapobject import MapObject
from coordinates import Coordinates
from math import *
class Enemy(MapObject):
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
def __init__(self,x,y,speed,hp,gamemap,moveto,etype):
MapObject.__init__(self, x, y)
self.map = gamemap
self.speed = speed
self.maxHp = hp
self.hp = hp
self.etype = etype
self.r = 0
self.dead = False
self.goal = False
self.moveTowards = moveto
self.coord = None
self.setCoordinates()
self.getNext()
self.direction = None
self.getDirection()
def move(self):
self.setCoordinates()
if self.isPastCenter():
self.getNext()
self.getDirection()
dx = (self.moveTowards.x+self.map.squareSize/2)-self.x
dy = (self.moveTowards.y+self.map.squareSize/2)-self.y
dist = sqrt(pow(dx,2) + pow(dy,2))
multip = self.speed/dist
x = multip*dx
y = multip*dy
self.x += x
self.y += y
def setCoordinates(self):
x = floor(self.x/self.map.squareSize)
y = floor(self.y/self.map.squareSize)
self.coord = Coordinates(x,y)
def getDirection(self):
if self.moveTowards.coord.x == self.coord.x + 1 and self.moveTowards.coord.y == self.coord.y:
#right
self.direction = 1
elif self.moveTowards.coord.x == self.coord.x and self.moveTowards.coord.y == self.coord.y+1:
#down
self.direction = 2
elif self.moveTowards.coord.x == self.coord.x -1 and self.moveTowards.coord.y == self.coord.y:
#left
self.direction = 3
elif self.moveTowards.coord.x == self.coord.x and self.moveTowards.coord.y == self.coord.y-1:
#up
self.direction = 0
def getNext(self):
if self.coord.x >= 0 and self.coord.y >= 0 and self.coord.x <= len(self.map.map)-1 and self.coord.y <= len(self.map.map[0])-1:
self.moveTowards = self.map.map[self.coord.y][self.coord.x].cameFrom
def isPastCenter(self):
# UP = 0 RIGHT = 1 DOWN = 2 LEFT = 3
if self.direction == 0 and self.y > self.moveTowards.y+20: #up
return True
elif self.direction == 1 and self.x > self.moveTowards.x+20: #right
return True
elif self.direction == 2 and self.y>self.moveTowards.y+20: #down
return True
elif self.direction == 3 and self.x < self.moveTowards.x+20: #left
return True
else:
return False
def getHit(self,damage):
self.hp -= damage
if self.hp <= 0:
self.dead = True