public Creature(Point gameSpaceLimit, GraphicsDevice graphicsDevice) { pPosition = new Vector2(Worker.StaticRandom.Instance.Next(pSize, gameSpaceLimit.X - pSize), Worker.StaticRandom.Instance.Next(pSize, gameSpaceLimit.Y - pSize)); pSpeed = Worker.StaticRandom.Instance.Next(10, 50); pAngle = Math.Asin(pPosition.X / pPosition.Length()) * (180 / Math.PI); pVelocity = new Vector2(pSpeed, pSpeed); pSenseRange = Worker.StaticRandom.Instance.Next(pSize, 20); pSenses = new Worker.Circle(Origin, pSenseRange); pCollider = new Rectangle(Position.ToPoint(), new Point(pSize)); pHunger = Worker.StaticRandom.Instance.Next(50, 150); pStomach = pHunger; pGestationLength = Worker.StaticRandom.Instance.Next(20, 70); pLongevity = Worker.StaticRandom.Instance.Next(70, 140); pFertilityPeriod = Worker.StaticRandom.Instance.Next(10, 20) * 10; if (Worker.StaticRandom.Instance.Next(10) < 3) { pCarnivore = true; pColor = new Color(255, pSpeed, 0); pName += "C" + pColor.G.ToString(); } else { pCarnivore = false; pColor = new Color(0, pSpeed, 255); pName += "H" + pColor.G.ToString(); } Color[] colorData = new Color[pSize * pSize]; for (int i = 0; i < pSize * pSize; i++) { colorData[i] = pColor; } pTexture = new Texture2D(graphicsDevice, pSize, pSize); pTexture.SetData <Color>(colorData); } //initial creation
} //subsequent creation #endregion public void UpdateCreature(GameTime Time, Point gameSpaceLimit, GraphicsDevice graphicsDevice, List <Food> Foods, List <Creature> Creatures, List <Egg> Eggs) { #region Age pAge += Time.ElapsedGameTime.TotalSeconds; if (pAge >= pLongevity) { this.Death(Creatures, "Age"); } #endregion #region Movement float elapsed = (float)Time.ElapsedGameTime.TotalSeconds; #region Herbivore Movement if (pCarnivore == false) { Creature closestCarnivore = null; foreach (Creature c in Creatures) { if (c.pCarnivore == true) { if (pSenses.Contains(c.Position)) { if (closestCarnivore == null) { closestCarnivore = c; } else { if (Vector2.Distance(pPosition, c.Position) < Vector2.Distance(pPosition, closestCarnivore.Position)) { closestCarnivore = c; } } } } } if (closestCarnivore != null) { pFoodTarget = null; Vector2 start = pPosition; Vector2 end = closestCarnivore.Position; pDirection = Vector2.Normalize(end - start); pAngle = Math.Asin(pDirection.X / pDirection.Length()) * (180 / Math.PI); pPosition.X += pDirection.X * -Math.Abs(pVelocity.X) * elapsed; pPosition.Y += pDirection.Y * -Math.Abs(pVelocity.Y) * elapsed; } else { if (!Foods.Contains(pFoodTarget)) { pFoodTarget = null; } if (pFoodTarget == null) { pFoodTarget = findFoodTarget(Foods); } if (pFoodTarget != null) { Vector2 start = pPosition; Vector2 end = pFoodTarget.Position; pDirection = Vector2.Normalize(end - start); pAngle = Math.Asin(pDirection.X / pDirection.Length()) * (180 / Math.PI); float distance = Vector2.Distance(start, end); pPosition.X += pDirection.X * Math.Abs(pVelocity.X) * elapsed; pPosition.Y += pDirection.Y * Math.Abs(pVelocity.Y) * elapsed; } else { pPosition.X += pVelocity.X * (float)Math.Sin(pAngle) * elapsed; pPosition.Y += pVelocity.Y * (float)Math.Cos(pAngle) * elapsed; } } } #endregion else { if (!Creatures.Contains(pCreatureTarget)) { pCreatureTarget = null; pChaseTimer = 0; } if (pCreatureTarget == null) { pCreatureTarget = findCreatureTarget(Creatures); } if (pCreatureTarget != null && (pChaseTimer >= pChaseLimit || pCreatureTarget.CreatureTarget == this)) { pCreatureTarget = null; pChaseTimer = 0; } if (pCreatureTarget != null) { pChaseTimer += Time.ElapsedGameTime.TotalSeconds; Vector2 start = pPosition; Vector2 end = pCreatureTarget.pPosition; if (start != end) { pDirection = Vector2.Normalize(end - start); pAngle = Math.Asin(pDirection.X / pDirection.Length()) * (180 / Math.PI); pPosition.X += pDirection.X * Math.Abs(pVelocity.X) * elapsed; pPosition.Y += pDirection.Y * Math.Abs(pVelocity.Y) * elapsed; } } else { pPosition.X += pVelocity.X * (float)Math.Sin(pAngle) * elapsed; pPosition.Y += pVelocity.Y * (float)Math.Cos(pAngle) * elapsed; } } if (pPosition.X < 0) { pPosition.X = 1; pVelocity.X = -pVelocity.X; } if (pPosition.Y < 0) { pPosition.Y = 1; pVelocity.Y = -pVelocity.Y; } if (pPosition.X > gameSpaceLimit.X - pSize) { pPosition.X = gameSpaceLimit.X - pSize; pVelocity.X = -pVelocity.X; } if (pPosition.Y > gameSpaceLimit.Y - pSize) { pPosition.Y = gameSpaceLimit.Y - pSize; pVelocity.Y = -pVelocity.Y; } pCollider = new Rectangle(pPosition.ToPoint(), new Point(pSize)); pSenses = new Worker.Circle(Origin, pSenseRange); #endregion #region Food pStomach -= (pSpeed / 10) * elapsed; if (pStomach <= 0) { Death(Creatures, "Starvation: " + Foods.Count.ToString()); } #endregion #region Pregnancy pEggCountdown += Time.ElapsedGameTime.TotalSeconds; pCurrentFertility += eat(Foods, Creatures); if (pEggCountdown >= pEggInterval) { if (pCurrentFertility >= pFertilityPeriod && (Creatures.Count + Eggs.Count) < populationCap) { Eggs.Add(new Egg(pPosition, pGestationLength, this.getAttributes(), graphicsDevice)); Debug.Print("Egg Laid: " + pName); pCurrentFertility -= pFertilityPeriod; } pEggCountdown -= pEggInterval; } #endregion if (double.IsNaN(pPosition.X)) { this.Death(Creatures, "Position Error"); } }
} //initial creation public Creature(Vector2 Pos, Point gameSpaceLimit, GraphicsDevice graphicsDevice, string[] parentData) { int random; pPosition = Pos; random = Worker.StaticRandom.Instance.Next(100); if (random > 2) { pSpeed = int.Parse(parentData[0]); } else { int modifier = Worker.StaticRandom.Instance.Next(1, 3); if (modifier == 2) { modifier = -1; } pSpeed = int.Parse(parentData[0]) + (modifier * 10); if (pSpeed < 0) { pSpeed = 0; } } pAngle = Math.Asin(pPosition.X / pPosition.Length()) * (180 / Math.PI); pVelocity = new Vector2(pSpeed, pSpeed); random = Worker.StaticRandom.Instance.Next(100); if (random > 2) { pHunger = int.Parse(parentData[4]); } else { pHunger = int.Parse(parentData[0]) + (Worker.StaticRandom.Instance.Next(-1, 1) * 10); if (pHunger < 0) { pHunger = 0; } } pStomach = pHunger; pSenseRange = int.Parse(parentData[5]); random = Worker.StaticRandom.Instance.Next(100); if (random > 2) { pSenseRange = int.Parse(parentData[4]); } else { pSenseRange = int.Parse(parentData[0]) + (Worker.StaticRandom.Instance.Next(-1, 1) * 10); if (pSenseRange < 0) { pSenseRange = 0; } } pSenses = new Worker.Circle(Origin, pSenseRange); pCollider = new Rectangle(Position.ToPoint(), new Point(pSize)); random = Worker.StaticRandom.Instance.Next(100); if (random > 2) { pGestationLength = int.Parse(parentData[6]); } else { pGestationLength = int.Parse(parentData[0]) + (Worker.StaticRandom.Instance.Next(-1, 1) * 10); if (pGestationLength < 0) { pGestationLength = 0; } } random = Worker.StaticRandom.Instance.Next(100); if (random > 2) { pFertilityPeriod = int.Parse(parentData[7]); } else { pFertilityPeriod = int.Parse(parentData[0]) + (Worker.StaticRandom.Instance.Next(-1, 1) * 10); if (pFertilityPeriod < 0) { pFertilityPeriod = 0; } } random = Worker.StaticRandom.Instance.Next(100); if (random > 2) { pLongevity = int.Parse(parentData[8]); } else { pLongevity = int.Parse(parentData[0]) + (Worker.StaticRandom.Instance.Next(-1, 1) * 10); if (pLongevity < 0) { pLongevity = 0; } } pLongevity = int.Parse(parentData[8]); random = Worker.StaticRandom.Instance.Next(100); if (random > 2) { pCarnivore = bool.Parse(parentData[10]); } else { pCarnivore = !bool.Parse(parentData[10]); } if (pCarnivore == true) { pColor = new Color(255, pSpeed, 0); pName += "C" + pSpeed; } else { pColor = new Color(0, pSpeed, 255); pName += "H" + pSpeed; } Color[] colorData = new Color[pSize * pSize]; for (int i = 0; i < pSize * pSize; i++) { colorData[i] = pColor; } pTexture = new Texture2D(graphicsDevice, pSize, pSize); pTexture.SetData <Color>(colorData); pGeneration = int.Parse(parentData[9]) + 1; pName += "-" + pGeneration; } //subsequent creation