public static Vector2 GetAdjustedLocation(GameObject gameObject) { ObstacleDTO obstacleDTO = new ObstacleDTO(); obstacleDTO.location = new Location(x: 0, y: 0); obstacleDTO.width = (int)Mathf.Ceil(gameObject.GetComponent <Renderer>().bounds.size.x); obstacleDTO.height = (int)Mathf.Ceil(gameObject.GetComponent <Renderer>().bounds.size.z); return(GetAdjustedLocation(obstacleDTO)); }
/* * ObstacleDTOs define the bottom left corner of the obstacle. * We need to adjust the location to the center of the obstacle for * Unity's coordinate system. */ public static Vector2 GetAdjustedLocation(ObstacleDTO obstacleDTO) { Location bottomLeftCorner = obstacleDTO.location; float shiftX = obstacleDTO.width > 1f ? -0.5f : 0f; float shiftY = obstacleDTO.height > 1f ? -0.5f : 0f; float centerX = bottomLeftCorner.x + obstacleDTO.width / 2f + shiftX; float centerY = bottomLeftCorner.y + obstacleDTO.height / 2f + shiftY; return(new Vector2(centerX, centerY)); }
public void TestAdjustedLocation() { ObstacleDTO obstacleDTO = new ObstacleDTO(); obstacleDTO.location = new Location(-4, -4); obstacleDTO.width = 2; obstacleDTO.height = 6; Vector2 adjustedLocation = ObstacleGenerator.GetAdjustedLocation(obstacleDTO); Assert.AreEqual(-3.5f, adjustedLocation.x, 0.01f); Assert.AreEqual(-1.5f, adjustedLocation.y, 0.01f); }
public void TestGenerateObstacleFromDTO() { ObstacleDTO obstacleDTO = new ObstacleDTO(); obstacleDTO.location = new Location(-4, -4); obstacleDTO.width = 2; obstacleDTO.height = 6; obstacleDTO.type = "wall"; GameObject obstacleParent = new GameObject(); GameObject generatedObstacle = ObstacleGenerator.GenerateObstacle(obstacleDTO, obstacleParent.transform); Assert.IsNotNull(generatedObstacle); Assert.AreEqual(obstacleParent.transform, generatedObstacle.transform.parent); Assert.AreEqual("Obstacle", generatedObstacle.tag); Assert.AreEqual(-3.5f, generatedObstacle.transform.position.x, 0.01f); Assert.AreEqual(-1.5f, generatedObstacle.transform.position.z, 0.01f); }
public static GameObject GenerateObstacle(ObstacleDTO obstacleDTO, Transform parent) { // TODO: era is hardcoded until we find a efficient way to pass // this from WorldControls. string resource = "Prefabs/Obstacles/obstacle_" + "future" + "_" + obstacleDTO.type; GameObject obstaclePrefab = Resources.Load <GameObject>(resource); Vector2 adjustedLocation = GetAdjustedLocation(obstacleDTO); GameObject obstacle = Object.Instantiate( obstaclePrefab, new Vector3(adjustedLocation.x, 0, adjustedLocation.y), Quaternion.identity) as GameObject; obstacle.transform.SetParent(parent: parent, worldPositionStays: true); obstacle.name = "obstacle_" + obstacleDTO.type + "_" + obstacleDTO.location.x + "_" + obstacleDTO.location.y; return(obstacle); }
public void TestObstacleDTODeserialisation() { string obstacleJSON = @" { ""obstacles"": [ { ""location"": { ""x"": 0, ""y"": 1 }, ""width"": 2, ""height"": 1, ""type"": ""van"", ""orientation"": ""east"" }, { ""location"": { ""x"": 9, ""y"": 1 }, ""width"": 20, ""height"": 2, ""type"": ""wall"", ""orientation"": ""west"" } ] } "; ObstaclesDTO obstaclesDTO = JsonUtility.FromJson <ObstaclesDTO>(obstacleJSON); Assert.AreEqual(2, obstaclesDTO.obstacles.Length); ObstacleDTO obstacleDTO = obstaclesDTO.obstacles[0]; Assert.AreEqual(new Location(0, 1), obstacleDTO.location); Assert.AreEqual(2, obstacleDTO.width); Assert.AreEqual(1, obstacleDTO.height); Assert.AreEqual(ObstacleType.Van, obstacleDTO.ObstacleType); Assert.AreEqual(Orientation.East, obstacleDTO.OrientationType); }