package entities
{
import entities.Bullets;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.net.LocalConnection;
import net.flashpunk.Entity;
import net.flashpunk.FP;
import net.flashpunk.Sfx;
import net.flashpunk.World;
import net.flashpunk.graphics.Image;
import net.flashpunk.graphics.Text;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;
import worlds.GameWorld;
import worlds.Level;
import worlds.MainMenuWorld;
public class Player extends Entity
{
[Embed( 'assets/jump2.mp3')]
private const JUMPSOUND:Class;
public var jumpsound:Sfx = new Sfx(JUMPSOUND);
[Embed( 'assets/click1.mp3')]
private const CLICKSOUND:Class;
public var clicksound:Sfx = new Sfx(CLICKSOUND);
[Embed( 'assets/hurt.mp3')]
private const HURTSOUND:Class;
public var hurtsound:Sfx = new Sfx(HURTSOUND);
[Embed( 'assets/Concerto.mp3')]
private const CONCERTOSOUND:Class;
public var concertosound:Sfx = new Sfx(CONCERTOSOUND);
[Embed( 'assets/powerup1.mp3')]
private const POWERUPSOUND:Class;
public var powerupsound:Sfx = new Sfx(POWERUPSOUND);
protected var playerImage:Image;
protected const PLAYER_HSPEED:int = 40;
protected var gravity:int = 24; protected var JUMP:int = 700;
protected var a:Point;
protected var v:Point;
protected var bGravityFlipped:Boolean; protected var cGravityFlipped:Boolean; protected var dGravityFlipped:Boolean;
protected var playerHealth:Number = 50;
protected var playerScore:Number = 0;
protected var roundNumber:Number = 0;
protected var hitcount:int = 0;
public function Player()
{
playerImage = new Image(new BitmapData(16, 24));
playerImage.color = 0xf0c826;
graphic = playerImage;
setHitbox(16,24);
a = new Point();
v = new Point();
bGravityFlipped = false; cGravityFlipped = false;
dGravityFlipped = false;
x = 320; y = 440;
}
public override function update():void
{
if (Input.check(Key.M)) {
concertosound.loop();
}
if (collide("ball",x,y)) {
var d:Ball= collide("ball",x,y) as Ball;
if (d) {
hurtsound.play();
graphic = playerImage;
playerHealth = playerHealth - 0.5;
if (playerHealth <= 0 ) {
FP.world = new MainMenuWorld(0, this.playerScore, this.roundNumber);
}
}
}
if(collide("bullet", x, y) && hitcount == -2 || hitcount ==3 && collide("bullet", x, y))
{
powerupsound.play();
var c:Bullets = collide("bullet",x,y) as Bullets;
if (c)
{
this.playerScore = this.playerScore + 10000;
this.roundNumber = this.roundNumber + 1;
FP.world = new Level(this.playerScore, this.playerHealth, this.roundNumber);
}
}
if (collide("bullet", x, y)) {
}
var b:Bullets = collide("bullet", x, y) as Bullets;
if (b && hitcount == -1 || b && hitcount == 2)
{
this.playerScore = this.playerScore + 30;
b.destroy();
}
if (b && hitcount == 0 || b && hitcount == 1)
{
this.playerScore = this.playerScore + 10;
b.destroy();
}
if (hitcount ==-2 && isOnGround && !cGravityFlipped && !dGravityFlipped) {
JUMP = 1000;
}
else if (hitcount ==-1 && isOnGround && !cGravityFlipped && !dGravityFlipped) {
JUMP = 700;
}
else if (hitcount == 3 && isOnGround3 && dGravityFlipped || hitcount == 3 && isOnGround3 && cGravityFlipped ) { JUMP = 1000;
}
else if (hitcount == 2 && isOnGround3 && dGravityFlipped|| hitcount == 2 && isOnGround3 && cGravityFlipped) { JUMP = 700;
}
else if (hitcount == 1 && isOnGround3 && dGravityFlipped || hitcount == 1 && isOnGround3 && cGravityFlipped) {
JUMP = 400;
}
else {
JUMP = 400;
}
var hInput:int = 0;
if (!cGravityFlipped && !bGravityFlipped) { if(Input.check(Key.LEFT)) hInput -= 8; if(Input.check(Key.RIGHT)) hInput += 8; if(Input.pressed(Key.UP)) jump(); if (Input.pressed(Key.W)) flipGravity();
if (Input.pressed(Key.D)) {
v.x = 0;
a.x = 0;
flipToRightLeftGravity();
}
if (Input.pressed(Key.A)) {
v.x = 0;
a.x = 0;
dGravityFlipped = true; flipToRightLeftGravity();
}
}
if (!cGravityFlipped && bGravityFlipped) { if(Input.check(Key.LEFT)) hInput -= 8; if(Input.check(Key.RIGHT)) hInput += 8; if(Input.pressed(Key.DOWN)) jump(); if (Input.pressed(Key.S)) flipGravity();
if (Input.pressed(Key.D)) {
flipToRightFromUp(); }
if (Input.pressed(Key.A)) {
flipToLeftFromUp();
}
}
if (cGravityFlipped && !dGravityFlipped) {
if(Input.check(Key.UP)) hInput -= 8; if(Input.check(Key.DOWN)) hInput += 8; if (Input.check(Key.LEFT)) jumpLeftRight();
if (Input.pressed(Key.A)) {
if (isOnRight) {
v.x=0; a.x = 0;
}
rightToLeftflipGravity();
}
if (Input.pressed(Key.S)) {
if (isOnRight) {
bGravityFlipped = false;
cGravityFlipped = false;
}
}
if (Input.pressed(Key.W)) {
if (isOnRight) {
bGravityFlipped = true;
cGravityFlipped = false;
}
}
}
if (cGravityFlipped && dGravityFlipped) { if(Input.check(Key.UP)) hInput -= 8; if(Input.check(Key.DOWN)) hInput += 8; if (Input.check(Key.RIGHT)) jumpLeftRight();
if (Input.pressed(Key.D)) {
if (isOnLeft) {
v.x = 0; a.x = 0;
}
rightToLeftflipGravity();
}
if (Input.pressed(Key.W)) {
if (isOnLeft) {
bGravityFlipped = true;
cGravityFlipped = false;
dGravityFlipped = false;
}
}
if (Input.pressed(Key.S)) {
v.y = 0;
a.y = 0;
if (isOnLeft) {
bGravityFlipped = false;
cGravityFlipped = false;
dGravityFlipped = false;
}
}
}
if (!cGravityFlipped) {
v.x = PLAYER_HSPEED * hInput;
if(bGravityFlipped) {
a.y = -gravity;
}
else {
a.y =gravity;
}
v.y += a.y;
x += v.x * FP.elapsed;
y += v.y * FP.elapsed;
if (v.y >= 1100 && hitcount == 2 && y + height >= FP.screen.height || y <= 0 && v.y <= -1100 && hitcount == 2 ) {
transform3();
}
if (v.y >= 1100 && y + height >= FP.screen.height && hitcount == 1 || y <= 0 && v.y <= -1100 && hitcount == 1) {
transform2();
}
if (v.y >= 1150 && y + height >= FP.screen.height && hitcount == 0 || y <= 0 && v.y <= -1150 && hitcount == 0 ) {
transform1();
}
if (v.y >= 1100 && y + height >= FP.screen.height && hitcount == -1 || y <= 0 && v.y <= -1100 && hitcount == -1 ) {
transform0();
}
if (v.y >= 1100 && y + height >= FP.screen.height && hitcount == -2 || y <= 0 && v.y <= -1100 && hitcount == -2 ) {
transform00();
}
if(y + height > FP.screen.height)
{
v.y = 0;
y = FP.screen.height - height;
}
if (y<0) {
v.y = 0;
y=0;
}
if(x > FP.screen.width-width)
{
v.x = 0;
x = FP.screen.width-width;
}
else if(x < 0)
{
v.x = 0;
x = 0;
}
}
if (cGravityFlipped) {
v.y = PLAYER_HSPEED * hInput;
if(dGravityFlipped) {
a.x = -gravity;
}
else {
a.x =gravity;
}
v.x += a.x;
x += v.x * FP.elapsed;
y += v.y * FP.elapsed;
if (v.x >= 1174 && x + width >= FP.screen.width && hitcount ==-1 || x <= 0 && v.x <= -1174 && hitcount ==-1) {
transform000();
}
if (v.x >= 1174 && x + width >= FP.screen.width && hitcount ==0 || x <= 0 && v.x <= -1174 && hitcount ==0) {
transform00();
}
if (v.x >= 1174 && x + width >= FP.screen.width && hitcount ==1 || x <= 0 && v.x <= -1174 && hitcount == 1) {
transform0();
}
if (v.x >= 1174 && x + width >= FP.screen.width && hitcount == 2 || x <= 0 && v.x <= -1174 && hitcount ==2) {
transform1();
}
if (v.x >= 1174 && x + width >= FP.screen.width && hitcount == 3 || x <= 0 && v.x <= -1174 && hitcount == 3 ) {
transform2();
}
if(y + height > FP.screen.height)
{
v.y = 0;
y = FP.screen.height - height;
}
if (y<0) {
v.y = 0;
y=0;
}
if(x > FP.screen.width-width)
{
v.x = 0;
x = FP.screen.width-width;
}
else if(x < 0)
{
v.x = 0;
x = 0;
}
}
super.update();
}
protected function jump():void
{
if(isOnGround)
{
jumpsound.play();
if (bGravityFlipped) {
v.y = JUMP;
} else {
v.y = -JUMP;
}
}
}
protected function jumpLeftRight():void
{
if(isOnGround2 || isOnGround3)
{
jumpsound.play();
if (dGravityFlipped) {
v.x = JUMP;
} else {
v.x = -JUMP;
}
}
}
protected function flipGravity():void {
if (isOnGround) {
bGravityFlipped = !bGravityFlipped;
}
}
protected function flipToRightLeftGravity():void {
if (isOnGround2) {
cGravityFlipped = !cGravityFlipped;
}
}
protected function flipToRightFromUp():void {
if (isOnGround) {
bGravityFlipped = !bGravityFlipped;
cGravityFlipped = !cGravityFlipped;
}
}
protected function flipToLeftFromUp():void {
if (isOnGround) {
bGravityFlipped = !bGravityFlipped;
cGravityFlipped = !cGravityFlipped;
dGravityFlipped = !dGravityFlipped;
}
}
protected function rightToLeftflipGravity():void {
if (isOnGround3) {
dGravityFlipped = !dGravityFlipped;
}
}
public function get isOnGround():Boolean {
return (y + height >= FP.screen.height && !bGravityFlipped) || (y <=0 && bGravityFlipped);
}
public function get isOnGround2():Boolean {
return (y + height >= FP.screen.height && !cGravityFlipped) || (x + width >= FP.screen.width && cGravityFlipped) ;
}
public function get isOnGround3():Boolean {
return (x + width >= FP.screen.width && !dGravityFlipped) || (x <= 0 && dGravityFlipped);
}
public function get isOnLeft(): Boolean {
return (x <= 0 && dGravityFlipped);
}
public function get isOnRight(): Boolean {
return (x + width >= FP.screen.width && !dGravityFlipped);
}
protected function transform000():void {
clicksound.play();
playerImage = new Image(new BitmapData(7, 55));
playerImage.color = 0xf0c826;
graphic = playerImage;
setHitbox(7,55);
width = 7;
height = 55;
hitcount = -2;
}
protected function transform00():void {
clicksound.play();
playerImage = new Image(new BitmapData(11, 36));
playerImage.color = 0xf0c826;
graphic = playerImage;
setHitbox(11,36);
width = 11;
height = 36;
hitcount = -1;
}
protected function transform0():void {
clicksound.play();
playerImage = new Image(new BitmapData(16, 24));
playerImage.color = 0xf0c826;
graphic = playerImage;
setHitbox(16,24);
width = 16;
height = 24;
hitcount = 0;
}
protected function transform1():void {
clicksound.play();
playerImage = new Image(new BitmapData(26, 15));
playerImage.color = 0xf0c826;
graphic = playerImage;
setHitbox(26,15);
width = 26;
height = 15;
hitcount = 1;
}
protected function transform2():void {
clicksound.play();
playerImage = new Image(new BitmapData(36, 11));
playerImage.color = 0xf0c826;
graphic = playerImage;
setHitbox(36,11);
width = 36;
height = 11;
hitcount = 2;
}
protected function transform3():void {
clicksound.play();
playerImage = new Image(new BitmapData(55, 7));
playerImage.color = 0xf0c826;
graphic = playerImage;
setHitbox(55,7);
width = 55;
height = 7;
hitcount = 3;
}
public function getScore():Number {
return this.playerScore;
}
public function setScore(e:Number): void {
this.playerScore = e;
}
public function getHealth():Number {
return this.playerHealth;
}
public function setHealth(e:Number): void {
this.playerHealth = e;
}
public function getRound():Number {
return this.roundNumber;
}
public function setRound(e:Number): void {
this.roundNumber = e;
}
}
}