Methods now use relative coords

This commit is contained in:
Marcin Kostrzewski 2020-05-14 12:39:09 +02:00
parent 776be8fb90
commit cc98db0a96

View File

@ -105,25 +105,34 @@ class Map:
self.screen.addSprite(object, Locations.MAP) self.screen.addSprite(object, Locations.MAP)
self.collidables.add(object) self.collidables.add(object)
def getEntityOnCoord(self, coord): def getEntityOnCoord(self, coord, screenRelative=False):
"""
Get an entiity on a given coordinate
:param coord: Coords tuple of (x,y)
:param screenRelative: True, if coords are screen-relative (default = False)
:return: Entity
"""
result = None result = None
for entity in self.entities: for entity in self.entities:
if screenRelative:
if entity.rect.x == coord[0] and entity.rect.y == coord[1]: if entity.rect.x == coord[0] and entity.rect.y == coord[1]:
result = entity result = entity
else:
if entity.x == coord[0] and entity.y == coord[1]:
result = entity
return result return result
# W przypadku podania kordynatów playera, powinno zwrócić teren na którym jest player def getTileOnCoord(self, coord, screenRelative=False):
def getTileOnCoord_old(self, coord):
result = None result = None
for tile in self.terrainTilesList: for tile in self.terrainTilesList:
if tile.rect.x == coord[0] and tile.rect.y == coord[1]: isColliding = False
result = tile if screenRelative:
return result isColliding = tile.rect.collidepoint(coord[0], coord[1])
else:
def getTileOnCoord(self, coord): isColliding = tile.rect.collidepoint(coord[0] * self.tileSize + self.screen.mapCoord,
result = None coord[1] * self.tileSize)
for tile in self.terrainTilesList: if isColliding:
if tile.rect.collidepoint(coord[0], coord[1]):
result = tile result = tile
break break
return result return result
@ -138,7 +147,14 @@ class Map:
self.entities.append(entity) self.entities.append(entity)
# add object to map.collidables list to be collidable # add object to map.collidables list to be collidable
def collision(self, x, y): def collision(self, x, y, screenRelative=False):
if not screenRelative:
for b in self.collidables:
# Temp coord translation
if b.rect.x == (x * self.tileSize + self.screen.mapCoord) and b.rect.y == y * self.tileSize:
return True
return False
else:
for b in self.collidables: for b in self.collidables:
if b.rect.x == x and b.rect.y == y: if b.rect.x == x and b.rect.y == y:
return True return True