-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEntity.py
More file actions
113 lines (96 loc) · 3.16 KB
/
Entity.py
File metadata and controls
113 lines (96 loc) · 3.16 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import Tile
class Entity(object):
def __init__(self,level,x,y):
self.level=level
self.x=x
self.y=y
self.centreX = x
self.centreY = y
self.ticks = 0
self.steps = 0
self.movingDir = 0
self.limitedToOneTile = False
self.canPickUpTreasure = False
self.entityCollidedWith = None
self.blocksPath = True
self.isSolid = True
self.speed = 1
"""Updates logic associated with entity
@Params:
None
@Retrun:
None
"""
def tick(self):
self.ticks+=1
"""Moves the entity by xa,ya. As long as the entity will not collide
with anything by doing so. Can only move along one axis at once.
@Params:
xa(int): x movement
ya(int): y movement
@Return:
hasCollided(boolean): true if the entity has collided
"""
def move(self,xa,ya):
if xa != 0 and ya != 0:
self.move(xa, 0)
self.move(0, ya)
return True
xa=xa*self.speed
ya=ya*self.speed
if not self.hasCollided(xa, ya):
if ya < 0:
self.movingDir = 1
if ya > 0:
self.movingDir = 0
if xa < 0:
self.movingDir = 2
if xa > 0:
self.movingDir = 3
if self.ticks%self.getTileUnder().getSpeed() ==0:
self.x += xa
self.y += ya
self.steps+=1
return False
return True
"""Returns the tile under the centre of the entity
@Params:
None
@Return:
tileUnderEntity(Tile): the tile under the entity
"""
def getTileUnder(self):
return self.level.getTile(self.centreX>>5, self.centreY>>5)
"""Determins if the entity has collided
@Params:
None
@Return:
hasCollided(boolean): if the entity has collided
"""
def hasCollided(self,xa, ya):
return False
"""Checks tile at x+xa,y+ya and sees if it's solid. Also calls Tile.bump()
for tiles collided with.
@Params:
xa(int): x movement
ya(int): y movement
x(int): x position of entity
y(int): y position of entity
@Return:
ifTileIsNotPassable(bool): true if tile is solid or can't be passed
"""
def isSolidTile(self,xa, ya, x, y):
lastTile = self.level.getTile((self.x + x) >>5, (self.y + y) >>5)
nextTile = self.level.getTile((self.x + x + xa) >>5,(self.y + y + ya) >> 5)
nextTile.bump(self.level,self,(self.x + x + xa) >>5,(self.y + y + ya) >> 5)
if self.limitedToOneTile and lastTile != nextTile:#like a duck on water (not a simile)
return True
if lastTile != nextTile and nextTile.isSolid:
return True
for i in self.level.entities:
if (i.centreX>>5,i.centreY>>5) == ((self.x + x + xa) >>5,(self.y + y + ya) >> 5) and i!=self:
if not i.isSolid and i.canPickUpTreasure and self.isSolid and not self.canPickUpTreasure:
self.entityCollidedWith = i
i.entityCollidedWith = self
return True
return False