-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhysStudent.as
92 lines (83 loc) · 1.83 KB
/
PhysStudent.as
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
package{
import org.flixel.*;
public class PhysStudent extends FlxSprite {
[Embed(source="assets/phys.png")] private var sprite:Class;
private var _target:FlxObject;
private var _bolts:FlxGroup;
private var _shock:Boolean;
public function PhysStudent(X:Number,Y:Number,bolts:FlxGroup,target:FlxObject=null){
super(X,Y);
_bolts = bolts;
loadGraphic(sprite,true,true,28,43);
addAnimation('run',[0,1,2,3],10);
addAnimation('shock',[4,5,6,7],10);
play('shock');
width = 16;
height = 32;
offset.x = 2;
offset.y = 4;
//Max speeds
maxVelocity.x = 200;
maxVelocity.y = 200;
//Gravity
acceleration.y = 400;
//acceleration.x = 100;
//Friction
drag.x = 300;
//Initial right facing
facing = RIGHT;
//Allow collisions
solid = true;
health=1;
_target = target;
}
override public function update():void{
if (_shock) return;
if (_target == null || !_target.alive || (_target.flickering && onScreen() )){
facing = LEFT;
velocity.x = -150;
play('run');
}
else if (onScreen() && Math.abs(_target.x - x) < 100){
play('run');
if (Math.abs(_target.x - x) < 30){
addBolt();
_shock = true;
velocity.x=0;
play('shock');
(new FlxTimer()).start(1,1,function(T:FlxTimer):void{_shock = false;});
return;
}
if (_target.x < x){
facing = LEFT;
velocity.x = -150;
}
else if (_target.x > x){
facing = RIGHT;
velocity.x = 150;
}
if (_target.y < y && touching&DOWN){
velocity.y = -100;
}
}
else{
play('shock');
}
}
private function addBolt():void{
var bx:Number;
var by:Number;
by = y;
var b:Bolt = new Bolt(0,0);
if (facing == LEFT){
bx = x - b.width;
}
else {
bx = x + width;
}
b.x=bx;b.y=by;
b.facing=facing;
_bolts.add(b);
}
}
}