/// <summary> /// creates an instance of a room /// </summary> /// <param name="engine">the parent engine</param> public Room(PEngine engine) { Engine = engine; Sounds = new SoundManager(); Background = new SpriteData(); Background.LayerData = new LayerData(0); Width = 512; Height = 512; ViewPosition = new Vector2(0, 0); GameObjectList = new List <GameObject>(); GameTileList = new List <GameTile>(); LightList = new List <Light>(); Physics = null; var pp = Engine.Game.GraphicsDevice.PresentationParameters; MainTarget = new RenderTarget2D(Engine.Game.GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight); LightTarget = new RenderTarget2D(Engine.Game.GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight); }
/// <summary> /// creates an instance of a room /// </summary> /// <param name="engine">the parent engine</param> public Room(PEngine engine) { Engine = engine; Sounds = new SoundManager(); Background = null; Width = 512; Height = 512; ViewPosition = new Vector2(0, 0); GameObjectList = new List <GameObject>(); GameTileList = new List <GameTile>(); LightList = new List <Light>(); LightingEnabled = false; Physics = null; var pp = Engine.Game.GraphicsDevice.PresentationParameters; MainTarget = new RenderTarget2D(Engine.Game.GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight); LightTarget = new RenderTarget2D(Engine.Game.GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight); CurrentFileName = ""; }
// Update is called once per frame private void Update() { if (Input.GetKeyDown(KeyCode.Space)) { if (_state == null) { _state = new GJKState(ref PolytopeA, ref PolytopeB); _state.LastDirection = initDirection; } _minkowskisumPoints.Clear(); _minkowskisumPoints = MinkowskiSum.CalcMinkowskiSum(PolytopeA, PolytopeB); _gjkSupportPoints.Clear(); if (!_state.FinishRun) { GJK.Collided(ref _state); for (int i = 0; i < _state.CurrentSimplex.GetSize(); i++) { _gjkSupportPoints.Add(_state.CurrentSimplex.PeekAt(i)); } } else { // Physics res float c = 0.5f; PhysicsSim compA = _state.GetPolytopeA.GetComponent <PhysicsSim>(); PhysicsSim compB = _state.GetPolytopeB.GetComponent <PhysicsSim>(); compA.velocity = UIController.initVelocutyA; compB.velocity = UIController.initVelocutyB; float massTotal = compA.mass + compB.mass; // Contacts Vector3 lpA = _state.CurrentSimplex.PopBack(); Vector3 lpB = _state.CurrentSimplex.PopBack(); Vector3 cpA = _state.epaData.Contact; Vector3 cpB = cpA; // Inert float invVal = 1f / 0.16f; Matrix4x4 rotInert = Matrix4x4.identity; rotInert.SetRow(0, new Vector4(invVal, 0f, 0f, 0f)); rotInert.SetRow(1, new Vector4(0f, invVal, 0f, 0f)); rotInert.SetRow(2, new Vector4(0f, 0f, 0f, invVal)); float j = (-(1f + c) * Vector3.Dot(compA.velocity - compB.velocity, _state.epaData.Normal)) / ((1f / compA.mass + 1f / compB.mass) + Vector3.Dot( Vector3.Cross(rotInert * Vector3.Cross(cpA, _state.epaData.Normal), cpA) + Vector3.Cross(rotInert * Vector3.Cross(cpB, _state.epaData.Normal), cpB), _state.epaData.Normal )); Vector4 wa = rotInert * ( Vector3.Cross( cpA, j * _state.epaData.Normal ) ); compA.AngularVelocity.x += wa.x; compA.AngularVelocity.y += wa.y; compA.AngularVelocity.z += wa.z; Vector4 wb = rotInert * ( Vector3.Cross( cpB, j * _state.epaData.Normal ) ); compB.AngularVelocity.x -= wb.x; compB.AngularVelocity.y -= wb.y; compB.AngularVelocity.z -= wb.z; compA.velocity = compA.velocity + ((j / compA.mass) * _state.epaData.Normal); compB.velocity = compA.velocity - ((j / compB.mass) * _state.epaData.Normal); _state = null; } } }
/// <summary> /// loads the room from a file /// </summary> /// <param name="filename">the filename with the room data</param> public Room Load(string filename) { CurrentFileName = filename; //get the json string json = File.ReadAllText(filename, Encoding.UTF8); //turn it into an internal json object JObject obj = JObject.Parse(json); //width Width = (int)obj.GetValue("width").ToObject(typeof(int)); //height Height = (int)obj.GetValue("height").ToObject(typeof(int)); //physics if (obj.ContainsKey("physics")) { JObject physicsObj = (JObject)obj.GetValue("physics").ToObject(typeof(JObject)); float gravityX = (float)physicsObj.GetValue("gravityx").ToObject(typeof(float)); float gravityY = (float)physicsObj.GetValue("gravityy").ToObject(typeof(float)); Physics = new PhysicsSim(); Physics.Gravity = new Vector2(gravityX, gravityY); } //background if (obj.ContainsKey("background")) { JObject backObj = (JObject)obj.GetValue("background").ToObject(typeof(JObject)); string backgroundType = (string)backObj.GetValue("type").ToObject(typeof(string)); if (backgroundType.Equals("static")) { string backAssetName = (string)backObj.GetValue("name").ToObject(typeof(string)); Background = new StaticBackground(this, backAssetName); } else if (backgroundType.Equals("parallax")) { JArray backAssetNames = (JArray)backObj.GetValue("names").ToObject(typeof(JArray)); string targetName = (string)backObj.GetValue("target").ToObject(typeof(string)); string[] names = new string[backAssetNames.Count]; float[] speeds = new float[backAssetNames.Count]; for (int i = 0; i < backAssetNames.Count; i++) { JObject backLayer = (JObject)backAssetNames[i].ToObject(typeof(JObject)); names[i] = (string)backLayer.GetValue("name").ToObject(typeof(string)); speeds[i] = (float)backLayer.GetValue("speed").ToObject(typeof(float)); } Background = new ParallaxBackground(this, names, speeds, targetName); } } //world layers JArray layerArray = (JArray)obj.GetValue("layers").ToObject(typeof(JArray)); foreach (JToken item in layerArray) { JObject layerObject = (JObject)item.ToObject(typeof(JObject)); int layer = (int)layerObject.GetValue("layer").ToObject(typeof(int)); JArray objectArray = (JArray)layerObject.GetValue("objects").ToObject(typeof(JArray)); foreach (JToken gameObjectToken in objectArray) { JObject gameObjectData = (JObject)gameObjectToken.ToObject(typeof(JObject)); string internalName = (string)gameObjectData.GetValue("name").ToObject(typeof(string)); Type type = PEngine.GetTypeFromName(internalName); if (type == null) { ConsoleManager.WriteLine("could not find object name \"" + internalName + "\"", "err"); continue; } Vector2 position = new Vector2((int)gameObjectData.GetValue("x").ToObject(typeof(int)), (int)gameObjectData.GetValue("y").ToObject(typeof(int))); GameObject gameObject = (GameObject)type.GetConstructor(new Type[] { typeof(Room), typeof(Vector2) }).Invoke(new object[] { this, position }); gameObject.Sprite.LayerData.Layer = layer; GameObjectList.Add(gameObject); } JArray tileArray = (JArray)layerObject.GetValue("tiles").ToObject(typeof(JArray)); foreach (JToken tileToken in tileArray) { JObject tileData = (JObject)tileToken.ToObject(typeof(JObject)); string internalName = (string)tileData.GetValue("name").ToObject(typeof(string)); Type type = PEngine.GetTypeFromName(internalName); if (type == null) { ConsoleManager.WriteLine("could not find tile name \"" + internalName + "\"", "err"); continue; } Vector2 position = new Vector2((int)tileData.GetValue("x").ToObject(typeof(int)), (int)tileData.GetValue("y").ToObject(typeof(int))); GameTile tile = (GameTile)type.GetConstructor(new Type[] { typeof(Room), typeof(Vector2) }).Invoke(new object[] { this, position }); tile.Sprite.LayerData.Layer = layer; GameTileList.Add(tile); } } LoadAssets(Engine.Assets); return(this); }
/// <summary> /// loads the room from a file /// </summary> /// <param name="filename">the filename with the room data</param> public Room Load(string filename) { //get the json string json = File.ReadAllText(filename, Encoding.UTF8); //turn it into an internal json object JObject obj = JObject.Parse(json); //width Width = (int)obj.GetValue("width").ToObject(typeof(int)); //height Height = (int)obj.GetValue("height").ToObject(typeof(int)); //physics if (obj.ContainsKey("physics")) { JObject physicsObj = (JObject)obj.GetValue("physics").ToObject(typeof(JObject)); float gravityX = (float)physicsObj.GetValue("gravityx").ToObject(typeof(float)); float gravityY = (float)physicsObj.GetValue("gravityy").ToObject(typeof(float)); Physics = new PhysicsSim(); Physics.Gravity = new Vector2(gravityX, gravityY); } //world layers JArray layerArray = (JArray)obj.GetValue("layers").ToObject(typeof(JArray)); foreach (JToken item in layerArray) { JObject layerObject = (JObject)item.ToObject(typeof(JObject)); int layer = (int)layerObject.GetValue("layer").ToObject(typeof(int)); JArray objectArray = (JArray)layerObject.GetValue("objects").ToObject(typeof(JArray)); foreach (JToken gameObjectToken in objectArray) { JObject gameObjectData = (JObject)gameObjectToken.ToObject(typeof(JObject)); string internalName = (string)gameObjectData.GetValue("name").ToObject(typeof(string)); Type type = PEngine.GetTypeFromName(internalName); if (type == null) { ConsoleManager.WriteLine("could not find object name \"" + internalName + "\"", "err"); continue; } Vector2 position = new Vector2((int)gameObjectData.GetValue("x").ToObject(typeof(int)), (int)gameObjectData.GetValue("y").ToObject(typeof(int))); GameObject gameObject = (GameObject)type.GetConstructor(new Type[] { typeof(Room), typeof(Vector2) }).Invoke(new object[] { this, position }); gameObject.Sprite.LayerData.Layer = layer; GameObjectList.Add(gameObject); } JArray tileArray = (JArray)layerObject.GetValue("tiles").ToObject(typeof(JArray)); foreach (JToken tileToken in tileArray) { JObject tileData = (JObject)tileToken.ToObject(typeof(JObject)); string internalName = (string)tileData.GetValue("name").ToObject(typeof(string)); Type type = PEngine.GetTypeFromName(internalName); if (type == null) { ConsoleManager.WriteLine("could not find tile name \"" + internalName + "\"", "err"); continue; } Vector2 position = new Vector2((int)tileData.GetValue("x").ToObject(typeof(int)), (int)tileData.GetValue("y").ToObject(typeof(int))); GameTile tile = (GameTile)type.GetConstructor(new Type[] { typeof(Room), typeof(Vector2) }).Invoke(new object[] { this, position }); tile.Sprite.LayerData.Layer = layer; GameTileList.Add(tile); } } LoadAssets(Engine.Assets); return(this); }