Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change get_pos and set_pos to use property decorator and setter #155

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions src/camera.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
class Camera:
def __init__(self, hero_position, current_map, screen, tile_size):
self.current_map = current_map
self.x = None
self.y = None
self.current_map = current_map
self.screen = screen
self.set_camera_position(hero_position, tile_size)

@property
def pos(self):
return self.x, self.y

@pos.setter
def pos(self, coord):
self.x, self.y = coord

def set_camera_position(self, hero_position: tuple, tile_size: int):
"""
Sets the camera position.
Expand All @@ -22,18 +30,4 @@ def set_camera_position(self, hero_position: tuple, tile_size: int):
self.x = (-column + 8) * tile_size
self.y = (-row + 7) * tile_size

def get_pos(self) -> tuple:
"""
Gets the position of a particular rectangle.
"""
return self.x, self.y

def set_pos(self, coord):
"""
Sets the position of a particular rectangle.
"""
self.x = coord[0]
self.y = coord[1]
# TODO: Investigate Python getters/setters (prop and props live templates?)

# TODO: Migrate game move method here.
2 changes: 1 addition & 1 deletion src/drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def draw_all(self, screen, loop_count, big_map, current_map, player, cmd_menu, f
# in addition to the trajectory of the NPCs
self.handle_sprite_drawing_and_animation(current_map, foreground_rects, self.background,
enable_animate)
screen.blit(self.background, camera.get_pos()) if not self.game_state.config['NO_BLIT'] else None
screen.blit(self.background, camera.pos) if not self.game_state.config['NO_BLIT'] else None
if current_map.identifier == 'TantegelThroneRoom':
self.handle_initial_dialog(initial_dialog_enabled, cmd_menu, events, skip_text, allow_save_prompt)
self.handle_post_death_dialog(game_state, cmd_menu, events, skip_text)
Expand Down
17 changes: 7 additions & 10 deletions src/game.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import json
import os
import sys

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import random
import sys
from typing import List, Tuple

from pygame import FULLSCREEN, K_1, K_2, K_3, K_4, K_DOWN, K_LEFT, K_RIGHT, K_UP, K_a, K_d, K_i, K_k, K_s, \
Expand All @@ -14,6 +12,8 @@
from pygame.time import Clock
from pygame.time import get_ticks

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from data.text.intro_lookup_table import ControlInfo
from src import maps
from src.battle import calculate_enemy_attack_damage, Battle
Expand Down Expand Up @@ -356,7 +356,6 @@ def get_events(self) -> None:
# This prints out the current tile that the player is standing on.
# print(f"self.player.current_tile: {self.player.current_tile}")


if self.show_coordinates:
if not self.last_coordinates:
self.last_coordinates = self.player.column, self.player.row
Expand All @@ -368,9 +367,7 @@ def get_events(self) -> None:
# print(f"Distance from Tantegel Castle: {self.calculation.get_distance_from_tantegel(self.player.column, self.player.row)}")
self.last_coordinates = self.player.column, self.player.row



# print(self.camera.get_pos())
# print(self.camera.pos)

# print(f"Inventory: {self.player.inventory}, Gold: {self.player.gold}")
# print(self.tiles_moved_since_spawn)
Expand Down Expand Up @@ -1062,7 +1059,7 @@ def move_player(self, current_key) -> None:
# TODO(ELF): Allow for key taps, to just face in a particular direction
# block establishes direction if needed and whether to start or stop moving
# TODO(ELF): separate dependency of camera pos and player pos
curr_pos_x, curr_pos_y = self.camera.get_pos()
curr_pos_x, curr_pos_y = self.camera.pos

if not self.player.is_moving:
if current_key[K_UP] or current_key[K_w]:
Expand Down Expand Up @@ -1126,7 +1123,7 @@ def move(self, character: Player | RoamingCharacter, delta_x: int, delta_y: int)
:return: None
"""
self.cmd_menu.camera_position = \
curr_cam_pos_x, curr_cam_pos_y = next_cam_pos_x, next_cam_pos_y = self.camera.get_pos()
curr_cam_pos_x, curr_cam_pos_y = next_cam_pos_x, next_cam_pos_y = self.camera.pos
self.check_next_tile(character)
character.next_tile_id = self.calculation.get_next_tile_identifier(character.column, character.row,
character.direction_value,
Expand All @@ -1150,7 +1147,7 @@ def move(self, character: Player | RoamingCharacter, delta_x: int, delta_y: int)
next_cam_pos_y = curr_cam_pos_y + delta_y
# character.row += -delta_y // 2
if character.identifier == 'HERO' and self.game_state.enable_movement:
self.camera.set_pos(self.move_and_handle_sides_collision(next_cam_pos_x, next_cam_pos_y))
self.camera.pos = self.move_and_handle_sides_collision(next_cam_pos_x, next_cam_pos_y)

def check_next_tile(self, character: Player | RoamingCharacter) -> None:
if not character.next_tile_checked or not character.next_tile_id:
Expand Down
2 changes: 1 addition & 1 deletion src/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, game):
self.background = self.game.drawer.background
self.color = self.game.get_current_color()
self.screen = self.game.screen
self.camera_position = self.game.camera.get_pos()
self.camera_position = self.game.camera.pos
self.current_map: DragonWarriorMap = self.game.current_map
self.current_battle = None
self.current_tile = self.player.current_tile
Expand Down
2 changes: 1 addition & 1 deletion tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ def setUp(self) -> None:
self.center_pt = 0, 0

def test_set_pos(self):
self.game.camera.set_pos((1, 2))
self.game.camera.pos = (1, 2)
self.assertEqual(1, self.game.camera.x)
self.assertEqual(2, self.game.camera.y)
6 changes: 3 additions & 3 deletions tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ def setUp(self) -> None:
self.game.screen, tile_size)

def test_get_initial_camera_position(self):
self.assertEqual((288.0, 256.0), self.camera.get_pos())
self.assertEqual((288.0, 256.0), self.camera.pos)
self.game.current_map.staircases = {
(10, 13): {'map': 'TantegelThroneRoom', 'destination_coordinates': (14, 18)}}
# self.game.change_map(TantegelThroneRoom())
# self.assertEqual((-160.0, -96.0), self.camera.get_pos())
# self.assertEqual((-160.0, -96.0), self.camera.pos)
# self.game.change_map(TantegelCourtyard())
# self.assertEqual((-192.0, -224.0), self.camera.get_pos())
# self.assertEqual((-192.0, -224.0), self.camera.pos)

# self.game.current_map.layout = [[1, 0],
# [34, 2]]
Expand Down
Loading