-
Notifications
You must be signed in to change notification settings - Fork 12
/
PlaceEntranceMobsStep.cs
80 lines (67 loc) · 2.71 KB
/
PlaceEntranceMobsStep.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
using System;
using System.Collections.Generic;
using RogueElements;
using RogueEssence.Dungeon;
namespace RogueEssence.LevelGen
{
/// <summary>
/// Places mobs in the room where the entrance is.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TEntrance"></typeparam>
[Serializable]
public class PlaceEntranceMobsStep<T, TEntrance> : PlaceMobsStep<T> where T : ListMapGenContext, IViewPlaceableGenContext<TEntrance>
where TEntrance : IEntrance
{
public PlaceEntranceMobsStep()
{
}
public PlaceEntranceMobsStep(IMultiTeamSpawner<T> spawn) : base(spawn) { }
public override void Apply(T map)
{
List<Team> spawns = Spawn.GetSpawns(map);
if (spawns.Count == 0)
return;
// get the start room
int startRoom = 0;
Loc startLoc = map.GetLoc(0);
for (int ii = 0; ii < map.RoomPlan.RoomCount; ii++)
{
FloorRoomPlan room = map.RoomPlan.GetRoomPlan(ii);
if (map.RoomPlan.InBounds(room.RoomGen.Draw, startLoc))
{
startRoom = ii;
break;
}
}
int trials = 10 * spawns.Count;
for (int ii = 0; ii < trials && spawns.Count > 0; ii++)
{
int teamIndex = map.Rand.Next(spawns.Count);
Team newTeam = spawns[teamIndex];
List<Loc> freeTiles = Grid.FindTilesInBox(map.RoomPlan.GetRoom(startRoom).Draw.Start, map.RoomPlan.GetRoom(startRoom).Draw.Size,
(Loc testLoc) =>
{
return map.BaseCanPlaceTeam(testLoc) && testLoc != startLoc;
});
//this actually places the members of the team in random scattered locations, leaving them to group together via wandering
if (freeTiles.Count >= newTeam.MemberGuestCount)
{
Loc[] locs = new Loc[newTeam.MemberGuestCount];
for (int jj = 0; jj < locs.Length; jj++)
{
int randIndex = map.Rand.Next(freeTiles.Count);
locs[jj] = freeTiles[randIndex];
freeTiles.RemoveAt(randIndex);
}
((IGroupPlaceableGenContext<TeamSpawn>)map).PlaceItems(new TeamSpawn(newTeam, Ally), locs);
spawns.RemoveAt(teamIndex);
}
}
}
public override string ToString()
{
return String.Format("{0}", this.GetType().GetFormattedTypeName());
}
}
}