-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnemyTank.py
98 lines (83 loc) · 3.09 KB
/
EnemyTank.py
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
97
98
__author__ = 'John'
import sfml as sf
from Tank import TankBody
from Shell import Shell
from Player import PlayerTank, PlayerGun
from EnemyTurret import EnemyTankTurret
from workarounds import *
from Path import Path
class EnemyTank(TankBody):
def __init__(self, texture=sf.Texture.from_file("res/EnemyTankDetailed.png")):
TankBody.__init__(self, texture)
self.turret = EnemyTankTurret(sf.Texture.from_file("res/EnemyTankTurret.png"))
self.turret.set_body(self)
self.target = None
self.path = None
def check_shells(self, objects):
for ob in objects:
if isinstance(ob, Shell):
if intersects(ob.global_bounds, self.global_bounds):
self.health -= ob.damage
objects.remove(ob)
def check_sight_to_target(self, objects):
blockers = []
for ob in objects:
if ob.blocks_sight:
blockers.append(ob)
ret = True
for blocker in blockers:
if not check_sight(self.position, self.target.position, blocker):
ret = False
print "Sight blocked"
return ret
def move(self):
dx = self.path[1].position.x - self.position.x
dy = self.path[1].position.y - self.position.y
angle = math.degrees(math.atan2(dy, dx)) + 90
if abs(angle - self.rotation) < 3:
self.rotation = angle
else:
if calc_shortest_direction(self.rotation, angle) >= 0:
self.rotate(3)
else:
self.rotate(-3)
if abs(angle - self.rotation) < 10:
self.forward()
# elif 170 < abs(angle - self.rotation) < 190:
# self.backward()
def range(self):
ret = False
dx = abs(self.position.x - self.target.position.x)
dy = abs(self.position.y - self.target.position.y)
dist = math.sqrt(dx ** 2 + dy ** 2)
if dist < 300:
ret = True
return ret
def update(self, objects):
for ob in objects:
if isinstance(ob, PlayerTank) or isinstance(ob, PlayerGun):
self.target = ob
if self.target:
sight = self.check_sight_to_target(objects)
range_to_target = self.range()
if not sight or not range_to_target:
self.path = Path(self.position, self.target.position)
sight_blockers = []
for ob in objects:
if ob.blocks_sight:
sight_blockers.append(ob)
self.path.update(sight_blockers)
self.move()
else:
self.slow_down()
self.calc_position()
self.check_collisions(objects)
self.turret.update(objects)
self.check_shells(objects)
def create_enemy_tank():
tank = EnemyTank(sf.Texture.from_file("res/EnemyTankDetailed.png"))
tank.position = sf.Vector2(100, 100)
tank.rotation = 180
turret = EnemyTankTurret(sf.Texture.from_file("res/EnemyTankTurret.png"))
turret.set_body(tank)
return tank