public bool Move(Vector2 fractionalMove, Game1 game) { FractionalPosition += fractionalMove; Point offsetMovePosition = FractionalPosition.ToPoint(); if (offsetMovePosition == Point.Zero) { return(true); } if (!CanMove(offsetMovePosition, game)) { FractionalPosition = Vector2.Zero; return(false); } FractionalPosition -= offsetMovePosition.ToVector2(); foreach (OrganismPiece piece in Pieces) { Point toCheck = WorldPos(piece.Position); game.Blocks[toCheck.X, toCheck.Y].ControllerIdx = -1; game.Blocks[toCheck.X, toCheck.Y].Type = BlockType.None; } Position = WorldPos(offsetMovePosition); OrganismPiece[] piecesCopy = new OrganismPiece[Pieces.Count]; Pieces.CopyTo(piecesCopy); foreach (OrganismPiece piece in piecesCopy) { Point toCheck = WorldPos(piece.Position); Block oldBlock = game.Blocks[toCheck.X, toCheck.Y]; if (oldBlock.Type != BlockType.None) { bool oldBone = oldBlock.Type == BlockType.Bone; bool newBone = piece.BlockType == BlockType.Bone; if ((oldBone && !newBone) || (oldBone == newBone && Game1.rnd.Next(10) == 0)) { // remove this piece from me DestroyBlock(toCheck, game); if (dead) { return(false); } continue; } else if (oldBlock.ControllerIdx != -1) { // remove this piece from the other organism game.Organisms[oldBlock.ControllerIdx].DestroyBlock(toCheck, game); } } game.Blocks[toCheck.X, toCheck.Y].ControllerIdx = organismIndex; game.Blocks[toCheck.X, toCheck.Y].Type = piece.BlockType; //Energy += game.Blocks[toCheck.X, toCheck.Y].EnergyAmount; //game.Blocks[toCheck.X, toCheck.Y].EnergyAmount = 0; } return(true); }
public void DestroyBlock(Point absolutePosition, Game1 game) { if (dead) { return; } if (game.Blocks[absolutePosition.X, absolutePosition.Y].ControllerIdx == this.organismIndex) { game.Blocks[absolutePosition.X, absolutePosition.Y].ControllerIdx = -1; game.Blocks[absolutePosition.X, absolutePosition.Y].Type = BlockType.None; } game.Blocks[absolutePosition.X, absolutePosition.Y].EnergyAmount += 5; Point posToDestroy = LocalPos(absolutePosition); for (int pieceIdx = 0; pieceIdx < Pieces.Count; ++pieceIdx) { OrganismPiece piece = Pieces[pieceIdx]; if (piece.Position != posToDestroy) { continue; } switch (piece.BlockType) { case BlockType.Heart: Die(game); return; case BlockType.Buoyancy: MovePressure.Y--; break; case BlockType.Sinker: MovePressure.Y++; break; case BlockType.Engine: if (posToDestroy.X >= 0) { MovePressure.X--; } else { MovePressure.X++; } break; case BlockType.Bone: break; } Pieces.RemoveAt(pieceIdx); return; } }
/* * static readonly EnumArray<BlockType, byte> growCosts = new EnumArray<BlockType, byte> * { * {BlockType.Leaf, 1 }, * {BlockType.Heart, 20 }, * // {BlockType.Grower, 1 }, * {BlockType.Engine, 1 }, * {BlockType.Buoyancy, 1 }, * {BlockType.Bone, 10 }, * {BlockType.Sinker, 1 }, * };*/ public bool Grow(Point positionRelativeToHeart, BlockType blockType, Game1 game, bool reproduce = true, int pieceIdx = 0) { if (Pieces.Count > 15) { return(false); } int growCost = GetGrowCost(blockType); if (Energy < growCost) { return(false); } Point newPos = WorldPos(positionRelativeToHeart); if (newPos.Y < 0 || newPos.Y >= Game1.worldHeight) { return(true); } int replaceIdx = game.Blocks[newPos.X, newPos.Y].ControllerIdx; bool replaceSelf = replaceIdx == organismIndex && game.Blocks[newPos.X, newPos.Y].Type != BlockType.Heart; if (replaceIdx != -1 && !replaceSelf) { return(true); // growth blocked } Energy -= growCost; if (replaceSelf) { DestroyBlock(newPos, game); } if (blockType == BlockType.Heart && reproduce) { Create(Mutate(Design), newPos, game); return(true); } if (blockType == BlockType.Buoyancy) { MovePressure.Y++; } else if (blockType == BlockType.Sinker) { MovePressure.Y--; } else if (blockType == BlockType.Engine) { if (positionRelativeToHeart.X >= 0) { MovePressure.X++; } else { MovePressure.X--; } } OrganismPiece newPiece = new OrganismPiece { Position = positionRelativeToHeart, BlockType = blockType }; /* * if(Pieces.FindIndex(p=> p.Position == positionRelativeToHeart) != -1) * { * int breakhere = 1; * }*/ if (pieceIdx < Pieces.Count) { Pieces.Add(Pieces[pieceIdx]); Pieces[pieceIdx] = newPiece; } else { Pieces.Add(newPiece); } Block oldBlock = game.Blocks[newPos.X, newPos.Y]; oldBlock.ControllerIdx = organismIndex; oldBlock.Type = blockType; //Energy += oldBlock.EnergyAmount; //oldBlock.EnergyAmount = 0; game.Blocks[newPos.X, newPos.Y] = oldBlock; return(true); }