using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using Microsoft.Xna.Framework; namespace TalkingIsAFreeAction { public static class Util { public static bool CheckCollision(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) { if (x1 + w1 > x2) { if (x2 + w2 > x1) { if (y1 + h1 > y2) { if (y2 + h2 > y1) { return true; } } } } return false; } public static bool CheckCollision(PhysicsObject one, PhysicsObject two) { return CheckCollision(one.x, one.y, one.width, one.height, two.x, two.y, two.width, two.height); } public static string[] FileToStringArray(string filename) { List toReturn = new List(); StreamReader sr = new StreamReader(filename); while(!sr.EndOfStream) toReturn.Add(sr.ReadLine()); return toReturn.ToArray(); } public static string ReadStringAfterEquals(string source) { return source.Substring(source.IndexOf("=") + 1); } public static int ReadIntAfterEquals(string source) { return Convert.ToInt32(source.Substring(source.IndexOf("=") + 1)); } public static string ReadStringBeforeEquals(string source) { return source.Substring(0, source.IndexOf("=")); } public static Vector2 CalculateCollision(float redX, float redY, float redWidth, float redHeight, float blueX, float blueY, float blueWidth, float blueHeight) { float redHalfSizeX = redWidth / 2f; float redHalfSizeY = redHeight / 2f; float blueHalfSizeX = blueWidth / 2f; float blueHalfSizeY = blueHeight / 2f; //Horizontal float redCenterX = redX + redHalfSizeX; float blueCenterX = blueX + blueHalfSizeX; float greenLineX = 0; if (blueCenterX > redCenterX) { greenLineX = (blueCenterX) - (redCenterX); } else { greenLineX = (redCenterX) - (blueCenterX); } float totalVectorX = (redHalfSizeX + blueHalfSizeX) - (greenLineX); //Vertical float redCenterY = redY + redHalfSizeY; float blueCenterY = blueY + blueHalfSizeY; float greenLineY = 0; if (blueCenterY > redCenterY) { greenLineY = (blueCenterY) - (redCenterY); } else { greenLineY = (redCenterY) - (blueCenterY); } float totalVectorY = (redHalfSizeY + blueHalfSizeY) - (greenLineY); return new Vector2(totalVectorX, totalVectorY); } } }