Skip to content

Commit

Permalink
Create shooter object (#25)
Browse files Browse the repository at this point in the history
* Created moving object

* Added number checking to moving object, and tests. Fixed from_json for moving object

* Added some comments

* add player object

* added proper super init call and finish to and from json methods

* reformatting code to align with pep8

* reformatting more code to align with pep8

* changed player to shooter, added list generation for visible and inventory to_json

* add to_json call for visible list in to_json()

* change to collidable = True

upon further thought, it's better than a comment

Co-authored-by: Sean Hagen <[email protected]>
  • Loading branch information
erickbickler and HagenSR authored May 30, 2021
1 parent bafc387 commit a7da072
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
10 changes: 7 additions & 3 deletions game/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ class ObjectType:
action = 1
player = 2
game_board = 3
map_object = 4
item = 5
gun = 6
shooter = 4
map_object = 5
item = 6
gun = 7


class GunType:
none = 0
Expand All @@ -21,12 +23,14 @@ class GunType:
shotgun = 3
sniper = 4


class GunLevel:
level_zero = 0
level_one = 1
level_two = 2
level_three = 3


class ShotPattern:
none = 0
single = 1
Expand Down
2 changes: 1 addition & 1 deletion game/common/moving/moving_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def get_speed(self):

# setter for heading. Should be degrees between 0 and 360 inclusive
def set_heading(self, val):
if val >= 0 and val <= 360:
if 0 <= val <= 360:
self.__heading = val

# Set speed must be greater than 0
Expand Down
54 changes: 54 additions & 0 deletions game/common/moving/shooter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from game.common.moving.moving_object import MovingObject
from game.common.stats import GameStats
from game.common.enums import *


class Shooter(MovingObject):
def __init__(self, heading=0, speed=0, coordinates=GameStats.player_stats['starting_coordinates']):
super().__init__(
heading,
speed,
GameStats.player_stats['starting_health'],
coordinates,
GameStats.player_stats['hitbox'],
collidable = True
)
self.object_type = ObjectType.shooter
self.inventory = []
self.money = GameStats.player_stats['starting_money']
self.armor = None
self.visible = []
self.view_radius = GameStats.player_stats['view_radius']
self.moving = False

# set the heading and direction in a controlled way, might need to add distance attribute later
def move(self, heading):
super().set_heading(heading)
super().set_speed(GameStats.player_stats['move_speed'])
self.moving = True

def stop(self):
super().set_speed(0)
self.moving = False

def to_json(self):
data = super().to_json()

data['inventory'] = [item.to_json() for item in self.inventory]
data['visible'] = [obj.to_json() for obj in self.visible]

data['money'] = self.money
data['armor'] = self.armor
data['view_radius'] = self.view_radius
data['moving'] = self.moving

return data

def from_json(self, data):
super().from_json(data)
self.inventory = data['inventory']
self.money = data['money']
self.armor = data['armor']
self.visible = data['visible']
self.view_radius = data['view_radius']
self.moving = data['moving']
5 changes: 4 additions & 1 deletion game/common/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ class GameStats:

player_stats = {
'starting_health': 10,
'starting_money': 10,
'starting_coordinates': [{'x': 450, 'y': 450}, {'x': 50, 'y': 50}],
'hitbox': {'width': 10, 'height': 10}
'hitbox': {'width': 10, 'height': 10},
'view_radius': 10,
'move_speed': 10,
}

moving_object_stats = {
Expand Down
15 changes: 7 additions & 8 deletions game/test_suite/tests/test_moving_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# to insure your tests are run.

import unittest
from game.common.moving import moving_object
from game.common.moving.moving_object import MovingObject
from game.common.stats import GameStats

class TestMovingObject(unittest.TestCase): # Your test class is a subclass of unittest.Testcase, this is important

def setUp(self): # This method is used to set up anything you wish to test prior to every test method below.
class TestMovingObject(unittest.TestCase): # Your test class is a subclass of unittest.Testcase, this is important

def setUp(self): # This method is used to set up anything you wish to test prior to every test method below.
self.movObj = MovingObject(10, 10)

# Test if a valid heading set works
Expand Down Expand Up @@ -63,14 +63,13 @@ def test_set_get_speed_boundary_low(self):

def test_moving_obj_parent_params(self):
self.setUp()
testMov = MovingObject(10, 10, health=1, coordinates=[{'x': 450, 'y': 450}, {'x': 50, 'y': 50}], hitbox={'width': 10, 'height': 10}, collidable=True)
self.assertIsNotNone(testMov.coordinates)
self.assertIsNotNone(testMov.hitbox)
self.assertIsNotNone(testMov.collidable)
test_mov = MovingObject(10, 10, health=1, coordinates=[{'x': 450, 'y': 450}, {'x': 50, 'y': 50}], hitbox={'width': 10, 'height': 10}, collidable=True)
self.assertIsNotNone(test_mov.coordinates)
self.assertIsNotNone(test_mov.hitbox)
self.assertIsNotNone(test_mov.collidable)
self.assertIsNone(self.movObj.coordinates)
self.assertIsNone(self.movObj.hitbox)
self.assertIsNone(self.movObj.collidable)


# This is just the very basics of how to set up a test file
# For more info: https://docs.python.org/3/library/unittest.html
Expand Down

0 comments on commit a7da072

Please sign in to comment.