public bool TestMove(Person p,Vector2 to) { int x = (int)to.X; int y = (int)to.Y; int w = p.width; int h = p.height; int cw = collisionRect.Width; int ch = collisionRect.Height; int[] newCollisionRect = new int[4]; newCollisionRect[0] = x / cw + y / ch; newCollisionRect[1] = (x + w) / cw + y / ch; newCollisionRect[2] = (x + w) / cw + (y + h) / ch; newCollisionRect[3] = x / cw + (y + h) / ch; for (int j = 0; j < backgroundMap.height; j++) for (int i = 0;i<backgroundMap.width;i++) { Sprite s = backgroundMap.data[i, j]; if ((s.passable==false) && (p.collisionRect.Contains<int>(s.collisionRect[0]) || p.collisionRect.Contains<int>(s.collisionRect[1]) || p.collisionRect.Contains<int>(s.collisionRect[2]) || p.collisionRect.Contains<int>(s.collisionRect[3])) && p.TryCollisionsCheck(to,s) ) return false; } foreach (Person pp in persons.Values) { if (pp == p) continue; if (pp.sprite.passable && (p.collisionRect.Contains<int>(pp.collisionRect[0]) || p.collisionRect.Contains<int>(pp.collisionRect[1]) || p.collisionRect.Contains<int>(pp.collisionRect[2]) || p.collisionRect.Contains<int>(pp.collisionRect[3])) && p.TryCollisionsCheck(to,pp.sprite)) return false; } return true; }
void personsParse(XmlDocument xmlDc) { foreach (XmlNode node in xmlDc.SelectNodes("Resource/Person")) { Person p = new Person(); p.options = new Dictionary<string, string>(); p.pos = new Vector2(); foreach (XmlNode child in node.ChildNodes) { child.InnerText = child.InnerText.Trim().ToLower(); switch (child.Name.ToLower()) { case "name": p.name = child.InnerText; break; case "sprite": p.sprite = getSprite(child.InnerText); break; case "x": try { p.pos.X = Convert.ToInt32(child.InnerText); } catch { Game1.dbg.Log("Incorrect person x pos"); } break; case "y": try { p.pos.Y = Convert.ToInt32(child.InnerText); } catch { Game1.dbg.Log("Incorrect person y pos"); } break; case "width": try { p.width = Convert.ToInt32(child.InnerText); } catch { Game1.dbg.Log("Incorrect person width"); } break; case "height": try { p.height = Convert.ToInt32(child.InnerText); } catch { Game1.dbg.Log("Incorrect person height"); } break; default: p.options.Add(child.Name, child.InnerText); break; } } if (p.width < 0) p.width = p.sprite.width; if (p.height < 0) p.height = p.sprite.height; Persons.Add(p.name, p); } }