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 entity.rect.x == coord[0] and entity.rect.y == coord[1]: if screenRelative:
result = entity if entity.rect.x == coord[0] and entity.rect.y == coord[1]:
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,8 +147,15 @@ 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):
for b in self.collidables: if not screenRelative:
if b.rect.x == x and b.rect.y == y: for b in self.collidables:
return True # Temp coord translation
return False 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:
if b.rect.x == x and b.rect.y == y:
return True
return False