diff --git a/game/common/moving/__init__.py b/game/common/moving/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/game/common/moving/moving_object.py b/game/common/moving/moving_object.py new file mode 100644 index 00000000..be8265f2 --- /dev/null +++ b/game/common/moving/moving_object.py @@ -0,0 +1,43 @@ +from game.common.map_object import MapObject +from game.common.enums import * + +# Inherits MapObject +class MovingObject(MapObject): + def __init__(self, heading=0, speed=0): + super().__init__() + # Double underscore 'name mangles' the variable. The closest to private we can get in python + self.__heading = heading + self.__speed = speed + + def get_heading(self): + return self.__heading + + def get_speed(self): + return self.__speed + + # setter for heading. Should be degrees between 0 and 360 inclusive + def set_heading(self, val): + if val >= 0 and val <= 360: + self.__heading = val + + # Set speed must be greater than 0, potential speed limit in the future? + def set_speed(self, val): + if val >= 0: + self.__speed = val + + # To_json creates a dictionary representation of the object. + # super().to_json() calls MapObject.to_json(), which calls gameObject.to_json() + # This dictionary can then easily be converted to json by the game engine + def to_json(self): + data = super().to_json() + data['heading'] = self.heading + data['speed'] = self.speed + + return data + + # Not actually necessary, but the idea is that it takes a json representation (dictionary) + # And converts it back into an object + def from_json(self, data): + super().from_json(data) + self.heading = data['heading'] + self.speed = data['speed'] \ No newline at end of file diff --git a/game/test_suite/tests/__init__.py b/game/test_suite/tests/__init__.py index 3aea0e85..f4f13093 100644 --- a/game/test_suite/tests/__init__.py +++ b/game/test_suite/tests/__init__.py @@ -2,7 +2,9 @@ # Simply import the class from your file, and then add that class to the '__all__' array. from game.test_suite.tests.test_game_board import TestGameBoard +from game.test_suite.tests.test_moving_object import TestMovingObject __all__ = [ - 'TestGameBoard' + 'TestGameBoard', + 'TestMovingObject' ] \ No newline at end of file diff --git a/game/test_suite/tests/test_moving_object.py b/game/test_suite/tests/test_moving_object.py new file mode 100644 index 00000000..6fe6769c --- /dev/null +++ b/game/test_suite/tests/test_moving_object.py @@ -0,0 +1,66 @@ +# This is a quick example test file to show you the basics. +# Always remember to add the proper details to the __init__.py file in the 'tests' folder +# to insure your tests are run. + +import unittest +from game.common.moving.moving_object import MovingObject + +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 + def test_set_get_heading_valid(self): + self.setUp() + self.movObj.set_heading(90) + self.assertEqual(self.movObj.get_heading(), 90) + + # Test if a valid speed set works + def test_set_get_speed_valid(self): + self.setUp() + self.movObj.set_speed(100) + self.assertEqual(self.movObj.get_speed(), 100) + + # Test if invalid sets don't work + def test_set_get_heading_invalid_low(self): + self.setUp() + self.movObj.set_heading(-10) + self.assertEqual(self.movObj.get_heading(), 10) + + def test_set_get_speed_invalid_low(self): + self.setUp() + self.movObj.set_speed(-1) + self.assertEqual(self.movObj.get_speed(), 10) + + def test_set_get_heading_invalid_high(self): + self.setUp() + self.movObj.set_heading(370) + self.assertEqual(self.movObj.get_heading(), 10) + + # Check if boundary sets do work + def test_set_get_heading_boundary_high(self): + self.setUp() + self.movObj.set_heading(360) + self.assertEqual(self.movObj.get_heading(), 360) + + def test_set_get_heading_boundary_low(self): + self.setUp() + self.movObj.set_heading(0) + self.assertEqual(self.movObj.get_heading(), 0) + + def test_set_get_speed_boundary_low(self): + self.setUp() + self.movObj.set_speed(0) + self.assertEqual(self.movObj.get_speed(), 0) + + + + + + # 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 + + +if __name__ == '__main__': + unittest.main