forked from tdooner/eecs-290-super-power-robots
/
Battle.cs
176 lines (143 loc) · 6.28 KB
/
Battle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Project290.Screens;
using Microsoft.Xna.Framework;
using Project290.Rendering;
using Project290.GameElements;
using Project290.Inputs;
using Microsoft.Xna.Framework.Graphics;
using Project290.Clock;
using Project290.Physics.Dynamics;
using Project290.Physics.Collision.Shapes;
using Project290.Physics.Factories;
using Project290.Games.SuperPowerRobots.Entities;
using Project290.Physics.Common;
using Project290.Physics.Common.ConvexHull;
using Project290.Games.SuperPowerRobots.Controls;
using Project290.Physics.Dynamics.Contacts;
using Project290.Physics.Common.PolygonManipulation;
namespace Project290.Games.SuperPowerRobots
{
public class Battle
{
private XmlDocument xmlDoc;
private SortedDictionary<ulong, Entity> m_Entities;
private SPRWorld sprWorld;
private World world;
private int botHalfWidth;
public int winReward { get; private set; }
public static string nextAllies = ""; // This will be populated so the user can customize their bot.
public Battle(SPRWorld sprWorld, int botHalfWidth, World world, int currentLevel)
{
this.sprWorld = sprWorld;
this.botHalfWidth = botHalfWidth;
this.world = world;
xmlDoc = new XmlDocument();
if (nextAllies == "")
{
xmlDoc.Load("Games/SuperPowerRobots/Storage/Allies.xml");
}
else
{
xmlDoc.LoadXml(nextAllies);
}
//xmlDoc.
XmlNodeList nodes = xmlDoc.GetElementsByTagName("Bot");
Vector2[] edges = { new Vector2(-botHalfWidth, -botHalfWidth) * Settings.MetersPerPixel, new Vector2(botHalfWidth, -botHalfWidth) * Settings.MetersPerPixel, new Vector2(botHalfWidth, botHalfWidth) * Settings.MetersPerPixel, new Vector2(-botHalfWidth, botHalfWidth) * Settings.MetersPerPixel };
CreateBots(nodes, edges);
xmlDoc.Load("Games/SuperPowerRobots/Storage/Battles.xml");
nodes = xmlDoc.GetElementsByTagName("Level");
nodes = nodes[currentLevel].ChildNodes;
CreateBots(nodes, edges);
}
public void CreateBots(XmlNodeList nodes, Vector2[] edges)
{
Console.WriteLine(nodes.Count);
foreach (XmlNode botNode in nodes)
{
if (botNode.Name == "Award")
{
this.winReward = int.Parse(botNode.InnerText);
continue;
}
XmlNodeList innerNodes = botNode.ChildNodes;
Bot.Player AIType;
Console.WriteLine(innerNodes[0].InnerText);
SPRAI control;
switch (innerNodes[0].InnerText)
{
case "HumanAI":
AIType = Bot.Player.Human;
control = new HumanAI(sprWorld);
break;
case "BrickAI":
AIType = Bot.Player.Computer;
control = new BrickAI(sprWorld);
break;
case "ModeAI":
AIType = Bot.Player.Computer;
control = new ModeAI(sprWorld);
break;
default:
AIType = Bot.Player.Human;
control = new HumanAI(sprWorld);
break;
}
float health = float.Parse(innerNodes[1].InnerText);
Texture2D texture = TextureStatic.Get(innerNodes[2].InnerText);
Vector2 position = new Vector2(int.Parse(innerNodes[3].InnerText), int.Parse(innerNodes[4].InnerText));
Body tempBody = BodyFactory.CreateBody(world);
tempBody.BodyType = BodyType.Dynamic;
Fixture f = FixtureFactory.CreatePolygon(new Vertices(edges), 10f, tempBody);
f.OnCollision += MyOnCollision;
f.Friction = .5f;
f.Restitution = 0f;
tempBody.SetTransform(position * Settings.MetersPerPixel, 0);
Bot newBot = new Bot(sprWorld, tempBody, AIType, Bot.Type.FourSided, control, texture, 2 * botHalfWidth * Settings.MetersPerPixel, 2 * botHalfWidth * Settings.MetersPerPixel, health);
f.UserData = newBot;
for (int weaponNumber = 0; weaponNumber < 4; weaponNumber++)
{
XmlNodeList weaponChilds = innerNodes[weaponNumber + 5].ChildNodes;
int side = weaponNumber;
WeaponType weaponType;
switch (weaponChilds[0].InnerText)
{
case "gun":
weaponType = WeaponType.gun;
break;
case "shield":
weaponType = WeaponType.shield;
break;
case "melee":
weaponType = WeaponType.melee;
break;
default:
weaponType = WeaponType.gun;
break;
}
String weaponTexture = weaponChilds[1].InnerText;
float weaponHealth = float.Parse(weaponChilds[2].InnerText);
float weaponPower = float.Parse(weaponChilds[3].InnerText);
newBot.AddWeapon(weaponNumber, weaponTexture, weaponType, weaponHealth, weaponPower);
}
sprWorld.AddEntity(newBot);
}
}
public void AddEntity(Entity e)
{
this.m_Entities.Add(e.GetID(), e);
}
public SortedDictionary<ulong, Entity> AllEntities()
{
return this.m_Entities;
}
public bool MyOnCollision(Fixture f1, Fixture f2, Contact c)
{
//f2.Body.Dispose();
return true;
}
}
}