package source.Objects {
import flash.geom.Point;
import net.flashpunk.Entity;
import net.flashpunk.FP;
import net.flashpunk.graphics.Image;
import net.flashpunk.graphics.Spritemap;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;
import source.Global;
public class Player extends Physics {
[Embed(source='../../assets/graphics/player.png')]
public var imgPlayer:Class;
public var sprPlayer:Spritemap = new Spritemap(imgPlayer, 32, 32, animEnd);
// normal gravity
public const NORMAL_GRAVITY:Number = 0.4;
public const SLOW_GRAVITY:Number = 0.2;
//how fast we accelerate
public var movement:Number = 1;
public var jump:Number = 8;
//current player direction (true = right, false = left)
public var direction:Boolean = true;
//are we on the ground?
public var onground:Boolean = false;
public var dead:Boolean = false;
public var start:Point;
public var doubleJumpPw:Boolean = false;
public var doubleJump:Boolean = false;
public var jumpFlag:Boolean = false;
public var shootpw:Boolean = false;
public var wallJumppPw:Boolean = false;
public var wallJump:int = 0;
public const tipo:String = "Player";
public function Player(x:int, y:int){
//set position
super(x, y);
start = new Point(x, y);
//set different speeds and such
mGravity = NORMAL_GRAVITY;
mMaxspeed = new Point(4, 8);
mFriction = new Point(0.5, 0.5);
//set up animations
sprPlayer.add("standLeft", [0], 0, false);
sprPlayer.add("standRight", [8], 0, false);
sprPlayer.add("walkLeft", [0, 1, 2, 3, 4, 5, 6, 7], 0.2, true);
sprPlayer.add("walkRight", [8, 9, 10, 11, 12, 13, 14, 15], 0.2, true);
sprPlayer.add("jumpLeft", [2], 0, false);
sprPlayer.add("jumpRight", [10], 0, false);
sprPlayer.play("standRight");
//set hitbox & graphic
setHitbox(16, 24, -8, -8);
graphic = sprPlayer;
type = tipo;
FP.watch("jumpFlag", "shootpw");
}
override public function update():void {
//did we... die?
if (dead){
sprPlayer.alpha -= 0.1;
return;
} else if (sprPlayer.alpha < 1){
sprPlayer.alpha += 0.1
}
//are we on the ground?
onground = false;
if (collide(solid, x, y + 1)){
onground = true;
}
//set acceleration to nothing
acceleration.x = 0;
//increase acceeration
if (Input.check(Global.keyLeft)){
acceleration.x = -movement;
direction = false;
}
if (Input.check(Global.keyRight)){
acceleration.x = movement;
direction = true;
}
//friction
if (!Input.check(Global.keyLeft) && !Input.check(Global.keyRight)){
friction(true, false);
}
//jump
if (wallJump==0 && Input.pressed(Global.keyA) && onground) {
trace("jump");
speed.y = -jump;
jumpFlag = true;
}
//double jump
if (doubleJumpPw == true && Input.pressed(Global.keyA) && !(doubleJump) && !collide("Solid", x, y + 2)) {
trace("double jump");
speed.y = -jump;
doubleJump = true;
jumpFlag = true;
}
if (doubleJump){
if (collide("Solid", x, y + 1)){
doubleJump = false;
jumpFlag = false;
}
}
// wall jump
if (wallJumppPw) {
// sliding control
if (!onground && collide("Solid", x+width+1, y) && !collide("Solid",x,y-1)){
//trace("right slinding");
wallJump = 2;
}
if (!onground && collide("Solid", x - 1, y) && !collide("Solid",x,y-1)){
//trace("left slinding");
wallJump = 1;
}
// apply wall jump
if (!onground && Input.pressed(Global.keyA) && wallJump == 1 && speed.y >= 0) {
trace("i'm on left wall");
speed.y = -jump;
speed.x = 64;
direction = true;
}
if (!onground && Input.pressed(Global.keyA) && wallJump == 2 && speed.y >= 0) {
trace("i'm on right wall");
speed.y = -jump;
speed.x = -64;
direction = false;
}
}
//shoot
if (shootpw){
if (Input.pressed(Global.keyB)){
var bullet:Bullet = new Bullet(x, y + 8);
if (speed.x > 0 || direction){
bullet.x += 36;
}
if (speed.x < 0 || !direction){
bullet.x -= 8;
bullet.movement = -8;
}
FP.world.add(bullet);
}
}
//set the gravity
gravity();
//make sure we're not going too fast
maxspeed(true, true);
//variable jumping (tripple gravity)
if (speed.y < 0 && !Input.check(Global.keyA)){
gravity();
gravity();
}
//set the sprites according to if we're on the ground, and if we are moving or not
if (onground){
if (speed.x < 0){
sprPlayer.play("walkLeft");
}
if (speed.x > 0){
sprPlayer.play("walkRight");
}
if (speed.x == 0){
if (direction){
sprPlayer.play("standRight");
} else {
sprPlayer.play("standLeft");
}
}
jumpFlag = false;
mGravity = NORMAL_GRAVITY;
} else {
if (direction){
sprPlayer.play("jumpRight");
} else {
sprPlayer.play("jumpLeft");
}
}
//set the motion. We set this later so it stops all movement if we should be stopped
motion();
wallJump = 0;
collisionResponse();
}
public function killme():void {
dead = true;
Global.restart = true;
}
private function collisionResponse():void {
//did we just get.. KILLED? D:
if (collide("Spikes", x, y) && speed.y > 0){
//killme!
killme();
}
var ent:Entity = null;
// double jump power up collect
if (!doubleJumpPw){
ent = collide("double", x, y)
if (ent != null){
doubleJumpPw = true;
FP.world.remove(ent);
}
}
// shoot power up collect
if (!shootpw){
ent = collide("shoot", x, y)
if (ent != null){
shootpw = true;
FP.world.remove(ent);
}
}
// shoot power up collect
if (!wallJumppPw){
ent = collide("wallJump", x, y)
if (ent != null){
wallJumppPw = true;
FP.world.remove(ent);
}
}
}
public function animEnd():void {
}
}
}