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

Primary gun slot #78

Merged
merged 3 commits into from
Jun 30, 2021
Merged
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
43 changes: 39 additions & 4 deletions game/common/moving/shooter.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from copy import deepcopy

from game.common.moving.moving_object import MovingObject
from game.common.items.gun import Gun
from game.common.errors.inventory_full_error import InventoryFullError
from game.common.stats import GameStats
from game.common.enums import *


class Shooter(MovingObject):
"""The main player within the game logic"""
def __init__(self, heading=0, speed=0, coordinates=GameStats.player_stats['starting_coordinates'][0]):
super().__init__(
heading,
Expand Down Expand Up @@ -38,21 +40,23 @@ def __init__(self, heading=0, speed=0, coordinates=GameStats.player_stats['start
for slot_type, slot_obj_type in self.slot_obj_types
}

# set initial primary gun to be none
self.__primary_pointer = 0
self.__primary = self.__inventory['guns'][self.__primary_pointer]

@property
def inventory(self):
return deepcopy(self.__inventory)

@inventory.setter
def inventory(self, value):
self.__inventory = value

def has_empty_slot(self, slot_type):
"""check if there's an empty slot of a particular type in the inventory"""
for slot in self.__inventory[slot_type]:
if not slot:
return True
return False

def append_inventory(self, value):
"""Add object to inventory"""
if not isinstance(value, tuple(slot_type[1] for slot_type in self.slot_obj_types)):
raise TypeError(f"Value appended must be of type "
f"{[obj_type[1] for obj_type in self.slot_obj_types]} "
Expand All @@ -64,24 +68,55 @@ def append_inventory(self, value):
raise InventoryFullError(f"Inventory full for type {type(value)}")

def remove_from_inventory(self, obj):
"""Remove object from inventory"""
for slot_type in self.__inventory:
# this try except block checks to make sure you're only checking the correct slot type
try:
self.__inventory[slot_type][self.__inventory[slot_type].index(obj)] = None
except ValueError:
continue
# if a gun is removed and it's the primary one, cycle to the next one
if isinstance(obj, Gun) and obj == self.primary_gun:
self.cycle_primary()
return obj
return None

@property
def primary_gun(self):
"""Gun currently equipped"""
return self.__inventory['guns'][self.__primary_pointer]

def cycle_primary(self):
"""Cycle primary gun to the next one in the inventory"""
def cycle():
if self.__primary_pointer >= len(self.__inventory['guns']):
self.__primary_pointer = 0
return self.primary_gun
self.__primary_pointer += 1

# cycle to the next gun
cycle()
# if the next gun is None, cycle until you find one that isn't
if self.primary_gun is None:
# use a for loop here because you don't want infinite loop scenarios if they're all None
for gun in self.__inventory['guns']:
if gun is None:
cycle()
else:
break
return self.primary_gun

# set the heading and direction in a controlled way, might need to add distance attribute later
def move(self, heading, speed):
"""Set heading and speed to handle moving"""
super().heading = heading
if speed < GameStats.player_stats['move_speed']:
super().speed = speed
self.moving = True
raise ValueError("Speed must be less than max move speed for the player")

def stop(self):
"""Define stop movement"""
super().speed = 0
self.moving = False

Expand Down