/
unit.py
103 lines (86 loc) · 2.71 KB
/
unit.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
99
100
101
102
103
import pygame
import os
from common import toPixels
class Unit(pygame.sprite.Sprite):
def __init__(self,x,y,event,pather,image):
pygame.sprite.Sprite.__init__(self)
self.path = []
self.target = (x,y)
self.event = event
self.pather = pather
self.image = image
self.baseImage= self.image
self.rect = self.baseImage.get_rect()
self.speed = 4
self.bearing = 0
self.turnSpeed= 10
self.x,self.y = toPixels((x, y))
self.event.register("recheckPath", self.recheck)
self.rect.center = (x+32, y+32)
def move(self,x,y):
#Set the unit to move towards grid square (x,y)
self.path = self.pather.getPath(self.target, (x,y))
def recheck(self,x,y):
#Recalculate path based on a change of passability at (x,y)
if (x,y) in self.path:
self.path = self.pather.getPath(self.target, self.path[-1])
def update(self):
reached = True
targetPixel = toPixels(self.target)
if self.rect.centerx > targetPixel[0]:
if self.bearing == 90:
self.x -= self.speed
else:
self.turnTo(90)
reached = False
elif self.rect.centerx < targetPixel[0]:
if self.bearing == 270:
self.x += self.speed
else:
self.turnTo(270)
reached = False
if self.rect.centery > targetPixel[1]:
if self.bearing == 0:
self.y -= self.speed
else:
self.turnTo(0)
reached = False
elif self.rect.centery < targetPixel[1]:
if self.bearing == 180:
self.y += self.speed
else:
self.turnTo(180)
reached = False
if reached:
if len(self.path) > 0:
self.target = self.path.pop(0)
self.image = pygame.transform.rotate(self.baseImage, self.bearing)
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
def turnTo(self, targetBearing):
if self.bearing < targetBearing:
if abs(targetBearing - self.bearing) > 180:
self.bearing -= self.turnSpeed
else:
self.bearing += self.turnSpeed
elif self.bearing > targetBearing:
if abs(targetBearing - self.bearing) > 180:
self.bearing += self.turnSpeed
else:
self.bearing -= self.turnSpeed
if self.bearing > 360:
self.bearing -= 360
elif self.bearing < 0:
self.bearing += 360
class Player(Unit):
def __init__(self,x,y,id,event,map):
self.id = id
if id == 0:
image = pygame.image.load(os.path.join("img","player_b.png"))
elif id == 1:
image = pygame.image.load(os.path.join("img","player_r.png"))
Unit.__init__(self,x,y,event,map,image)
event.register("playerMove",self.moveID)
def moveID(self,id,x,y):
if id == self.id:
self.move(x,y)