//number of units = total number of units in the map public Map(int maxX, int maxY, int numUnits) { units = new Units[numUnits]; for (int i = 0; i < numUnits; i++) { MeleeUnit m = new MeleeUnit(R.Next(0, maxX), R.Next(0, maxY), 100, 10, 1, 1, i % 2, "M"); Units[i] = m; } units = new Units[numUnits]; for (int j = 0; j < numUnits; j++) { RangedUnit r = new RangedUnit(R.Next(0, maxX), R.Next(0, maxY), 100, 10, 1, 1, j % 2, "R"); Units[j] = r; } }
private int DistanceTo(Units u) { if (u.GetType() == typeof(RangedUnit)) { RangedUnit r = (RangedUnit)u; int d = Math.Abs(XPos - r.XPos) + Math.Abs(YPos - r.XPos); return(d); } else { return(0); } }
public override bool inRange(Units u) { if (u.GetType() == typeof(RangedUnit)) { RangedUnit r = (RangedUnit)u; if (DistanceTo(u) <= Range) { return(true); } else { return(false); } } else { return(false); } }
private void UpdateMap() { foreach (Units u in map.Units) { if (u.GetType() == typeof(MeleeUnit)) { MeleeUnit m = (MeleeUnit)u; if (m.Health < 25)// running away { switch (R.Next(0, 4)) { case 0: m.Move(Direction.North); break; case 1: m.Move(Direction.East); break; case 2: m.Move(Direction.South); break; case 3: m.Move(Direction.West); break; } } else // in combat or moving toward { bool inCombat = false; foreach (Units e in map.Units) { if (u.inRange(e)) // in combat { u.Combat(e); inCombat = true; } } if (inCombat) { Units c = m.Closest(map.Units); m.Move(m.DirectionTo(c)); } } } foreach (Units Y in map.Units) { if (Y.GetType() == typeof(RangedUnit)) { RangedUnit r = (RangedUnit)Y; if (r.Health < 25)// running away { switch (R.Next(0, 4)) { case 0: r.Move(Direction.North); break; case 1: r.Move(Direction.East); break; case 2: r.Move(Direction.South); break; case 3: r.Move(Direction.West); break; } } else // in combat or moving toward { bool inCombat = false; foreach (Units e in map.Units) { if (Y.inRange(e)) // in combat { Y.Combat(e); inCombat = true; } } if (inCombat) { Units c = r.Closest(map.Units); r.Move(r.DirectionTo(c)); } } } } } }
private void DisplayMap() { groupBox1.Controls.Clear(); foreach (Units u in map.Units) { if (u.GetType() == typeof(MeleeUnit)) { int start_x, start_y; start_x = groupBox1.Location.X; start_y = groupBox1.Location.Y; MeleeUnit m = (MeleeUnit)u; Button b = new Button(); b.Size = new Size(SIZE, SIZE); b.Location = new Point(start_x + (m.XPos * SIZE), start_y + (m.YPos * SIZE)); b.Text = m.Symbol; if (m.Faction == 1) { b.ForeColor = Color.Blue; } else { b.ForeColor = Color.Orange; } if (m.IsDead()) { b.ForeColor = Color.Black; } b.Click += new EventHandler(buttn_click); groupBox1.Controls.Add(b); } foreach (Units Y in map.Units) { if (Y.GetType() == typeof(RangedUnit)) { int start_x, start_y; start_x = groupBox1.Location.X; start_y = groupBox1.Location.Y; RangedUnit r = (RangedUnit)Y; Button b = new Button(); b.Size = new Size(SIZE, SIZE); b.Location = new Point(start_x + (r.XPos * SIZE), start_y + (r.YPos * SIZE)); b.Text = r.Symbol; if (r.Faction == 1) { b.ForeColor = Color.Blue; } else { b.ForeColor = Color.Orange; } if (r.IsDead()) { b.ForeColor = Color.Black; } b.Click += new EventHandler(buttn_click); groupBox1.Controls.Add(b); } } } }