ManaLamp.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using Microsoft.Xna.Framework;
  2. using System.Collections.Generic;
  3. using DwarfCorp;
  4. namespace ManaLampMod
  5. {
  6. public class ManaLamp : CraftedBody
  7. {
  8. [EntityFactory("Mana Lamp")]
  9. private static DwarfCorp.GameComponent __factory(ComponentManager Manager, Vector3 Position, Blackboard Data)
  10. {
  11. return new ManaLamp(Manager, Position, Data.GetData<List<ResourceAmount>>("Resources", null));
  12. }
  13. public ManaLamp()
  14. {
  15. }
  16. public ManaLamp(ComponentManager Manager, Vector3 position, List<ResourceAmount> Resources) :
  17. base(Manager, "Lamp", Matrix.CreateTranslation(position), new Vector3(1.0f, 1.0f, 1.0f), Vector3.Zero, new CraftDetails(Manager, "Mana Lamp", Resources))
  18. {
  19. Tags.Add("Lamp");
  20. CollisionType = CollisionType.Static;
  21. CreateCosmeticChildren(Manager);
  22. }
  23. public override void CreateCosmeticChildren(ComponentManager Manager)
  24. {
  25. base.CreateCosmeticChildren(Manager);
  26. var spriteSheet = new SpriteSheet("mana-lamp", 32);
  27. List<Point> frames = new List<Point>
  28. {
  29. new Point(0, 0),
  30. new Point(2, 0),
  31. new Point(1, 0),
  32. new Point(2, 0)
  33. };
  34. var lampAnimation = AnimationLibrary.CreateAnimation(spriteSheet, frames, "ManaLampAnimation");
  35. lampAnimation.Loops = true;
  36. var sprite = AddChild(new AnimatedSprite(Manager, "sprite", Matrix.Identity)
  37. {
  38. LightsWithVoxels = false,
  39. OrientationType = AnimatedSprite.OrientMode.YAxis,
  40. }) as AnimatedSprite;
  41. sprite.AddAnimation(lampAnimation);
  42. sprite.AnimPlayer.Play(lampAnimation);
  43. sprite.SetFlag(Flag.ShouldSerialize, false);
  44. // This is a hack to make the animation update at least once even when the object is created inactive by the craftbuilder.
  45. sprite.AnimPlayer.Update(new DwarfTime(), false);
  46. AddChild(new LightEmitter(Manager, "light", Matrix.Identity, new Vector3(0.1f, 0.1f, 0.1f), Vector3.Zero, 255, 8)
  47. {
  48. HasMoved = true
  49. }).SetFlag(Flag.ShouldSerialize, false);
  50. AddChild(new GenericVoxelListener(Manager, Matrix.Identity, new Vector3(0.5f, 0.5f, 0.5f), new Vector3(0.0f, -1.0f, 0.0f), (changeEvent) =>
  51. {
  52. if (changeEvent.Type == VoxelChangeEventType.VoxelTypeChanged && changeEvent.NewVoxelType == 0)
  53. Die();
  54. })).SetFlag(Flag.ShouldSerialize, false);
  55. }
  56. }
  57. }