-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.hx
109 lines (85 loc) · 2.32 KB
/
Enemy.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package ;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.text.FlxText;
import flixel.tweens.FlxTween;
import flixel.util.FlxColor;
/**
* ...
* @author ...
*/
class Enemy extends FlxSprite
{
private var _sticky:Bool;
private var _fromLeft:Bool;
private var _flipped:Bool = false;
private var _speed:Float = 32;
private var _gravity:Float = 500;
public var onFloor:Bool = false;
public var pointValue:Int = 10;
private var _txtScore:FlxText;
public function new(X:Float=0, Y:Float=0, OnFloor:Bool = true, ?FromLeft:Bool = true, ?Sticky:Bool = false)
{
super(X, Y);
_sticky = Sticky;
_fromLeft = FromLeft;
onFloor = OnFloor;
_flipped = !onFloor;
pointValue = (!_sticky) ? 20 : pointValue;
//Load graphics
if (_sticky) {
loadGraphic(AssetPaths.enemy__png, true, 32, 18);
}
else{
loadGraphic(AssetPaths.hardenemy__png, true, 32, 22);
}
//Init animations
setFacingFlip(FlxObject.LEFT, false, false);
setFacingFlip(FlxObject.RIGHT, true, false);
animation.add("walk", [0, 1], 10, true);
facing = (_fromLeft) ? FlxObject.RIGHT : FlxObject.LEFT;
flipY = _flipped;
//Adjust hitbox
if(_sticky){
setSize(28, 18);
offset.x = 2;
}
else {
setSize(24, 22);
offset.x = 4;
}
//Set position
this.x = (_fromLeft) ? 0 : FlxG.width-PlayState.TILESIZE;
this.y = (onFloor) ? FlxG.height - PlayState.TILESIZE * 2 - height : PlayState.TILESIZE*2;
//Set gravity if applicable
_gravity = (!_sticky && onFloor) ? _gravity : -_gravity;
acceleration.y = (!_sticky) ? _gravity : 0;
//Start movement and animation
velocity.x = (_fromLeft) ? _speed : -_speed;
animation.play("walk");
}
override public function update():Void {
super.update();
//Turn around when hitting a wall
if ( (velocity.x > 0 && this.x > FlxG.width - PlayState.TILESIZE-this.width-1) || (velocity.x < 0 && this.x < PlayState.TILESIZE+1) ){
velocity.x *= -1;
facing = (facing == FlxObject.LEFT) ? FlxObject.RIGHT : FlxObject.LEFT;
flipY = _flipped;
}
//Handle animations
switch(facing) {
case FlxObject.LEFT, FlxObject.RIGHT:
animation.play("walk");
}
}
public function flipGravity():Void {
if (_sticky) {
return;
}
_gravity *= -1;
acceleration.y = _gravity;
_flipped = !_flipped;
flipY = !flipY;
}
}