/// <summary> /// Renders sugar. /// </summary> /// <param name="state"><see cref="SugarState"/></param> /// <param name="pickray"><see cref="Pickray"/></param> /// <param name="selected">true, if selected</param> /// <returns>distance from viewer to item, if <see cref="Pickray"/> hits</returns> public float RenderSugar(SugarState state, Pickray pickray, bool selected) { Matrix matrix = Matrix.Translation(state.PositionX - playgroundWidth, 0, -state.PositionY + playgroundHeight); matrix.M11 = matrix.M22 = matrix.M33 = state.Radius / 50.0f; renderDevice.Material = (selected ? selectionMaterial : sugarMaterial); renderDevice.Transform.World = matrix; sugarMesh.DrawSubset(0); // Check for pickray-collision matrix.M42 = 0.0f; matrix.Invert(); pickray.Origin.TransformCoordinate(matrix); pickray.Direction.TransformNormal(matrix); if (collisionBox.Intersect(pickray.Origin, pickray.Direction)) { return (Vector3.Subtract ( pickray.Origin, new Vector3(state.PositionX - playgroundWidth, 0, -state.PositionY + playgroundHeight)).Length ()); } return(0.0f); }
/// <summary> /// Creates a sugar-state of this sugar-hill. /// </summary> /// <returns>current state of that sugar-hill.</returns> internal SugarState CreateState() { SugarState state = new SugarState((ushort)Id); state.PositionX = (ushort)(koordinate.X / SimulationEnvironment.PLAYGROUND_UNIT); state.PositionY = (ushort)(koordinate.Y / SimulationEnvironment.PLAYGROUND_UNIT); state.Radius = (ushort)(koordinate.Radius / SimulationEnvironment.PLAYGROUND_UNIT); state.Amount = (ushort)menge; return(state); }
protected override void RequestDraw() { MainState _currentState = _state; if (_currentState != null) { Index2 cells = _currentState.Map.GetCellCount(); DrawPlayground(cells, _currentState.Map.Tiles); foreach (var item in _currentState.Items) { if (item is AnthillState) { AnthillState anthillState = item as AnthillState; DrawItem(item.Id, item.Position, anthillState.Radius, null, null, Color.Brown, null, null, null, null, null, null); } if (item is AntState) { AntState antState = item as AntState; // DrawItem(item.Id, item.Position, antState.Radius, Color.Black); } if (item is SugarState) { SugarState sugarState = item as SugarState; // DrawItem(item.Id, item.Position, sugarState.Radius, Color.White); } if (item is AppleState) { AppleState appleState = item as AppleState; // DrawItem(item.Id, item.Position, 5, Color.LightGreen); } if (item is MarkerState) { MarkerState markerState = item as MarkerState; // DrawItem(item.Id, item.Position, 10, Color.Yellow); } if (item is BugState) { // DrawItem(item.Id, item.Position, 10, Color.Blue); } } } }
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.DiffuseColor = new Vector3(1, 1, 1); eff.DirectionalLight0.Direction = LIGHT_0_DIRECTION; eff.DirectionalLight1.Enabled = true; eff.DirectionalLight1.DiffuseColor = new Vector3(1, 1, 1); eff.DirectionalLight1.Direction = LIGHT_1_DIRECTION; eff.DirectionalLight2.Enabled = true; eff.DirectionalLight2.DiffuseColor = new Vector3(1, 1, 1); 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); }
private void render(object sender, PaintEventArgs e) { if (Visible && renderDevice != null) { if (watch.ElapsedMilliseconds > 40) { watch.Reset(); watch.Start(); Selection selectedItem = new Selection(); // Selektionsinfos zurücksetzen selectedItem.SelectionType = SelectionType.Nothing; selectedItem.Item = null; float distanceToSelectedItem = VIEWRANGE_MAX * VIEWRANGE_MAX; renderDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(51, 153, 255), 1.0f, 0); renderDevice.BeginScene(); //// Falls schon ein Zustand da ist kann gezeichnet werden if (simulationState != null) { SimulationState currentState = simulationState; // Update Camera camera.Update(currentState.PlaygroundWidth, currentState.PlaygroundHeight); renderDevice.Transform.View = camera.ViewMatrix; Pickray pickray = camera.Pickray; Point mousePosition = camera.MousePosition; // render Playerground modelManager.SetPlaygroundSize(currentState.PlaygroundWidth, currentState.PlaygroundHeight); modelManager.RenderPlayground(); // render these preaty little, blue items... float distance; for (int i = 0; i < currentState.BugStates.Count; i++) { if ((distance = modelManager.RenderBug(currentState.BugStates[i], pickray, false)) > 0) { // select, if pickray collides with item if (distance < distanceToSelectedItem) { distanceToSelectedItem = distance; selectedItem.Item = currentState.BugStates[i]; selectedItem.SelectionType = SelectionType.Bug; } } } // Render sugar for (int i = 0; i < currentState.SugarStates.Count; i++) { if ((distance = modelManager.RenderSugar(currentState.SugarStates[i], pickray, false)) > 0) { // select, if pickray collides with item if (distance < distanceToSelectedItem) { distanceToSelectedItem = distance; selectedItem.Item = currentState.SugarStates[i]; selectedItem.SelectionType = SelectionType.Sugar; } } } // Render Fruit for (int i = 0; i < currentState.FruitStates.Count; i++) { if ((distance = modelManager.RenderFruit(currentState.FruitStates[i], pickray, false)) > 0) { // select, if pickray collides with item if (distance < distanceToSelectedItem) { distanceToSelectedItem = distance; selectedItem.Item = currentState.FruitStates[i]; selectedItem.SelectionType = SelectionType.Fruit; } } } // Colony-specific stuff int count = 0; for (int teamIndex = 0; teamIndex < currentState.TeamStates.Count; teamIndex++) { for (int colonyIndex = 0; colonyIndex < currentState.TeamStates[teamIndex].ColonyStates.Count; colonyIndex++) { ColonyState colony = currentState.TeamStates[teamIndex].ColonyStates[colonyIndex]; // Ensure available materials for that colony modelManager.PrepareColony(count); // Render Anthills for (int anthillIndex = 0; anthillIndex < colony.AnthillStates.Count; anthillIndex++) { if ( (distance = modelManager.RenderAnthill( count, colony.AnthillStates[anthillIndex], pickray, false)) > 0) // select, if pickray collides with item { if (distance < distanceToSelectedItem) { distanceToSelectedItem = distance; selectedItem.Item = colony.AnthillStates[anthillIndex]; selectedItem.SelectionType = SelectionType.Anthill; selectedItem.AdditionalInfo = colony.ColonyName; } } } // Render Ants for (int antIndex = 0; antIndex < colony.AntStates.Count; antIndex++) { if ( (distance = modelManager.RenderAnt( count, colony.AntStates[antIndex], pickray, false)) > 0) { // select, if pickray collides with item if (distance < distanceToSelectedItem) { distanceToSelectedItem = distance; selectedItem.Item = colony.AntStates[antIndex]; selectedItem.SelectionType = SelectionType.Ant; selectedItem.AdditionalInfo = colony.ColonyName; } } } count++; } } // Render Marker // This must happen at the end, cause of alpha-tranperency count = 0; for (int teamIndex = 0; teamIndex < currentState.TeamStates.Count; teamIndex++) { TeamState team = currentState.TeamStates[teamIndex]; for (int colonyIndex = 0; colonyIndex < team.ColonyStates.Count; colonyIndex++) { ColonyState colony = team.ColonyStates[colonyIndex]; for (int markerIndex = 0; markerIndex < colony.MarkerStates.Count; markerIndex++) { MarkerState marker = colony.MarkerStates[markerIndex]; modelManager.RenderMarker(count, marker); } count++; } } // Render Statistics in the upper left corner modelManager.RenderInfobox(currentState); // Render 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 name; if (!antNames.ContainsKey(ameise.Id)) { name = names[random.Next(names.Length)]; antNames.Add(ameise.Id, name); } else { name = antNames[ameise.Id]; } line1 = string.Format(Resource.HovertextAntLine1, name, selectedItem.AdditionalInfo); line2 = string.Format(Resource.HovertextAntLine2, ameise.Vitality); break; case SelectionType.Anthill: line1 = Resource.HovertextAnthillLine1; line2 = string.Format(Resource.HovertextAnthillLine2, selectedItem.AdditionalInfo); break; case SelectionType.Bug: BugState bugState = (BugState)selectedItem.Item; line1 = Resource.HovertextBugLine1; line2 = string.Format(Resource.HovertextBugLine2, bugState.Vitality); break; case SelectionType.Fruit: FruitState fruitState = (FruitState)selectedItem.Item; line1 = Resource.HovertextFruitLine1; line2 = string.Format(Resource.HovertextFruitLine2, fruitState.Amount); break; case SelectionType.Sugar: SugarState sugar = (SugarState)selectedItem.Item; line1 = Resource.HovertextSugarLine1; line2 = string.Format(Resource.HovertextSugarLine2, sugar.Amount); break; default: line1 = String.Empty; line2 = String.Empty; break; } // Text an Mausposition ausgeben if (line1 != String.Empty || line2 != String.Empty) { modelManager.RenderInfoTag(mousePosition, line1, line2); } } } renderDevice.EndScene(); renderDevice.Present(); } Application.DoEvents(); Invalidate(); } }
/// <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); }