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

Create grenade object #52

Merged
merged 11 commits into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
15 changes: 11 additions & 4 deletions game/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ class ObjectType:
map_object = 4
damaging_object = 5
moving_object = 6
shooter = 7
item = 8
gun = 9
wall = 10
grenade = 7
shooter = 8
item = 9
gun = 10
wall = 11


class DamagingType:
none = 0
#note that bullet object has not been added yet
bullet = 1
grenade = 2

class GunType:
none = 0
handgun = 1
Expand Down
33 changes: 33 additions & 0 deletions game/common/moving/damaging/grenade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from game.common.moving.damaging.damaging_object import DamagingObject
from game.common.stats import GameStats
from game.common.enums import *


class Grenade(DamagingObject):
def __init__(self, fuse_time = GameStats.grenade_stats['min_fuse_time'], range= None, damage= None,
heading = None, speed = None, health=None, coordinates=None, hitbox=None, collidable=None):
super().__init__(range, damage, heading, speed, health, coordinates, hitbox, collidable)
self.__fuse_time = fuse_time
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use the property setting so it uses validation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you clarify which line you're talking about? (I'm assuming you're talking about line 10, but just making sure). I'm looking at damaging_object and moving_object's def init statements and they're pretty much the same- so changes will need to be made to them as well if they weren't already fixed in dev.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, line 10. The other objects need to be changed as well. If you do self.fuse_time = fuse_time that should use the property setter

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, about that- when Sean was working on the clean-up refactor branch, he had initially done that for damaging_object and moving_object, but eventually switched them back to name mangling. We actually had a discussion on that in the pull request if you wanted to pull that up and take a read. For now, I'll wait to proceed until I get a general consensus from you and Sean on how the attributes should be done. The fixes would only take a few moments anyway so it isn't a big rush.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just looked at the example I posted in dis. The backing variable comes from the property, you can use the property setter in the I init I think. I'll do some testing tonight or early tomorrow tho to double check

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mk. I'll make the changes early tomorrow if need be. Sean mentioned something about problems with a recursive overflow, so it would be good to test and make sure that isn't happening prior to making the changes.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erickbickler I think name mangling is still needed. In the example you can directly call the backing variable _temperature and change it to whatever without the property being invoked. The name mangling will prevent this and will ensure there are no "Accidental" accesses to the backing field.
test.txt

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a question of name mangling it's whether you need to set the internal name mangled variable in the init or if you can use the property setter, if you look at the example init, it uses the property setter. ie self.temperature = temperature

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attributes should be fixed now

self.object_type = ObjectType.grenade

@property
def fuse_time(self):
return self.__fuse_time

@fuse_time.setter
def fuse_time(self, val):
if val >= GameStats.grenade_stats['min_fuse_time']:
self.__fuse_time = val
else:
raise Exception("fuse time value outside bounds, Not set")


def to_json(self):
data = super().to_json()
data['fuse_time'] = self.fuse_time

return data

def from_json(self, data):
super().from_json(data)
self.fuse_time = data['fuse_time']
5 changes: 5 additions & 0 deletions game/common/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ class GameStats:
'fire_rate': 1, 'range': 100, 'mag_size': 1, 'reload_speed': 8,
'cooldown': {'max': 1, 'rate': 1}, 'level_mod': 1.25}
}

grenade_stats = {
'min_fuse_time': 10,
'max_fuse_time': 50
}
2 changes: 2 additions & 0 deletions game/test_suite/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from game.test_suite.tests.objects.test_game_board import TestGameBoard
from game.test_suite.tests.objects.test_moving_object import TestMovingObject
from game.test_suite.tests.objects.test_damaging_object import TestDamagingObject
from game.test_suite.tests.objects.test_grenade import TestGrenade
from game.test_suite.tests.objects.test_initialization import TestInit

__all__ = [
'TestGameBoard',
'TestMovingObject',
'TestDamagingObject',
'TestGrenade',
'TestInit'
]
4 changes: 2 additions & 2 deletions game/test_suite/tests/objects/test_damaging_object.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
from game.common.moving import damaging_object
from game.common.moving.damaging_object import DamagingObject
from game.common.moving.damaging import damaging_object
from game.common.moving.damaging.damaging_object import DamagingObject
from game.common.stats import GameStats

class TestDamagingObject(unittest.TestCase):
Expand Down
51 changes: 51 additions & 0 deletions game/test_suite/tests/objects/test_grenade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import unittest
from game.common.moving.damaging import grenade
from game.common.moving.damaging.grenade import Grenade
from game.common.stats import GameStats

class TestGrenade(unittest.TestCase):

def setUp(self):
self.grnObj = Grenade(10)

def test_set_get_fuse_time_valid(self):
self.grnObj.fuse_time = 20
self.assertEqual(self.grnObj.fuse_time, 20)


def test_set_get_fuse_time_invalid_low(self):
HagenSR marked this conversation as resolved.
Show resolved Hide resolved
self.assertRaises(Exception, lambda : self.grnObj.fuse_time(0))

def test_set_get_fuse_time_boundary_low(self):
self.grnObj.fuse_time = GameStats.grenade_stats['min_fuse_time']
self.assertEqual(self.grnObj.fuse_time, GameStats.grenade_stats['min_fuse_time'])


def test_set_get_fuse_time_boundary_high(self):
self.grnObj.fuse_time = GameStats.grenade_stats['max_fuse_time']
self.assertEqual(self.grnObj.fuse_time, GameStats.grenade_stats['max_fuse_time'])


def test_grenade_obj_parent_params(self):

testGrn = Grenade(fuse_time = 20, range = 10, damage = 10, heading = 10, speed = 10, health = 1, coordinates=[{'x': 450, 'y': 450}, {'x': 50, 'y': 50}],
hitbox={'width': 10, 'height': 10}, collidable=True)

self.assertIsNotNone(testGrn.range)
self.assertIsNotNone(testGrn.damage)
self.assertIsNotNone(testGrn.heading)
self.assertIsNotNone(testGrn.speed)
self.assertIsNotNone(testGrn.coordinates)
self.assertIsNotNone(testGrn.hitbox)
self.assertIsNotNone(testGrn.collidable)

self.assertIsNone(self.grnObj.range)
self.assertIsNone(self.grnObj.damage)
self.assertIsNone(self.grnObj.heading)
self.assertIsNone(self.grnObj.speed)
self.assertIsNone(self.grnObj.coordinates)
self.assertIsNone(self.grnObj.hitbox)
self.assertIsNone(self.grnObj.collidable)

if __name__ == '__main__':
unittest.main
6 changes: 4 additions & 2 deletions game/test_suite/tests/objects/test_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import unittest
from game.common.items.gun import Gun
from game.common.items.item import Item
from game.common.moving.damaging_object import DamagingObject
from game.common.moving.moving_object import MovingObject
from game.common.moving.damaging_object import DamagingObject
from game.common.moving.damaging.damaging_object import DamagingObject
from game.common.moving.damaging.grenade import Grenade
from game.common.moving.shooter import Shooter
from game.common.action import Action
from game.common.game_board import GameBoard
Expand All @@ -25,6 +25,7 @@ def setUp(self): # This method is used to set up anything you wish to test prio
breakpoint()
HagenSR marked this conversation as resolved.
Show resolved Hide resolved
self.gun = Gun()
self.item = Item()
self.grnObj = Grenade()
self.damaging = DamagingObject()
self.movObj = MovingObject(10, 10)
self.shooter = Shooter()
Expand All @@ -37,6 +38,7 @@ def setUp(self): # This method is used to set up anything you wish to test prio

self.assertEqual(self.gun.object_type, ObjectType.gun)
self.assertEqual(self.item.object_type, ObjectType.item)
self.assertEqual(self.grnObj.object_type, ObjectType.grenade)
self.assertEqual(self.damaging.object_type, ObjectType.damaging_object)
self.assertEqual(self.movObj.object_type, ObjectType.moving_object)
self.assertEqual(self.shooter.object_type, ObjectType.shooter)
Expand Down