public int GetNumTrigger(Body Body) { return ((int) MathFunctions.Clamp((int)(Math.Abs(1 * Body.BoundingBox.Max.Y - Body.BoundingBox.Min.Y)), 1, 3)); }
public int GetNumTrigger() { return ((int) MathFunctions.Clamp((int)(Math.Abs(1 * LocParent.BoundingBox.Max.Y - LocParent.BoundingBox.Min.Y)), 1, 3)); }
public static Act.Status SetTarget(string voxelOutName, string entityName, Creature creature) { Body target = creature.AI.Blackboard.GetData <Body>(entityName); if (target == null) { return(Status.Fail); } else { var targetPosition = target.BoundingBox.Center(); targetPosition = MathFunctions.Clamp(targetPosition, target.World.ChunkManager.Bounds.Expand(-1)); var voxelUnder = VoxelHelpers.FindFirstVoxelBelowIncludeWater(new VoxelHandle( creature.World.ChunkManager.ChunkData, GlobalVoxelCoordinate.FromVector3(targetPosition))); if (voxelUnder.IsValid) { voxelUnder = VoxelHelpers.GetVoxelAbove(voxelUnder); creature.AI.Blackboard.SetData(voxelOutName, voxelUnder); return(Status.Success); } else { return(Status.Fail); } } }
private IEnumerable <String> SayUtterance(IEnumerable <Utterance> utterances) { foreach (Utterance utter in utterances) { if (utter.Type == UtteranceType.Pause) { Timer pauseTimer = new Timer(0.25f, true, Timer.TimerMode.Real); while (!pauseTimer.HasTriggered && !IsSkipping) { pauseTimer.Update(DwarfTime.LastTime); yield return(""); } } else { SoundEffectInstance inst = null; if (!IsSkipping) { inst = SoundManager.PlaySound(utter.Syllable, 0.2f, MathFunctions.Clamp(MathFunctions.Rand(-0.4f, 0.4f) + Pitch, -1.0f, 1.0f));// MathFunctions.Rand(1e-2f, 2e-2f)); } foreach (char c in utter.SubSentence) { yield return("" + c); } while (!IsSkipping && inst != null && inst.State == SoundState.Playing) { yield return(""); } } } IsSkipping = false; }
void Minimap_OnClicked() { MouseState mouseState = Mouse.GetState(); Viewport viewPort = new Viewport(RenderTarget.Bounds); Rectangle imageBounds = GetImageBounds(); if (IsOverButtons(mouseState.X, mouseState.Y) || IsMinimized || SuppressClick || !imageBounds.Contains(mouseState.X, mouseState.Y)) { SuppressClick = false; return; } Vector3 forward = (PlayState.Camera.Target - PlayState.Camera.Position); forward.Normalize(); Vector3 pos = viewPort.Unproject(new Vector3(mouseState.X - imageBounds.X, mouseState.Y - imageBounds.Y, 0), Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity) - forward * 10; Vector3 target = new Vector3(pos.X, PlayState.Camera.Target.Y, pos.Z); float height = PlayState.ChunkManager.ChunkData.GetFilledVoxelGridHeightAt(target.X, target.Y, target.Z); target.Y = Math.Max(height + 15, target.Y); target = MathFunctions.Clamp(target, PlayState.ChunkManager.Bounds); PlayState.Camera.ZoomTargets.Clear(); PlayState.Camera.ZoomTargets.Add(target); }
public static BiomeData GetBiomeAt(Vector3 worldPosition) { Vector2 vec = new Vector2(worldPosition.X, worldPosition.Z) / PlayState.WorldScale; Overworld.Biome biome = Overworld.Map[(int)MathFunctions.Clamp(vec.X, 0, Overworld.Map.GetLength(0) - 1), (int)MathFunctions.Clamp(vec.Y, 0, Overworld.Map.GetLength(1) - 1)].Biome; return(BiomeLibrary.Biomes[biome]); }
public static BiomeData GetBiomeAt(Vector3 worldPos, float scale, Vector2 origin) { Vector2 v = WorldToOverworld(worldPos, scale, origin); var biome = Overworld.Map[(int)MathFunctions.Clamp(v.X, 0, Overworld.Map.GetLength(0) - 1), (int)MathFunctions.Clamp(v.Y, 0, Overworld.Map.GetLength(1) - 1)].Biome; return(BiomeLibrary.Biomes[biome]); }
public override void Render(DwarfTime time, SpriteBatch batch) { Rectangle fieldRect = new Rectangle(GlobalBounds.X, GlobalBounds.Y + GlobalBounds.Height / 2 - GUI.Skin.TileHeight / 2, GlobalBounds.Width, GUI.Skin.TileHeight); Rectangle textRect = new Rectangle(GlobalBounds.X + 5, GlobalBounds.Y + GlobalBounds.Height / 2 - GUI.Skin.TileHeight / 2, GlobalBounds.Width, GUI.Skin.TileHeight); GUI.Skin.RenderField(fieldRect, batch); string toShow = GetSubstringToShow(); Carat = MathFunctions.Clamp(Carat, 0, toShow.Length); string first = toShow.Substring(0, Carat); string last = toShow.Substring(Carat, toShow.Length - Carat); if (!HasKeyboardFocus) { Drawer2D.DrawAlignedText(batch, " " + toShow, GUI.DefaultFont, GUI.DefaultTextColor, Drawer2D.Alignment.Left, textRect); } else { if (time.TotalGameTime.TotalMilliseconds % 1000 < 500) { Drawer2D.DrawAlignedText(batch, " " + first + "|" + last, GUI.DefaultFont, GUI.DefaultTextColor, Drawer2D.Alignment.Left, textRect); } else { Drawer2D.DrawAlignedText(batch, " " + first + " " + last, GUI.DefaultFont, GUI.DefaultTextColor, Drawer2D.Alignment.Left, textRect); } } base.Render(time, batch); }
private void InputManager_KeyPressedCallback(Microsoft.Xna.Framework.Input.Keys key) { if (HasKeyboardFocus && IsEditable) { if (key == Keys.Back || key == Keys.Delete) { if (Text.Length > 0) { Text = Text.Remove(Carat - 1, 1); Carat = MathFunctions.Clamp(Carat - 1, 0, Text.Length); } } else if (key == Keys.Left) { Carat = MathFunctions.Clamp(Carat - 1, 0, Text.Length); } else if (key == Keys.Right) { Carat = MathFunctions.Clamp(Carat + 1, 0, Text.Length); } else { char k = ' '; if (InputManager.TryConvertKeyboardInput(key, Keyboard.GetState().IsKeyDown(Keys.LeftShift) || Keyboard.GetState().IsKeyDown(Keys.RightShift), out k)) { Text = Text.Insert(Carat, k.ToString()); Carat = MathFunctions.Clamp(Carat + 1, 0, Text.Length); } } OnTextModified.Invoke(Text); } }
public static float LinearInterpolate(Vector2 position, MapData[,] map, ScalarFieldType fieldType) { float x = position.X; float y = position.Y; float x1 = (int)MathFunctions.Clamp((float)Math.Ceiling(x), 0, map.GetLength(0) - 2); float y1 = (int)MathFunctions.Clamp((float)Math.Ceiling(y), 0, map.GetLength(1) - 2); float x2 = (int)MathFunctions.Clamp((float)Math.Floor(x), 0, map.GetLength(0) - 2); float y2 = (int)MathFunctions.Clamp((float)Math.Floor(y), 0, map.GetLength(1) - 2); if (Math.Abs(x1 - x2) < 0.5f) { x1 = x1 + 1; } if (Math.Abs(y1 - y2) < 0.5f) { y1 = y1 + 1; } float q11 = map[(int)x1, (int)y1].GetValue(fieldType); float q12 = map[(int)x1, (int)y2].GetValue(fieldType); float q21 = map[(int)x2, (int)y1].GetValue(fieldType); float q22 = map[(int)x2, (int)y2].GetValue(fieldType); return(MathFunctions.LinearCombination(x, y, x1, y1, x2, y2, q11, q12, q21, q22)); }
/// <summary> /// Gets a unique set of identifiers that were selected on the screen. /// </summary> /// <param name="screenRectangle">The screen rectangle to select from.</param> /// <returns></returns> public IEnumerable <uint> GetIDsSelected(Rectangle screenRectangle) { int width = Buffer.Width; int height = Buffer.Height; int startX = MathFunctions.Clamp(screenRectangle.X / Scale, 0, width - 1); int startY = MathFunctions.Clamp(screenRectangle.Y / Scale, 0, height - 1); int endX = MathFunctions.Clamp(screenRectangle.Right / Scale, 0, width - 1); int endY = MathFunctions.Clamp(screenRectangle.Bottom / Scale, 0, height - 1); HashSet <uint> selected = new HashSet <uint>(); for (int x = startX; x <= endX; x++) { for (int y = startY; y <= endY; y++) { uint id = GameComponent.GlobalIDFromColor(colorBuffer[x + y * width]); if (id == 0) { continue; } if (selected.Contains(id)) { continue; } selected.Add(id); yield return(id); } } }
public static float GetValueAt(Vector3 worldPosition, Overworld.ScalarFieldType T) { Vector2 vec = new Vector2(worldPosition.X, worldPosition.Z) / PlayState.WorldScale; return(Overworld.GetValue(Overworld.Map, new Vector2(MathFunctions.Clamp(vec.X, 0, Overworld.Map.GetLength(0) - 1), MathFunctions.Clamp(vec.Y, 0, Overworld.Map.GetLength(1) - 1)), T)); }
public void SetFullness(float value) { if (Children == null) { return; } value = MathFunctions.Clamp(value, 0.0f, 1.0f); int frame = (int)Math.Round(value * 2); if (_frame != frame) { Frame = new Point(frame, 0); //var childrenToKill = Children?.OfType<SimpleSprite>().ToList(); //foreach (var child in childrenToKill) // child.Delete(); if (GetComponent <SimpleSprite>().HasValue(out var sprite)) { sprite.SetFrame(Frame); } _frame = frame; } }
public static BiomeData GetBiomeAt(Vector2 worldPosition) { var vec = worldPosition / WorldScale; var biome = Overworld.Map[(int)MathFunctions.Clamp(vec.X, 0, Overworld.Map.GetLength(0) - 1), (int)MathFunctions.Clamp(vec.Y, 0, Overworld.Map.GetLength(1) - 1)].Biome; return(BiomeLibrary.Biomes[biome]); }
public static VoxelHandle GetValidVoxelNear(ChunkManager chunks, Microsoft.Xna.Framework.Vector3 pos) { Microsoft.Xna.Framework.BoundingBox bounds = chunks.Bounds; bounds.Max = new Microsoft.Xna.Framework.Vector3(bounds.Max.X, VoxelConstants.ChunkSizeY, bounds.Max.Z); var clampedPos = MathFunctions.Clamp(pos, chunks.Bounds) + Microsoft.Xna.Framework.Vector3.Down * 0.05f; return(new VoxelHandle(chunks.ChunkData, GlobalVoxelCoordinate.FromVector3(clampedPos))); }
public static Sound3D PlaySound(string name, Vector3 location, bool randomPitch, float volume = 1.0f, float pitch = 0.0f) { if (!HasAudioDevice) { return(null); } if (Content == null) { return(null); } SoundEffect effect = null; if (!EffectLibrary.ContainsKey(name)) { effect = Content.Load <SoundEffect>(name); EffectLibrary[name] = effect; } else { effect = EffectLibrary[name]; } if (!SoundCounts.ContainsKey(name)) { SoundCounts[name] = 0; } if (SoundCounts[name] < MaxSounds) { SoundCounts[name]++; Sound3D sound = new Sound3D { Position = location, EffectInstance = effect.CreateInstance(), HasStarted = false, Name = name }; SFXMixer.Levels levels = Mixer.GetOrCreateLevels(name); sound.EffectInstance.IsLooped = false; sound.VolumeMultiplier = volume * levels.Volume; if (randomPitch) { sound.EffectInstance.Pitch = MathFunctions.Clamp((float)(MathFunctions.Random.NextDouble() * 1.0f - 0.5f) * levels.RandomPitch + pitch, -1.0f, 1.0f); } ActiveSounds.Add(sound); return(sound); } return(null); }
public static BiomeData GetBiomeAt(Vector3 worldPos) { float x = worldPos.X; float y = worldPos.Z; Vector2 v = new Vector2(x, y) / GameSettings.Default.WorldScale; var biome = Overworld.Map[(int)MathFunctions.Clamp(v.X, 0, Overworld.Map.GetLength(0) - 1), (int)MathFunctions.Clamp(v.Y, 0, Overworld.Map.GetLength(1) - 1)].Biome; return(BiomeLibrary.Biomes[biome]); }
public void SnapToBounds(BoundingBox bounds) { Vector3 clampTarget = MathFunctions.Clamp(Target, bounds.Expand(-2.0f)); Vector3 clampPosition = MathFunctions.Clamp(Position, bounds.Expand(-2.0f)); Vector3 dTarget = clampTarget - Target; Vector3 dPosition = clampPosition - Position; Position += dTarget + dPosition; Target += dTarget + dPosition; }
public MaybeNull <BiomeData> GetBiomeAt(Vector3 worldPos, Vector2 origin) { var v = WorldToOverworld(worldPos, origin); var r = WorldToOverworldRemainder(new Vector2(worldPos.X, worldPos.Z)); var blendColor = BiomeBlend.Data[BiomeBlend.Index((int)MathFunctions.Clamp(r.X, 0, VoxelConstants.ChunkSizeX), (int)MathFunctions.Clamp(r.Y, 0, VoxelConstants.ChunkSizeZ))]; var offsetV = v + new Vector2((blendColor.R - 127.0f) / 128.0f, (blendColor.G - 127.0f) / 128.0f); var biome1 = Map[(int)MathFunctions.Clamp(v.X, 0, Map.GetLength(0) - 1), (int)MathFunctions.Clamp(v.Y, 0, Map.GetLength(1) - 1)].Biome; var biome2 = Map[(int)MathFunctions.Clamp(offsetV.X, 0, Map.GetLength(0) - 1), (int)MathFunctions.Clamp(offsetV.Y, 0, Map.GetLength(1) - 1)].Biome; return(Library.GetBiome(Math.Max(biome1, biome2))); }
public void SetFullness(float value) { value = MathFunctions.Clamp(value, 0.0f, 1.0f); int frame = (int)Math.Round(value * 2); if (_frame != frame) { ResetSprite(new SpriteSheet(ContentPaths.Entities.DwarfObjects.coinpiles, 32, 32), new Point(frame, 0)); _frame = frame; } }
public void SnapToBounds(BoundingBox bounds) { Vector3 clampTarget = MathFunctions.Clamp(Target, bounds.Expand(-2.0f)); Vector3 clampPosition = MathFunctions.Clamp(Position, bounds.Expand(-2.0f)); Vector3 dTarget = clampTarget - Target; Vector3 dPosition = clampPosition - Position; var newTarget = Target + dTarget + dPosition; var newPosition = Position + dTarget + dPosition; Target = 0.95f * Target + 0.05f * newTarget; Position = 0.95f * Position + 0.05f * newPosition; }
public void DrawHilites( WorldManager World, DesignationSet Set, Action <Vector3, Vector3, Color, float, bool> DrawBoxCallback, Action <Vector3, VoxelType> DrawPhantomCallback) { var colorModulation = Math.Abs(Math.Sin(DwarfTime.LastTime.TotalGameTime.TotalSeconds * 2.0f)); foreach (var properties in DesignationProperties) { properties.Value.ModulatedColor = new Color( (byte)(MathFunctions.Clamp((float)(properties.Value.Color.R * colorModulation + 50), 0.0f, 255.0f)), (byte)(MathFunctions.Clamp((float)(properties.Value.Color.G * colorModulation + 50), 0.0f, 255.0f)), (byte)(MathFunctions.Clamp((float)(properties.Value.Color.B * colorModulation + 50), 0.0f, 255.0f)), 255); } // Todo: Can this be drawn by the entity, allowing it to be properly frustrum culled? // - Need to add a 'gestating' entity state to the alive/dead/active mess. foreach (var entity in Set.EnumerateEntityDesignations()) { if ((entity.Type & VisibleTypes) == entity.Type) { var props = DefaultProperties; if (DesignationProperties.ContainsKey(entity.Type)) { props = DesignationProperties[entity.Type]; } // Todo: More consistent drawing? if (entity.Type == DesignationType.Craft) { entity.Body.SetFlagRecursive(GameComponent.Flag.Visible, true); if (!entity.Body.Active) { entity.Body.SetVertexColorRecursive(props.ModulatedColor); } } else { var box = entity.Body.GetBoundingBox(); DrawBoxCallback(box.Min, box.Max - box.Min, props.ModulatedColor, props.LineWidth, false); entity.Body.SetVertexColorRecursive(props.ModulatedColor); } } else if (entity.Type == DesignationType.Craft) // Make the ghost object invisible if these designations are turned off. { entity.Body.SetFlagRecursive(GameComponent.Flag.Visible, false); } } }
public void UpdateAnimation(DwarfTime gameTime, ChunkManager chunks, Camera camera) { float veloNorm = Physics.Velocity.Length(); if (veloNorm > Stats.MaxSpeed) { Physics.Velocity = (Physics.Velocity / veloNorm) * Stats.MaxSpeed; if (IsOnGround && CurrentCharacterMode == CharacterMode.Idle) { CurrentCharacterMode = CharacterMode.Walking; } } if (veloNorm > 0.25f) { if (IsOnGround && CurrentCharacterMode == CharacterMode.Idle) { CurrentCharacterMode = CharacterMode.Walking; } } if (CurrentCharacterMode == CharacterMode.Attacking) { return; } if (!IsOnGround) { return; } if (veloNorm < 0.25f || Physics.IsSleeping) { if (CurrentCharacterMode == CharacterMode.Walking) { CurrentCharacterMode = CharacterMode.Idle; } } else { if (CurrentCharacterMode == CharacterMode.Idle) { CurrentCharacterMode = CharacterMode.Walking; Animation walk = Sprite.GetAnimation(CharacterMode.Walking, Sprite.CurrentOrientation); if (walk != null) { walk.SpeedMultiplier = MathFunctions.Clamp(veloNorm / Stats.MaxSpeed * 5.0f, 0.5f, 3.0f); } } } }
public override void Render(DwarfTime time, Microsoft.Xna.Framework.Graphics.SpriteBatch batch) { Rectangle clipBounds = batch.GraphicsDevice.ScissorRectangle; Rectangle fieldRect = new Rectangle(GlobalBounds.X, GlobalBounds.Y + GlobalBounds.Height / 2 - GUI.Skin.TileHeight / 2, GlobalBounds.Width - 37, 32); GUI.Skin.RenderField(fieldRect, batch); batch.GraphicsDevice.ScissorRectangle = MathFunctions.Clamp(fieldRect, batch.GraphicsDevice.Viewport.Bounds); Drawer2D.DrawAlignedText(batch, CurrentValue, Font, Color.Black, Drawer2D.Alignment.Center, fieldRect); batch.GraphicsDevice.ScissorRectangle = clipBounds; GUI.Skin.RenderDownArrow(new Rectangle(GlobalBounds.X + GlobalBounds.Width - 32, GlobalBounds.Y + GlobalBounds.Height / 2 - GUI.Skin.TileHeight / 2, 32, 32), batch); base.Render(time, batch); }
private void DrawVoxels(DwarfTime time, IEnumerable <VoxelHandle> selected) { GameState.Game.GraphicsDevice.DepthStencilState = DepthStencilState.None; Effect = World.Renderer.DefaultShader; float t = (float)time.TotalGameTime.TotalSeconds; float st = (float)Math.Sin(t * 4) * 0.5f + 0.5f; float alpha = 0.25f * st + 0.6f; Effect.MainTexture = AssetManager.GetContentTexture(ContentPaths.Terrain.terrain_tiles); Effect.LightRamp = Color.White; Effect.SetTexturedTechnique(); DrawVoxels(MathFunctions.Clamp(alpha * 0.5f, 0.25f, 1.0f), selected); GameState.Game.GraphicsDevice.DepthStencilState = DepthStencilState.Default; DrawVoxels(MathFunctions.Clamp(alpha, 0.25f, 1.0f), selected); Effect.LightRamp = Color.White; Effect.VertexColorTint = Color.White; Effect.World = Matrix.Identity; }
/* * public void GenerateOres(VoxelChunk chunk, ComponentManager components, ContentManager content, * GraphicsDevice graphics) * { * Vector3 origin = chunk.Origin; * int chunkSizeX = chunk.SizeX; * int chunkSizeY = chunk.SizeY; * int chunkSizeZ = chunk.SizeZ; * Voxel v = chunk.MakeVoxel(0, 0, 0); * for (int x = 0; x < chunkSizeX; x++) * { * for (int z = 0; z < chunkSizeZ; z++) * { * int h = chunk.GetFilledVoxelGridHeightAt(x, chunkSizeY - 1, z); * for (int y = 1; y < chunkSizeY; y++) * { * foreach ( * KeyValuePair<string, VoxelLibrary.ResourceSpawnRate> spawns in VoxelLibrary.ResourceSpawns) * { * float s = spawns.Value.VeinSize; * float p = spawns.Value.VeinSpawnThreshold; * v.GridPosition = new Vector3(x, y, z); * if (v.IsEmpty || y >= h - 1 || !(y - h/2 < spawns.Value.MaximumHeight) || * !(y - h/2 > spawns.Value.MinimumHeight) || * !(PlayState.Random.NextDouble() <= spawns.Value.Probability) || v.Type.Name != "Stone") * { * continue; * } * * float caviness = (float) NoiseGenerator.Noise((float) (x + origin.X)*s, * (float) (z + origin.Z)*s, * (float) (y + origin.Y + h)*s); * * if (caviness > p) * { * v.Type = VoxelLibrary.GetVoxelType(spawns.Key); * } * continue; * } * } * } * } * } */ public void GenerateFauna(VoxelChunk chunk, ComponentManager components, ContentManager content, GraphicsDevice graphics, FactionLibrary factions) { int waterHeight = (int)(SeaLevel * chunk.SizeY); Voxel v = chunk.MakeVoxel(0, 0, 0); for (int x = 0; x < chunk.SizeX; x++) { for (int z = 0; z < chunk.SizeZ; z++) { Vector2 vec = new Vector2(x + chunk.Origin.X, z + chunk.Origin.Z) / PlayState.WorldScale; Overworld.Biome biome = Overworld.Map[(int)MathFunctions.Clamp(vec.X, 0, Overworld.Map.GetLength(0) - 1), (int)MathFunctions.Clamp(vec.Y, 0, Overworld.Map.GetLength(1) - 1)].Biome; BiomeData biomeData = BiomeLibrary.Biomes[biome]; int y = chunk.GetFilledVoxelGridHeightAt(x, chunk.SizeY - 1, z); if (!chunk.IsCellValid(x, (int)(y - chunk.Origin.Y), z)) { continue; } v.GridPosition = new Vector3(x, y, z); if (chunk.Data.Water[v.Index].WaterLevel != 0 || y <= waterHeight) { continue; } foreach (FaunaData animal in biomeData.Fauna) { if (y <= 0 || !(PlayState.Random.NextDouble() < animal.SpawnProbability)) { continue; } EntityFactory.CreateEntity <Body>(animal.Name, chunk.Origin + new Vector3(x, y, z) + Vector3.Up * 1.0f); break; } } } }
public void SetFullness(float value) { value = MathFunctions.Clamp(value, 0.0f, 1.0f); int frame = (int)Math.Round(value * 2); if (_frame != frame) { Frame = new Point(frame, 0); var childrenToKill = Children.OfType <SimpleSprite>().ToList(); foreach (var child in childrenToKill) { child.Delete(); } CreateCosmeticChildren(Manager); _frame = frame; } }
public static SoundEffectInstance PlaySound(string name, float volume = 1.0f, float pitch = 0.0f) { if (!HasAudioDevice) { return(null); } // TODO: Remove this block once the SoundManager is initialized in a better location. if (Content == null) { return(null); } SoundEffect effect = null; if (!EffectLibrary.ContainsKey(name)) { effect = Content.Load <SoundEffect>(AssetManager.ResolveContentPath(name)); EffectLibrary[name] = effect; } else { effect = EffectLibrary[name]; if (effect.IsDisposed) { effect = Content.Load <SoundEffect>(AssetManager.ResolveContentPath(name)); EffectLibrary[name] = effect; } } SFXMixer.Levels levels = Mixer.GetOrCreateLevels(name); SoundEffectInstance instance = effect.CreateInstance(); instance.Volume = GameSettings.Current.MasterVolume * GameSettings.Current.SoundEffectVolume * volume * levels.Volume; instance.Pitch = MathFunctions.Clamp(pitch, -1.0f, 1.0f); instance.Play(); instance.Pan = MathFunctions.Rand(-0.25f, 0.25f); instance.Volume = GameSettings.Current.MasterVolume * GameSettings.Current.SoundEffectVolume * volume * levels.Volume; instance.Pitch = MathFunctions.Clamp(pitch, -1.0f, 1.0f); ActiveSounds2D.Add(instance); return(instance); }
private void CreateBackgroundMesh(GraphicsDevice Device, BoundingBox worldBounds) { int resolution = 4; int width = 256; int height = 256; int numVerts = (width * height) / resolution; BackgroundMesh = new VertexBuffer(Device, VertexPositionColor.VertexDeclaration, numVerts, BufferUsage.None); VertexPositionColor[] verts = new VertexPositionColor[numVerts]; Perlin noise = new Perlin(MathFunctions.RandInt(0, 1000)); Vector2 posCenter = new Vector2(width, height) * 0.5f; Vector3 extents = worldBounds.Extents(); float scale = 16; Vector3 offset = new Vector3(extents.X, 0, extents.Z) * 0.5f * scale - new Vector3(worldBounds.Center().X, worldBounds.Min.Y, worldBounds.Center().Z); int i = 0; for (int x = 0; x < width; x += resolution) { for (int y = 0; y < height; y += resolution) { float dist = MathFunctions.Clamp( (new Vector2(x, y) - posCenter).Length() * 0.1f, 0, 4); float landHeight = (noise.Generate(x * 0.01f, y * 0.01f)) * 8.0f * dist; verts[i].Position = new Vector3(((float)x) / width * extents.X * scale, ((int)(landHeight / 10.0f)) * 10.0f, ((float)y) / height * extents.Z * scale) - offset; if (worldBounds.Contains(verts[i].Position) == ContainmentType.Contains) { verts[i].Position = new Vector3(verts[i].Position.X, Math.Min(verts[i].Position.Y, worldBounds.Min.Y), verts[i].Position.Z); } i++; } } BackgroundMesh.SetData(verts); int[] indices = WorldGenerator.SetUpTerrainIndices(width / resolution, height / resolution); BackgroundIndex = new IndexBuffer(Device, typeof(int), indices.Length, BufferUsage.None); BackgroundIndex.SetData(indices); }
/// <summary> /// Gets a unique set of identifiers that were selected on the screen. /// </summary> /// <param name="screenRectangle">The screen rectangle to select from.</param> /// <returns></returns> public IEnumerable <uint> GetIDsSelected(Rectangle screenRectangle) { ValidateBuffer(GameStates.GameState.Game.GraphicsDevice); HashSet <uint> selected = new HashSet <uint>(); lock (ColorBufferMutex) { if (colorBuffer == null) { return(selected); } int width = Buffer.Width; int height = Buffer.Height; int startX = MathFunctions.Clamp(screenRectangle.X / Scale, 0, width - 1); int startY = MathFunctions.Clamp(screenRectangle.Y / Scale, 0, height - 1); int endX = MathFunctions.Clamp(screenRectangle.Right / Scale, 0, width - 1); int endY = MathFunctions.Clamp(screenRectangle.Bottom / Scale, 0, height - 1); for (int x = startX; x <= endX; x++) { for (int y = startY; y <= endY; y++) { uint id = GameComponentExtensions.GlobalIDFromColor(colorBuffer[x + y * width]); if (id == 0) { continue; } selected.Add(id); } } } return(selected); }