本文整理汇总了C#中Pawn.OnCollisionStay方法的典型用法代码示例。如果您正苦于以下问题:C# Pawn.OnCollisionStay方法的具体用法?C# Pawn.OnCollisionStay怎么用?C# Pawn.OnCollisionStay使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Pawn
的用法示例。
在下文中一共展示了Pawn.OnCollisionStay方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetPawnPosition
public bool SetPawnPosition(Pawn pawn, Point newPosition)
{
if (!_pawns.Contains(pawn)) {
throw new ArgumentException("Pawn " + pawn.Name + " has not been added to this Board", "pawn");
}
bool moved = false;
List<HashSet<Pawn>> toRemove = new List<HashSet<Pawn>>();
List<HashSet<Pawn>> toAdd = new List<HashSet<Pawn>>();
HashSet<Pawn> enterCollisions = new HashSet<Pawn>();
HashSet<Pawn> exitCollisions = new HashSet<Pawn>();
Point oldPoint;
Point newPoint;
bool walled = false;
// Gather all pawns that were in the original and new position's footprint
foreach (Point offset in pawn.Footprint) {
HashSet<Pawn> oldBucket;
HashSet<Pawn> newBucket;
oldPoint = pawn.Position + offset;
newPoint = newPosition + offset;
// Check if there's a wall at the new point. If so, early out.
if (pawn.IsSolid) {
if (InBounds(newPoint) && GetTile(newPoint) == WALL_TILE) {
return false;
}
}
// Add all pawns in the current bucket to the exit collision set
oldBucket = GetBucket(oldPoint, false);
if (oldBucket != null) {
exitCollisions.UnionWith(oldBucket);
toRemove.Add(oldBucket);
}
// Add all pawns in the new position to the enter set
newBucket = GetBucket(newPoint, true);
enterCollisions.UnionWith(newBucket);
toAdd.Add(newBucket);
}
// If the pawn is solid, and any of the entering collision pawns are solid, don't allow the move
bool willMove = true;
if (pawn.IsSolid) {
if (walled) {
willMove = false;
} else {
foreach (Pawn other in enterCollisions) {
if (other != pawn && other.IsSolid) {
willMove = false;
break;
}
}
}
}
// Move the pawn between buckets
if (willMove) {
for (int i = 0; i < toRemove.Count; i++) {
toRemove[i].Remove(pawn);
}
for (int i = 0; i < toAdd.Count; i++) {
toAdd[i].Add(pawn);
}
pawn.SetPositionInternal(newPosition);
moved = true;
}
if (pawn.IsCollidable) {
// The stay set consists of pawns that are in both the exit and enter sets
HashSet<Pawn> stayCollisions = new HashSet<Pawn>(exitCollisions);
stayCollisions.IntersectWith(enterCollisions);
// Remove the stay collisions from both the exit and enter sets
enterCollisions.ExceptWith(stayCollisions);
exitCollisions.ExceptWith(stayCollisions);
// Call the collision methods
foreach (Pawn other in enterCollisions) {
if (pawn != other && other.IsCollidable) {
pawn.OnCollisionEnter(other);
other.OnCollisionEnter(pawn);
}
}
foreach (Pawn other in stayCollisions) {
if (pawn != other && other.IsCollidable) {
pawn.OnCollisionStay(other);
other.OnCollisionStay(pawn);
}
}
foreach (Pawn other in exitCollisions) {
//.........这里部分代码省略.........