/// <summary> /// Renders Fruit. /// </summary> /// <param name="state"><see cref="FruitState"/></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 RenderFruit(FruitState state, Pickray pickray, bool selected) { Matrix matrix = Matrix.Translation(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; renderDevice.Material = (selected ? selectionMaterial : fruitMaterial); renderDevice.Transform.World = matrix; fruitMesh.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> /// Renders a bug. /// </summary> /// <param name="state"><see cref="BugState"/> for additional information</param> /// <param name="pickray">current PickRay</param> /// <param name="selected">true, if bug is selected</param> /// <returns>distance from viewer to item, if <see cref="Pickray"/> hits</returns> public float RenderBug(BugState state, Pickray pickray, bool selected) { Matrix matrix = Matrix.Identity; matrix.RotateY((float)(state.Direction * Math.PI) / 180); matrix.M41 = (state.PositionX) - playgroundWidth; matrix.M43 = (-state.PositionY) + playgroundHeight; renderDevice.Material = (selected ? selectionMaterial : bugMaterial); renderDevice.Transform.World = matrix; bugMesh.DrawSubset(0); // Check for pickray-collision 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> /// Renders an ant. /// </summary> /// <param name="colony">ID of Colony</param> /// <param name="state">State</param> /// <param name="pickray"><see cref="Pickray"/></param> /// <param name="selected">true, if ant is selected</param> /// <returns>distance from viewer to item, if <see cref="Pickray"/> hits</returns> public float RenderAnt(int colony, AntState state, Pickray pickray, bool selected) { Matrix matrix = Matrix.Identity; matrix.RotateY((float)(state.Direction * Math.PI) / 180); matrix.M41 = state.PositionX - playgroundWidth; matrix.M43 = -state.PositionY + playgroundHeight; renderDevice.Material = (selected ? selectionMaterial : antMaterial[colony]); renderDevice.Transform.World = matrix; antMesh.DrawSubset(0); // Draw sugar-block, if ant has sugar loaded if (state.LoadType == LoadType.Sugar) { matrix.M42 = 3.5f; renderDevice.Material = sugarMaterial; renderDevice.Transform.World = matrix; sugarCubeMesh.DrawSubset(0); } if (selected && state.TargetPositionX != 0) { renderDevice.Transform.World = Matrix.Identity; renderDevice.Material = sugarMaterial; CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[2]; verts[0].X = (float)state.PositionX - playgroundWidth; verts[0].Z = (float)-state.PositionY + playgroundHeight; verts[0].Y = 2; verts[1].X = (float)state.TargetPositionX - playgroundWidth; verts[1].Z = (float)-state.TargetPositionY + playgroundHeight; verts[1].Y = 2; renderDevice.VertexFormat = CustomVertex.PositionColored.Format; renderDevice.DrawUserPrimitives(PrimitiveType.LineList, 1, verts); } // 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> /// Render Anthill. /// </summary> /// <param name="colony">Colony-ID</param> /// <param name="state"><see cref="AnthillState"/></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 RenderAnthill(int colony, AnthillState state, Pickray pickray, bool selected) { Matrix matrix = Matrix.Translation(state.PositionX - playgroundWidth, 0, -state.PositionY + playgroundHeight); renderDevice.Material = (selected ? selectionMaterial : antMaterial[colony]); renderDevice.Transform.World = matrix; antHillMesh.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); }
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(); } }