generated from acm-ndsu/byte_le_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Created moving object * Added number checking to moving object, and tests. Fixed from_json for moving object * Added some comments
- Loading branch information
Showing
4 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |