From 42b77b95cd695ba33a16826ef1a5287fef2f8eb4 Mon Sep 17 00:00:00 2001 From: Naveen Radhakrishnan Date: Sat, 6 Jun 2020 14:03:53 +0000 Subject: [PATCH] Initial checkin --- src/ent/Entity.hx | 81 ++++++++++++++++++++++++++++ src/ent/Object.hx | 132 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 src/ent/Entity.hx create mode 100644 src/ent/Object.hx diff --git a/src/ent/Entity.hx b/src/ent/Entity.hx new file mode 100644 index 0000000..10f93ad --- /dev/null +++ b/src/ent/Entity.hx @@ -0,0 +1,81 @@ +package ent; + +class Entity { + + var game : Game; + public var inf : Data.Object; + public var kind : Data.ObjectKind; + public var x : Float; + public var y : Float; + public var spr : h2d.Anim; + + public function new( kind, x : Int, y : Int ) { + game = Game.inst; + this.kind = kind; + inf = Data.object.get(kind); + this.x = x + 0.5; + this.y = y + 0.5; + spr = new h2d.Anim(getAnim(), 15); + game.world.add(spr, hasFlag(Under) ? Game.LAYER_ENT_UNDER : Game.LAYER_ENT); + game.entities.push(this); + } + + public function hasFlag(f) { + return inf.flags.has(f); + } + + public function isOccupied() { + if( getObj(Std.int(x), Std.int(y)) != null ) + return true; + return false; + } + + public function isCollide( with : ent.Entity ) { + return true; + } + + function getObj( x : Int, y : Int, ?k : Data.ObjectKind, ?flags : Array ) { + for( e in game.entities ) { + var o = Std.downcast(e, Object); + if( o == null || o.carried || o == this ) continue; + if( Std.int(o.x) != x || Std.int(o.y) != y ) continue; + if( flags != null ) { + var ok = true; + for( f in flags ) + if( !o.inf.flags.has(f) ) { + ok = false; + break; + } + if( !ok ) continue; + } + if( k == null || o.kind == k ) + return o; + } + return null; + } + + public function canPick() { + return false; + } + + public function remove() { + spr.remove(); + game.entities.remove(this); + } + + function getAnim() { + return [game.tiles.sub(inf.image.x * 32, inf.image.y * 32, 32, 32, -16, -16)]; + } + + public function update( dt : Float ) { + spr.x = Std.int(x * 64) / 2; + spr.y = Std.int(y * 64) / 2; + } + + + function toString() { + return kind + "(" + Std.int(x) + "," + Std.int(y) + ")"; + } + + +} \ No newline at end of file diff --git a/src/ent/Object.hx b/src/ent/Object.hx new file mode 100644 index 0000000..7d91604 --- /dev/null +++ b/src/ent/Object.hx @@ -0,0 +1,132 @@ +package ent; +import Data.ObjectKind; + +class Object extends Entity { + + var speed = 0.2; + var angle = 0.; + var wasCarried = false; + var color : h3d.shader.ColorAdd; + var pulse : Float = 0.; + var hintAct : h2d.Anim; + public var active : Bool; + public var carried(default, set) : Bool = false; + + public function new(k, x, y) { + super(k, x, y); + switch( kind ) { + case Square1, Square2, Square3, Wings: + 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); + a.loop = false; + a.onAnimEnd = function() { + haxe.Timer.delay(function() { + a.currentFrame = 0; + }, 200 + Std.random(400)); + }; + if( kind == Square3 ) + a.adjustColor({ hue : Math.PI / 2 }); + hintAct = a; + case Plate1, Plate2, Plate3, Plate4: + game.soilLayer.add(Std.int(x) * 32, Std.int(y) * 32, game.tiles.sub(64, 32, 32, 32)); + spr.alpha = 0.8; + case Hero: + game.soilLayer.add(Std.int(x) * 32, Std.int(y) * 32, game.tiles.sub(0, 96, 32, 32)); + spr.alpha = 1; + default: + } + } + + function set_carried(b) { + var ix = Std.int(x); + var iy = Std.int(y); + if( b ) + active = false; + wasCarried = carried; + game.world.add(spr, b ? Game.LAYER_CARRY : Game.LAYER_ENT); + return carried = b; + } + + override function isCollide( with : ent.Entity ) { + return with != null && with.kind != Hero; + } + + override function canPick() { + if( hasFlag(Under) ) + return false; + if( carried ) + return false; + return true; + } + + override function getAnim() { + return switch( kind ) { + case Exit: + [for( i in 0...6 ) game.tiles.sub(i * 32, 160, 32, 32, -16, -16)]; + default: + super.getAnim(); + } + } + + override public function update(dt:Float) { + + if( hintAct != null ) + hintAct.visible = !active; + else if( active ) { + pulse += dt * 0.1; + spr.adjustColor({ saturation : Math.abs(Math.sin(pulse)) * 0.5, lightness : Math.abs(Math.sin(pulse)) * 0.2 }); + } else if( pulse != 0 ) { + pulse %= Math.PI; + pulse += dt * 0.1; + if( pulse > Math.PI ) + pulse = 0; + spr.adjustColor({ saturation : Math.abs(Math.sin(pulse)) * 0.5, lightness : Math.abs(Math.sin(pulse)) * 0.2 }); + } + + if( spr.scaleX < 1 ) { + spr.scale(Math.pow(1.05, dt)); + if( spr.scaleX > 1 ) { + spr.setScale(1); + spr.smooth = false; + } + } + + var ix = Std.int(x), iy = Std.int(y); + switch( kind ) { + case Exit: + if( game.allActive ) { + spr.speed = 15; + } else { + spr.speed = 0; + spr.currentFrame = 0; + } + case Square1: + active = getObj(ix, iy, [Plate1, Plate2][game.hueValue], [CanPutOver]) != null; + case Square2: + active = getObj(ix, iy, [Plate2, Plate1][game.hueValue], [CanPutOver]) != null; + case Square3: + if( game.hueValue == 0 ) + active = getObj(ix, iy, Plate3, [CanPutOver]) != null || getObj(ix, iy, Steal, [CanPutOver]) != null; + else + active = getObj(ix, iy, Plate4, [CanPutOver]) != null; + case Wings: + var obj = getObj(ix, iy, [CanPutOver]); + active = obj != null && obj.kind != Steal; + default: + } + + if( wasCarried ) { + var tx = x * 32, ty = y * 32; + var d = hxd.Math.distance(tx - spr.x, ty - spr.y); + if( d > 1 ) { + spr.x = hxd.Math.lerp(spr.x, tx, 1 - Math.pow(0.7, dt)); + spr.y = hxd.Math.lerp(spr.y, ty, 1 - Math.pow(0.7, dt)); + return; + } + wasCarried = false; + } + + super.update(dt); + + } + +} \ No newline at end of file