public float DrawBug(BugState state, Pickray pickray, bool selected) { Matrix matrix = Matrix.CreateRotationY((float)(state.Direction * Math.PI) / 180); matrix.M41 = (state.PositionX) - playgroundWidth; matrix.M43 = (-state.PositionY) + playgroundHeight; foreach (var mesh in bug.Meshes) { foreach (BasicEffect eff in mesh.Effects) { eff.World = matrix; eff.View = camera.ViewMatrix; eff.Projection = camera.ProjectionMatrix; if (mesh.Name == "Sphere02" || mesh.Name == "Sphere03") { eff.LightingEnabled = false; // change the bugs eye color depending on its vitality eff.EmissiveColor = Vector3.Lerp(new Vector3(1, 0, 0), new Vector3(0f, 0.6f, 1f), state.Vitality / 1000f); } else if (mesh.Name == "Sphere01") { eff.LightingEnabled = true; eff.DiffuseColor = new Vector3(0.1f, 0.1f, 0.1f); eff.EmissiveColor = new Vector3(0f, 0f, 0f); eff.PreferPerPixelLighting = true; eff.DirectionalLight0.Enabled = true; eff.DirectionalLight0.Direction = LIGHT_0_DIRECTION; eff.DirectionalLight0.SpecularColor = new Vector3(0.6f, 1f, 1f);//new Vector3(0.7f, 0.3f, 0f);// eff.DirectionalLight1.Enabled = false; eff.DirectionalLight2.Enabled = false; } else { eff.LightingEnabled = false; } } mesh.Draw(); } BoundingSphere sphere = getBoundingSphere(bug, matrix); float?distance = sphere.Intersects(new Ray(pickray.Origin, pickray.Direction)); if (distance != null) { return(distance.Value); } return(0.0f); }
public float DrawFruit(FruitState state, Pickray pickray, bool selected) { Matrix matrix = Matrix.CreateTranslation(state.PositionX - playgroundWidth, 0, -state.PositionY + playgroundHeight); matrix.M11 = state.Radius / 4.5f; matrix.M22 = state.Radius / 4.5f; matrix.M33 = state.Radius / 4.5f; foreach (var mesh in apple.Meshes) { foreach (BasicEffect eff in mesh.Effects) { eff.World = matrix; eff.View = camera.ViewMatrix; eff.Projection = camera.ProjectionMatrix; eff.LightingEnabled = true; eff.DiffuseColor = new Vector3(0.6f, 0.7f, 0.1f); eff.EmissiveColor = new Vector3(0.1f, 0.3f, 0f); eff.DirectionalLight0.Enabled = true; eff.DirectionalLight0.Direction = LIGHT_0_DIRECTION; eff.DirectionalLight0.DiffuseColor = new Vector3(0.7f, 0.4f, 0f); eff.DirectionalLight0.SpecularColor = new Vector3(0.1f, 0.5f, 0f); eff.DirectionalLight1.Enabled = true; eff.DirectionalLight1.Direction = LIGHT_1_DIRECTION; eff.DirectionalLight1.DiffuseColor = new Vector3(0.3f, 0.1f, 0f); eff.DirectionalLight2.Enabled = false; eff.DirectionalLight2.Direction = LIGHT_2_DIRECTION; eff.DirectionalLight2.DiffuseColor = new Vector3(0.3f, 0.4f, 0f); eff.DirectionalLight0.SpecularColor = new Vector3(0.1f, 0.5f, 0f); } mesh.Draw(); } //// Check for pickray-collision float?distance = getBoundingSphere(apple, matrix).Intersects(new Ray(pickray.Origin, pickray.Direction)); if (distance.HasValue) { return(distance.Value); } return(0.0f); }
public float DrawSugar(SugarState state, Pickray pickray, bool selected) { Matrix matrix = Matrix.CreateTranslation(state.PositionX - playgroundWidth, 0, -state.PositionY + playgroundHeight); matrix.M11 = matrix.M22 = matrix.M33 = state.Radius / 50.0f; foreach (var mesh in sugar.Meshes) { foreach (BasicEffect eff in mesh.Effects) { eff.World = matrix; eff.View = camera.ViewMatrix; eff.Projection = camera.ProjectionMatrix; eff.LightingEnabled = true; eff.DiffuseColor = new Vector3(0.85f, 0.85f, 0.75f); eff.EmissiveColor = new Vector3(0.3f, 0.3f, 0.25f); eff.DirectionalLight0.Enabled = true; eff.DirectionalLight0.Direction = LIGHT_0_DIRECTION; eff.DirectionalLight1.Enabled = true; eff.DirectionalLight1.Direction = LIGHT_1_DIRECTION; eff.DirectionalLight2.Enabled = true; eff.DirectionalLight2.Direction = LIGHT_2_DIRECTION; } mesh.Draw(); } float?distance = getBoundingSphere(sugar, matrix).Intersects(new Ray(pickray.Origin, pickray.Direction)); if (distance.HasValue) { return(distance.Value); } return(0.0f); }
public float DrawAnthill(int colony, AnthillState state, Pickray pickray, bool selected) { Matrix matrix = Matrix.CreateTranslation(state.PositionX - playgroundWidth, 0, -state.PositionY + playgroundHeight); BoundingSphere collisionBox = new BoundingSphere( new Vector3(state.PositionX - playgroundWidth, 0, -state.PositionY + playgroundHeight), state.Radius); // Cone var mesh = anthill.Meshes[0]; foreach (BasicEffect eff in mesh.Effects) { eff.World = matrix; eff.View = camera.ViewMatrix; eff.Projection = camera.ProjectionMatrix; eff.LightingEnabled = true; eff.EmissiveColor = new Vector3(0.1f, 0.05f, 0f); eff.DirectionalLight0.Enabled = true; eff.DirectionalLight0.Direction = LIGHT_0_DIRECTION; eff.DirectionalLight0.DiffuseColor = new Vector3(0.4f, 0.4f, 0.4f); eff.DirectionalLight1.Enabled = true; eff.DirectionalLight1.Direction = LIGHT_1_DIRECTION; eff.DirectionalLight1.DiffuseColor = new Vector3(0.1f, 0.1f, 0.2f); eff.DirectionalLight2.Enabled = true; eff.DirectionalLight2.Direction = LIGHT_2_DIRECTION; eff.DirectionalLight2.DiffuseColor = new Vector3(0.1f, 0.1f, 0.2f); } mesh.Draw(); // Bar mesh = anthill.Meshes[1]; foreach (BasicEffect eff in mesh.Effects) { eff.World = matrix; eff.View = camera.ViewMatrix; eff.Projection = camera.ProjectionMatrix; eff.LightingEnabled = true; eff.EmissiveColor = new Vector3(0.5f, 0.5f, 0.5f); eff.DirectionalLight0.Enabled = true; eff.DirectionalLight0.Direction = LIGHT_0_DIRECTION; eff.DirectionalLight1.Enabled = true; eff.DirectionalLight1.Direction = LIGHT_1_DIRECTION; eff.DirectionalLight2.Enabled = true; eff.DirectionalLight2.Direction = LIGHT_2_DIRECTION; } mesh.Draw(); // Flag mesh = anthill.Meshes[2]; foreach (BasicEffect eff in mesh.Effects) { eff.World = matrix; eff.View = camera.ViewMatrix; eff.Projection = camera.ProjectionMatrix; eff.LightingEnabled = true; eff.EmissiveColor = playerColors[colony - 1]; eff.DirectionalLight0.Enabled = true; eff.DirectionalLight0.Direction = LIGHT_0_DIRECTION; eff.DirectionalLight1.Enabled = true; eff.DirectionalLight1.Direction = LIGHT_1_DIRECTION; eff.DirectionalLight2.Enabled = true; eff.DirectionalLight2.Direction = LIGHT_2_DIRECTION; } mesh.Draw(); // Check for pickray-collision float?distance = collisionBox.Intersects(new Ray(pickray.Origin, pickray.Direction)); if (distance.HasValue) { return(distance.Value); } return(0.0f); }
public float DrawAnt(int colony, AntState state, Pickray pickray, bool selected) { Matrix matrix = Matrix.CreateRotationY((float)(state.Direction * Math.PI) / 180); Vector3 position = new Vector3(state.PositionX - playgroundWidth, 0, -state.PositionY + playgroundHeight); matrix.M41 = position.X; matrix.M43 = position.Z; foreach (var mesh in ant.Meshes) { foreach (BasicEffect eff in mesh.Effects) { eff.World = matrix; eff.View = camera.ViewMatrix; eff.Projection = camera.ProjectionMatrix; eff.LightingEnabled = true; if (mesh.Name == "Sphere01" || mesh.Name == "Sphere02") { // eff.EmissiveColor = playerColors[colony]; eff.DiffuseColor = playerColors[colony - 1]; } else { // eff.EmissiveColor = new Vector3(0, 0, 0); eff.DiffuseColor = 0.2f * playerColors[colony - 1];//new Vector3(0.2f, 0.2f, 0.2f); } //eff.SpecularColor = new Vector3(0.5f, 0.5f, 0.5f) + playerColors[colony]; eff.DirectionalLight0.Enabled = true; eff.DirectionalLight0.Direction = LIGHT_0_DIRECTION; eff.DirectionalLight0.SpecularColor = new Vector3(1f, 1f, 1f);// + playerColors[colony]; eff.DirectionalLight1.Enabled = true; eff.DirectionalLight1.Direction = LIGHT_1_DIRECTION; eff.DirectionalLight2.Enabled = true; eff.DirectionalLight2.Direction = LIGHT_2_DIRECTION; } mesh.Draw(); } // Draw sugar-block, if ant has sugar loaded if (state.LoadType == LoadType.Sugar) { Matrix sugarWorld = Matrix.CreateScale(2) * Matrix.CreateTranslation(new Vector3(-1.5f, 4, 0)) * matrix; foreach (var mesh in box.Meshes) { foreach (BasicEffect eff in mesh.Effects) { eff.World = sugarWorld; eff.View = camera.ViewMatrix; eff.Projection = camera.ProjectionMatrix; eff.TextureEnabled = false; eff.DiffuseColor = Color.White.ToVector3(); } mesh.Draw(); } } // Debug info if (showDebugInfo) { Color color = new Color(playerColors[state.ColonyId - 1]); Vector3 antPosition = position + Vector3.Up; float sightRadius = state.ViewRange; debugRenderer.DrawHorizontalCircle(antPosition, sightRadius, 16, color); if (state.TargetType != TargetType.None) { Vector3 targetPos = new Vector3(state.TargetPositionX - playgroundWidth, 1, -state.TargetPositionY + playgroundHeight); debugRenderer.DrawLine(antPosition, targetPos, color); } } float?distance = getBoundingSphere(ant, matrix).Intersects(new Ray(pickray.Origin, pickray.Direction)); if (distance.HasValue) { return(distance.Value); } return(0.0f); }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.DepthStencilState = DepthStencilState.Default; SimulationState state = CurrentState; if (state == null) { return; } DrawSky(); effect.CurrentTechnique.Passes[0].Apply(); effect.Projection = camera.ProjectionMatrix; effect.View = camera.ViewMatrix; DrawPlayground(); Selection selectedItem = new Selection(); Pickray pickray = camera.Pickray; Point mousePosition = camera.MousePosition; // Selektionsinfos zurücksetzen selectedItem.SelectionType = SelectionType.Nothing; selectedItem.Item = null; float distanceToSelectedItem = VIEWRANGE_MAX * VIEWRANGE_MAX; // Draw Bugs float distance; foreach (var bug in state.BugStates) { if ((distance = DrawBug(bug, pickray, false)) > 0) { if (distance < distanceToSelectedItem) { distanceToSelectedItem = distance; selectedItem.Item = bug; selectedItem.SelectionType = SelectionType.Bug; } } } // Draw Sugar foreach (var sugar in state.SugarStates) { if ((distance = DrawSugar(sugar, pickray, false)) > 0) { if (distance < distanceToSelectedItem) { distanceToSelectedItem = distance; selectedItem.Item = sugar; selectedItem.SelectionType = SelectionType.Sugar; } } } // Draw Fruit foreach (var fruit in state.FruitStates) { if ((distance = DrawFruit(fruit, pickray, false)) > 0) { if (distance < distanceToSelectedItem) { distanceToSelectedItem = distance; selectedItem.Item = fruit; selectedItem.SelectionType = SelectionType.Fruit; } } } // Draw Colony Base foreach (var colony in state.ColonyStates) { // Draw AntHills foreach (var anthill in colony.AnthillStates) { if ((distance = DrawAnthill(colony.Id, anthill, pickray, false)) > 0) { if (distance < distanceToSelectedItem) { distanceToSelectedItem = distance; selectedItem.Item = anthill; selectedItem.SelectionType = SelectionType.Anthill; selectedItem.AdditionalInfo = CurrentState.ColonyStates[anthill.ColonyId - 1].ColonyName; } } } // Draw Ants foreach (var ant in colony.AntStates) { // Debug Messages aktualisieren if (!string.IsNullOrEmpty(ant.DebugMessage)) { DebugMessage msg; if (debugMessages.ContainsKey(ant.Id)) { msg = debugMessages[ant.Id]; } else { msg = new DebugMessage(); debugMessages.Add(ant.Id, msg); } msg.CreateRound = state.CurrentRound; msg.Message = ant.DebugMessage; } // Draw if ((distance = DrawAnt(colony.Id, ant, pickray, false)) > 0) { if (distance < distanceToSelectedItem) { distanceToSelectedItem = distance; selectedItem.Item = ant; selectedItem.SelectionType = SelectionType.Ant; selectedItem.AdditionalInfo = CurrentState.ColonyStates[ant.ColonyId - 1].ColonyName; } } } // Remove old Messages foreach (var key in debugMessages.Keys.ToArray()) { DebugMessage msg = debugMessages[key]; if (state.CurrentRound - msg.CreateRound > DebugMessage.ROUNDS_TO_LIFE) { debugMessages.Remove(key); } } } // Draw Marker foreach (var colony in state.ColonyStates) { foreach (var marker in colony.MarkerStates) { DrawMarker(colony.Id, marker); } } // render all sprites in one SpriteBatch.Begin()-End() cycle to save performance spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied); // Draw debug ant-thoughts if (showDebugInfo) { foreach (var colony in CurrentState.ColonyStates) { foreach (var ant in colony.AntStates) { // Draw actual debug text if (debugMessages.ContainsKey(ant.Id)) { DebugMessage msg = debugMessages[ant.Id]; Vector3 pos = new Vector3(ant.PositionX - playgroundWidth, 4, -ant.PositionY + playgroundHeight); Vector2 screenPos = debugRenderer.WorldToScreen(pos, new Vector2(0, -20)); Color boxCol = new Color(0.5f * playerColors[ant.ColonyId - 1]); boxCol.A = 128; DrawTextBox(msg.Message, screenPos, boxCol, Color.White); } } } } // Draw Infobox DrawInfobox(state); // Draw Info-Tag at selected item if (selectedItem.SelectionType != SelectionType.Nothing) { string line1; string line2; switch (selectedItem.SelectionType) { case SelectionType.Ant: AntState ameise = (AntState)selectedItem.Item; string antName = NameHelper.GetFemaleName(ameise.Id); line1 = string.Format(Strings.HovertextAntLine1, antName, selectedItem.AdditionalInfo); line2 = string.Format(Strings.HovertextAntLine2, ameise.Vitality); break; case SelectionType.Anthill: line1 = Strings.HovertextAnthillLine1; line2 = string.Format(Strings.HovertextAnthillLine2, selectedItem.AdditionalInfo); break; case SelectionType.Bug: BugState bugState = (BugState)selectedItem.Item; string bugName = NameHelper.GetMaleName(bugState.Id); line1 = string.Format(Strings.HovertextBugLine1, bugName); line2 = string.Format(Strings.HovertextBugLine2, bugState.Vitality); break; case SelectionType.Fruit: FruitState fruitState = (FruitState)selectedItem.Item; line1 = Strings.HovertextFruitLine1; line2 = string.Format(Strings.HovertextFruitLine2, fruitState.Amount); break; case SelectionType.Sugar: SugarState sugar = (SugarState)selectedItem.Item; line1 = Strings.HovertextSugarLine1; line2 = string.Format(Strings.HovertextSugarLine2, sugar.Amount); break; default: line1 = String.Empty; line2 = String.Empty; break; } // Text an Mausposition ausgeben if (line1 != String.Empty || line2 != String.Empty) { DrawInfoTag(mousePosition, line1, line2); } } spriteBatch.End(); base.Draw(gameTime); }