You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package ent;
  2. import Data.ObjectKind;
  3. class Object extends Entity {
  4. var speed = 0.2;
  5. var angle = 0.;
  6. var wasCarried = false;
  7. var color : h3d.shader.ColorAdd;
  8. var pulse : Float = 0.;
  9. var hintAct : h2d.Anim;
  10. public var active : Bool;
  11. public var carried(default, set) : Bool = false;
  12. public function new(k, x, y) {
  13. super(k, x, y);
  14. switch( kind ) {
  15. case Square1, Square2, Square3, Wings:
  16. var a = new h2d.Anim([for( i in 0...9 ) game.tiles.sub(i * 32, 256 + (kind == Wings ? 64 : kind == Square2 ? 32 : 0), 32, 32, -16, -16)], 20, spr);
  17. a.loop = false;
  18. a.onAnimEnd = function() {
  19. haxe.Timer.delay(function() {
  20. a.currentFrame = 0;
  21. }, 200 + Std.random(400));
  22. };
  23. if( kind == Square3 )
  24. a.adjustColor({ hue : Math.PI / 2 });
  25. hintAct = a;
  26. case Plate1, Plate2, Plate3, Plate4:
  27. game.soilLayer.add(Std.int(x) * 32, Std.int(y) * 32, game.tiles.sub(64, 32, 32, 32));
  28. spr.alpha = 0.8;
  29. case Hero:
  30. game.soilLayer.add(Std.int(x) * 32, Std.int(y) * 32, game.tiles.sub(0, 96, 32, 32));
  31. spr.alpha = 1;
  32. default:
  33. }
  34. }
  35. function set_carried(b) {
  36. var ix = Std.int(x);
  37. var iy = Std.int(y);
  38. if( b )
  39. active = false;
  40. wasCarried = carried;
  41. game.world.add(spr, b ? Game.LAYER_CARRY : Game.LAYER_ENT);
  42. return carried = b;
  43. }
  44. override function isCollide( with : ent.Entity ) {
  45. return with != null && with.kind != Hero;
  46. }
  47. override function canPick() {
  48. if( hasFlag(Under) )
  49. return false;
  50. if( carried )
  51. return false;
  52. return true;
  53. }
  54. override function getAnim() {
  55. return switch( kind ) {
  56. case Exit:
  57. [for( i in 0...6 ) game.tiles.sub(i * 32, 160, 32, 32, -16, -16)];
  58. default:
  59. super.getAnim();
  60. }
  61. }
  62. override public function update(dt:Float) {
  63. if( hintAct != null )
  64. hintAct.visible = !active;
  65. else if( active ) {
  66. pulse += dt * 0.1;
  67. spr.adjustColor({ saturation : Math.abs(Math.sin(pulse)) * 0.5, lightness : Math.abs(Math.sin(pulse)) * 0.2 });
  68. } else if( pulse != 0 ) {
  69. pulse %= Math.PI;
  70. pulse += dt * 0.1;
  71. if( pulse > Math.PI )
  72. pulse = 0;
  73. spr.adjustColor({ saturation : Math.abs(Math.sin(pulse)) * 0.5, lightness : Math.abs(Math.sin(pulse)) * 0.2 });
  74. }
  75. if( spr.scaleX < 1 ) {
  76. spr.scale(Math.pow(1.05, dt));
  77. if( spr.scaleX > 1 ) {
  78. spr.setScale(1);
  79. spr.smooth = false;
  80. }
  81. }
  82. var ix = Std.int(x), iy = Std.int(y);
  83. switch( kind ) {
  84. case Exit:
  85. if( game.allActive ) {
  86. spr.speed = 15;
  87. } else {
  88. spr.speed = 0;
  89. spr.currentFrame = 0;
  90. }
  91. case Square1:
  92. active = getObj(ix, iy, [Plate1, Plate2][game.hueValue], [CanPutOver]) != null;
  93. case Square2:
  94. active = getObj(ix, iy, [Plate2, Plate1][game.hueValue], [CanPutOver]) != null;
  95. case Square3:
  96. if( game.hueValue == 0 )
  97. active = getObj(ix, iy, Plate3, [CanPutOver]) != null || getObj(ix, iy, Steal, [CanPutOver]) != null;
  98. else
  99. active = getObj(ix, iy, Plate4, [CanPutOver]) != null;
  100. case Wings:
  101. var obj = getObj(ix, iy, [CanPutOver]);
  102. active = obj != null && obj.kind != Steal;
  103. default:
  104. }
  105. if( wasCarried ) {
  106. var tx = x * 32, ty = y * 32;
  107. var d = hxd.Math.distance(tx - spr.x, ty - spr.y);
  108. if( d > 1 ) {
  109. spr.x = hxd.Math.lerp(spr.x, tx, 1 - Math.pow(0.7, dt));
  110. spr.y = hxd.Math.lerp(spr.y, ty, 1 - Math.pow(0.7, dt));
  111. return;
  112. }
  113. wasCarried = false;
  114. }
  115. super.update(dt);
  116. }
  117. }