private const int rumbleCount = 50; // Average the last X block positions to get the position of the rumble public void Update(float dt) { intervalTimer += dt; Entity targetEntity = this.Target.Value.Target; if (targetEntity != null && targetEntity.Active && this.Index < this.Coords.Length) { EffectBlockFactory factory = Factory.Get <EffectBlockFactory>(); Voxel m = targetEntity.Get <Voxel>(); float interval = 0.03f * this.IntervalMultiplier; while (intervalTimer > interval && this.Index < this.Coords.Length) { CoordinateEntry entry = this.Coords[this.Index]; Entity blockEntity = factory.CreateAndBind(main); EffectBlock effectBlock = blockEntity.Get <EffectBlock>(); entry.Coord.Data.ApplyToEffectBlock(blockEntity.Get <ModelInstance>()); effectBlock.CheckAdjacent = false; effectBlock.Offset.Value = m.GetRelativePosition(entry.Coord); effectBlock.DoScale = true; effectBlock.StartPosition = entry.Position + new Vector3(8.0f, 20.0f, 8.0f) * this.BlockLifetime.Value; effectBlock.StartOrientation = Quaternion.CreateFromYawPitchRoll(0.15f * this.Index, 0.15f * this.Index, 0); effectBlock.TotalLifetime = this.BlockLifetime; effectBlock.Setup(targetEntity, entry.Coord, entry.Coord.Data.ID); main.Add(blockEntity); this.rumbleSum += entry.Position; int lastIndex = this.Index - rumbleCount; if (lastIndex >= 0) { this.rumbleSum -= this.Coords[lastIndex].Position; } this.Index.Value++; intervalTimer -= interval; } if (this.Coords.Count > 200) { this.RumblePosition.Value = this.rumbleSum / Math.Min(this.Index + 1, rumbleCount); if (!this.rumbling) { AkSoundEngine.PostEvent(AK.EVENTS.PLAY_BLOCK_RUMBLE, this.Entity); this.rumbling = true; } } } else { this.Delete.Execute(); } }
public void Update(float dt) { intervalTimer += dt; Entity targetEntity = this.Target.Value.Target; if (targetEntity != null && targetEntity.Active && this.Index < this.Coords.Length) { float interval = 0.03f * this.IntervalMultiplier; while (intervalTimer > interval && this.Index < this.Coords.Length) { EffectBlockFactory factory = Factory.Get <EffectBlockFactory>(); Voxel m = targetEntity.Get <Voxel>(); CoordinateEntry entry = this.Coords[this.Index]; Entity blockEntity = factory.CreateAndBind(main); EffectBlock effectBlock = blockEntity.Get <EffectBlock>(); entry.Coord.Data.ApplyToEffectBlock(blockEntity.Get <ModelInstance>()); effectBlock.CheckAdjacent = false; effectBlock.Offset.Value = m.GetRelativePosition(entry.Coord); effectBlock.DoScale = true; effectBlock.StartPosition = entry.Position + new Vector3(8.0f, 20.0f, 8.0f) * this.BlockLifetime.Value; effectBlock.StartOrientation = Quaternion.CreateFromYawPitchRoll(0.15f * this.Index, 0.15f * this.Index, 0); effectBlock.TotalLifetime = this.BlockLifetime; effectBlock.Setup(targetEntity, entry.Coord, entry.Coord.Data.ID); main.Add(blockEntity); this.Index.Value++; intervalTimer -= interval; } } else { this.Delete.Execute(); } }
public static void Consolidate(Main main, DynamicVoxel voxel, Voxel targetVoxel, Voxel.Coord targetCoord, float interval = 1.0f) { if (targetVoxel != null) { // Combine this map with the other one Direction x = targetVoxel.GetRelativeDirection(voxel.GetAbsoluteVector(Vector3.Right)); Direction y = targetVoxel.GetRelativeDirection(voxel.GetAbsoluteVector(Vector3.Up)); Direction z = targetVoxel.GetRelativeDirection(voxel.GetAbsoluteVector(Vector3.Backward)); if (x.IsParallel(y)) { x = y.Cross(z); } else if (y.IsParallel(z)) { y = x.Cross(z); } Voxel.Coord offset = new Voxel.Coord(); float closestCoordDistance = float.MaxValue; Vector3 closestCoordPosition = targetVoxel.GetAbsolutePosition(targetCoord); foreach (Voxel.Coord c in voxel.Chunks.SelectMany(c => c.Boxes).SelectMany(b => b.GetCoords())) { float distance = (voxel.GetAbsolutePosition(c) - closestCoordPosition).LengthSquared(); if (distance < closestCoordDistance) { closestCoordDistance = distance; offset = c; } } Vector3 toLevitatingMap = voxel.Transform.Value.Translation - targetVoxel.GetAbsolutePosition(targetCoord); offset = offset.Move(voxel.GetRelativeDirection(-toLevitatingMap)); Quaternion orientation = Quaternion.CreateFromRotationMatrix(voxel.Transform.Value); EffectBlockFactory blockFactory = Factory.Get <EffectBlockFactory>(); int index = 0; List <Voxel.Coord> coords = voxel.Chunks.SelectMany(c => c.Boxes).SelectMany(b => b.GetCoords()).ToList(); Voxel.Coord camera = voxel.GetCoordinate(main.Camera.Position); foreach (Voxel.Coord c in coords.OrderBy(c2 => new Vector3(c2.X - camera.X, c2.Y - camera.Y, c2.Z - camera.Z).LengthSquared())) { Voxel.Coord offsetFromCenter = c.Move(-offset.X, -offset.Y, -offset.Z); Voxel.Coord targetCoord2 = new Voxel.Coord(); targetCoord2.SetComponent(x, offsetFromCenter.GetComponent(Direction.PositiveX)); targetCoord2.SetComponent(y, offsetFromCenter.GetComponent(Direction.PositiveY)); targetCoord2.SetComponent(z, offsetFromCenter.GetComponent(Direction.PositiveZ)); targetCoord2 = targetCoord2.Move(targetCoord.X, targetCoord.Y, targetCoord.Z); if (targetVoxel[targetCoord2].ID == 0) { Entity blockEntity = blockFactory.CreateAndBind(main); c.Data.ApplyToEffectBlock(blockEntity.Get <ModelInstance>()); EffectBlock effectBlock = blockEntity.Get <EffectBlock>(); effectBlock.Offset.Value = targetVoxel.GetRelativePosition(targetCoord2); effectBlock.DoScale = false; effectBlock.StartPosition = voxel.GetAbsolutePosition(c); effectBlock.StartOrientation = orientation; effectBlock.TotalLifetime = (0.05f + (index * 0.0075f)) * interval; effectBlock.Setup(targetVoxel.Entity, targetCoord2, c.Data.ID); main.Add(blockEntity); index++; } } // Delete the map voxel.Entity.Delete.Execute(); } }
public void Update(float dt) { if (this.TimeUntilRebuild > 0) { if (this.TargetVoxel.Value.Target == null || !this.TargetVoxel.Value.Target.Active) { this.Delete.Execute(); return; } float newValue = Math.Max(0.0f, this.TimeUntilRebuild.Value - dt); this.TimeUntilRebuild.Value = newValue; if (newValue == 0.0f) { // Rebuild Entity targetMap = this.TargetVoxel.Value.Target; Voxel m = targetMap.Get <Voxel>(); EffectBlockFactory factory = Factory.Get <EffectBlockFactory>(); int startIndex = this.Base.BaseBoxes.Sum(x => x.Volume); int index = startIndex; foreach (Entity.Handle e in this.DynamicVoxels) { Entity dynamicMap = e.Target; Voxel dynamicMapComponent = dynamicMap != null && dynamicMap.Active ? dynamicMap.Get <Voxel>() : null; if (dynamicMap == null || !dynamicMap.Active) { continue; } Quaternion orientation = Quaternion.CreateFromRotationMatrix(dynamicMapComponent.Transform.Value); List <Voxel.Coord> coords = new List <Voxel.Coord>(); foreach (Voxel.Coord c in dynamicMapComponent.Chunks.SelectMany(x => x.Boxes).SelectMany(x => x.GetCoords())) { if (m[c].ID == 0) { coords.Add(c); } } foreach (Voxel.Coord c in coords.OrderBy(x => (new Vector3(x.X, x.Y, x.Z) - this.Base.Position).LengthSquared())) { Entity blockEntity = factory.CreateAndBind(main); c.Data.ApplyToEffectBlock(blockEntity.Get <ModelInstance>()); EffectBlock effectBlock = blockEntity.Get <EffectBlock>(); effectBlock.Offset.Value = m.GetRelativePosition(c); effectBlock.DoScale = dynamicMapComponent == null; if (dynamicMapComponent != null && dynamicMapComponent[c].ID == c.Data.ID) { effectBlock.StartPosition = dynamicMapComponent.GetAbsolutePosition(c); effectBlock.StartOrientation = orientation; } else { effectBlock.StartPosition = m.GetAbsolutePosition(c) + new Vector3(0.25f, 0.5f, 0.25f) * index; effectBlock.StartOrientation = Quaternion.CreateFromYawPitchRoll(0.15f * index, 0.15f * index, 0); } effectBlock.TotalLifetime = 0.05f + (index * rebuildTimeMultiplier * RebuildTime); effectBlock.Setup(targetMap, c, c.Data.ID); main.Add(blockEntity); index++; } dynamicMap.Delete.Execute(); } this.DynamicVoxels.Clear(); if (index > startIndex) // We built some stuff. Build the base. { int baseIndex = 0; foreach (Voxel.Coord c in this.Base.BaseBoxes.SelectMany(x => x.GetCoords())) { if (m[c].ID == 0) { Entity blockEntity = factory.CreateAndBind(main); EffectBlock effectBlock = blockEntity.Get <EffectBlock>(); c.Data.ApplyToEffectBlock(blockEntity.Get <ModelInstance>()); effectBlock.Offset.Value = m.GetRelativePosition(c); effectBlock.StartPosition = m.GetAbsolutePosition(c) + new Vector3(0.25f, 0.5f, 0.25f) * baseIndex; effectBlock.StartOrientation = Quaternion.CreateFromYawPitchRoll(0.15f * baseIndex, 0.15f * baseIndex, 0); effectBlock.TotalLifetime = 0.05f + (baseIndex * rebuildTimeMultiplier * RebuildTime); effectBlock.Setup(targetMap, c, c.Data.ID); main.Add(blockEntity); baseIndex++; } } this.TimeUntilRebuildComplete.Value = 0.05f + (index * rebuildTimeMultiplier * RebuildTime); } else // We didn't build anything. Delete ourselves. { this.Delete.Execute(); } } } else if (this.TimeUntilRebuildComplete > 0) { // Rebuilding float newValue = Math.Max(0.0f, this.TimeUntilRebuildComplete.Value - dt); this.TimeUntilRebuildComplete.Value = newValue; if (newValue == 0.0f) { // Done rebuilding if (!this.Base.IsValid) { this.Delete.Execute(); } else { this.Base.EnableCellEmptyBinding = !main.EditorEnabled; if (this.IsTriggered) { fall(); } } } } }