-
Notifications
You must be signed in to change notification settings - Fork 0
/
Room.cs
66 lines (59 loc) · 1.48 KB
/
Room.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace AlienGame
{
class Room
{
public List<Brush> brushes = new List<Brush>();
readonly Model model;
static Random random = new Random();
public int Content() { return brushes.First().Content; }
public Room(Model m, Brush firstBrush) { brushes.Add(firstBrush); model = m; }
public bool IntersectsWith(Rectangle r)
{
return brushes.Any(b => b.Bounds.IntersectsWith(r));
}
public static List<Room> MakeRooms(Model m)
{
var rooms = new List<Room>();
foreach (var b in m.Brushes)
{
var rr = rooms.Where(r => r.IntersectsWith(b.Bounds)).ToList();
switch (rr.Count)
{
case 0:
rooms.Add(new Room(m, b));
break;
case 1:
rr[0].brushes.Add(b);
break;
default:
rr[0].brushes.Add(b);
var toRemove = rr.Skip(1);
rr[0].brushes.AddRange(toRemove.SelectMany(a => a.brushes));
rooms.Remove(toRemove);
break;
}
}
return rooms;
}
public IEnumerable<Actor> Actors
{
get
{
return model.Actors.Where(
a => this.IntersectsWith(a.Position.ToSquare().ToPointRect()));
}
}
public Point ChooseRandomTile()
{
var brush = brushes[random.Next(brushes.Count)];
return new Point(
random.Next(brush.Bounds.Left, brush.Bounds.Right),
random.Next(brush.Bounds.Top, brush.Bounds.Bottom));
}
}
}