private void CreateDudes() { for (int i = 0; i < dudeCount; i++) { Dude dude = new Dude(); dude.Age = 0; dude.X = random.Next(canvasWidth); dude.Y = random.Next(canvasHeight); dude.Health = 255; dudes.Add(dude); } }
private void CreateDudes() { for (int i = 0; i < dudeCount; i++) { Dude dude = new Dude(); dude.Age = 18; dude.X = random.Next(canvasWidth); dude.Y = random.Next(canvasHeight); dude.DestinationX = dude.X; dude.DestinationY = dude.Y; dude.Health = 255; dude.Id = gameObjectId++; dude.Gender = (Dude.gender)random.Next(2); dude.firstName = GetRandomFirstName(dude.Gender); dude.surname = GetRandomSurname(); dude.Name = $"{dude.firstName} {dude.surname}"; dudes.Add(dude); } }
public void Draw() { DirectBitmap bitmap = new DirectBitmap(canvasWidth, canvasHeight); try { while (go) { for (int y = 0; y < canvasHeight; y++) { for (int x = 0; x < canvasWidth; x++) { bitmap.SetPixel(x, y, System.Drawing.Color.Black); whatsAtLocation[x, y] = new List <GameObject>(); } } foreach (Town town in towns) { if (town.X >= canvasWidth) { town.X = canvasWidth - 1; } if (town.Y >= canvasHeight) { town.Y = canvasHeight - 1; } // left if (town.X > 1) { bitmap.SetPixel((int)town.X - 1, (int)town.Y, townColour); whatsAtLocation[(int)town.X - 1, (int)town.Y].Add(town); } // right if (town.X < canvasWidth - 1) { bitmap.SetPixel((int)town.X + 1, (int)town.Y, townColour); whatsAtLocation[(int)town.X + 1, (int)town.Y].Add(town); } // top if (town.Y > 1) { bitmap.SetPixel((int)town.X, (int)town.Y - 1, townColour); whatsAtLocation[(int)town.X, (int)town.Y - 1].Add(town); } //bottom if (town.Y < canvasWidth - 1) { bitmap.SetPixel((int)town.X, (int)town.Y + 1, townColour); whatsAtLocation[(int)town.X, (int)town.Y + 1].Add(town); } // top left if (town.X > 1 && town.Y > 1) { bitmap.SetPixel((int)town.X - 1, (int)town.Y - 1, townColour); whatsAtLocation[(int)town.X - 1, (int)town.Y - 1].Add(town); } //top right if (town.X < canvasWidth - 1 && town.Y > 1) { bitmap.SetPixel((int)town.X + 1, (int)town.Y - 1, townColour); whatsAtLocation[(int)town.X + 1, (int)town.Y - 1].Add(town); } //bottom left if (town.X > 1 && town.Y < canvasHeight - 1) { bitmap.SetPixel((int)town.X - 1, (int)town.Y + 1, townColour); whatsAtLocation[(int)town.X - 1, (int)town.Y + 1].Add(town); } //bottom right if (town.X < canvasWidth - 1 && town.Y < canvasWidth - 1) { bitmap.SetPixel((int)town.X + 1, (int)town.Y + 1, townColour); whatsAtLocation[(int)town.X + 1, (int)town.Y + 1].Add(town); } bitmap.SetPixel((int)town.X, (int)town.Y, townColour); whatsAtLocation[(int)town.X, (int)town.Y].Add(town); } foreach (Shop shop in shops) { if (shop.X >= canvasWidth) { shop.X = canvasWidth - 1; } if (shop.Y >= canvasHeight) { shop.Y = canvasHeight - 1; } // left if (shop.X > 1) { bitmap.SetPixel((int)shop.X - 1, (int)shop.Y, shopColour); whatsAtLocation[(int)shop.X - 1, (int)shop.Y].Add(shop); } // right if (shop.X < canvasWidth - 1) { bitmap.SetPixel((int)shop.X + 1, (int)shop.Y, shopColour); whatsAtLocation[(int)shop.X + 1, (int)shop.Y].Add(shop); } // top if (shop.Y > 1) { bitmap.SetPixel((int)shop.X, (int)shop.Y - 1, shopColour); whatsAtLocation[(int)shop.X, (int)shop.Y - 1].Add(shop); } //bottom if (shop.Y < canvasWidth - 1) { bitmap.SetPixel((int)shop.X, (int)shop.Y + 1, shopColour); whatsAtLocation[(int)shop.X, (int)shop.Y + 1].Add(shop); } bitmap.SetPixel((int)shop.X, (int)shop.Y, shopColour); whatsAtLocation[(int)shop.X, (int)shop.Y].Add(shop); } foreach (Dude dude in dudes) { if (dude.IsAtDestination()) { dude.PickNewDestination(shops, towns, desireToGoToShop, desireToGoToTown, desireToStayAtHome, desireToGoFar); if (dude.Action != Dude.action.StayingAtHome) { string updateText = $"{dude.Name} ({(int)dude.Age}) is going to {dude.DestinationName}"; canvas.Dispatcher.Invoke(new UpdateActionLogCallback(this.UpdateActionLog), new object[] { updateText }); } } else { dude.GoToDestination(dudeSpeed); } if (dude.X >= canvasWidth) { dude.X = canvasWidth - 1; } if (dude.Y >= canvasHeight) { dude.Y = canvasHeight - 1; } dude.timeSinceLastChild += 0.01; dude.Age += 0.01; whatsAtLocation[(int)dude.X, (int)dude.Y].Add(dude); int redFactor = 0; int blueFactor = 0; int greenFactor = 0; if (dude.Gender == Dude.gender.Male) { blueFactor = 255; redFactor = 0; } else { blueFactor = 0; redFactor = 255; } double ageFactor = (dude.Age / maxAge) * 255; redFactor = redFactor - (int)ageFactor; greenFactor = greenFactor - (int)ageFactor; blueFactor = blueFactor - (int)ageFactor; if (redFactor < 0) { redFactor = 0; } if (greenFactor < 0) { greenFactor = 0; } if (blueFactor < 0) { blueFactor = 0; } bitmap.SetPixel((int)dude.X, (int)dude.Y, System.Drawing.Color.FromArgb(255, redFactor, greenFactor, blueFactor)); } //examine the gameobjects for (int Y = 0; Y < canvasHeight; Y++) { for (int X = 0; X < canvasWidth; X++) { List <GameObject> gameObjects = whatsAtLocation[X, Y]; List <Dude> dudesAtLocation = new List <Dude>(); foreach (GameObject gameObject in gameObjects) { if (gameObject is Dude) { dudesAtLocation.Add((Dude)gameObject); } } bool hasMale = false; bool hasFemale = false; Dude firstMale = new Dude(); Dude firstFemale = new Dude(); foreach (Dude dude in dudesAtLocation) { if (dude.Age > maxAge) { string updateText = $"\r\n{dude.Name} has died!!!\r\n"; canvas.Dispatcher.Invoke(new UpdateActionLogCallback(this.UpdateActionLog), new object[] { updateText }); dudes.Remove(dude); } if (dude.Gender == Dude.gender.Male && dude.Age > minBreedAge && dude.Age < maxBreedAge) { firstMale = dude; hasMale = true; } if (dude.Gender == Dude.gender.Female && dude.Age > minBreedAge && dude.Age < maxBreedAge && dude.timeSinceLastChild > 1) { firstFemale = dude; hasFemale = true; } } if (hasMale && hasFemale && dudes.Count < maxDudes && firstFemale.Action == Dude.action.StayingAtHome && firstMale.Action == Dude.action.StayingAtHome) { firstFemale.timeSinceLastChild = 0; firstMale.timeSinceLastChild = 0; Dude newDude = new Dude(); newDude.X = X; newDude.Y = Y; newDude.DestinationX = X; newDude.DestinationY = Y; newDude.DestinationName = firstFemale.DestinationName; newDude.Health = 255; newDude.Gender = (Dude.gender)random.Next(2); newDude.firstName = GetRandomFirstName(newDude.Gender); newDude.surname = $"{firstMale.surname}-{firstFemale.surname}"; if (newDude.surname.Length > 20) { newDude.surname = GetRandomSurname(); } newDude.Name = $"{newDude.firstName} {newDude.surname}"; newDude.Age = 0; newDude.Action = Dude.action.StayingAtHome; dudes.Add(newDude); string updateText = $"\r\n{newDude.Name} has been born to {firstFemale.Name} ({(int)firstFemale.Age}) and {firstMale.Name} ({(int)firstMale.Age})!!!\r\n"; canvas.Dispatcher.Invoke(new UpdateActionLogCallback(this.UpdateActionLog), new object[] { updateText }); } } } canvas.Dispatcher.Invoke(new UpdateBitmapCallback(this.UpdateCanvas), new object[] { bitmap.Bitmap }); } } catch (Exception e) { } }