Files

484 lines
16 KiB
ActionScript

package com.arc.dh.state
{
import Playtomic.Log;
import com.arc.dh.entities.Player;
import com.arc.dh.menu.GameOver;
import com.arc.dh.objects.*;
import com.arc.dh.objects.enemies.Seed;
import com.arc.dh.util.AchievementSystem;
import com.arc.dh.util.Config;
import com.arc.dh.util.Data;
import com.arc.dh.util.DataSerializer;
import com.arc.dh.util.Sound;
import com.arc.dh.util.SoundSystem;
import com.arc.flixel.ArcParticleSystem;
import com.arc.flixel.ArcU;
import flash.display.BitmapData;
import org.flixel.FlxG;
import org.flixel.FlxObject;
import org.flixel.FlxPoint;
import org.flixel.FlxTilemap;
import org.flixel.system.FlxTile;
public class RunMode extends HorizontalMode
{
protected static const ESCAPE_SPRITE:Class = RunMode_ESCAPE_SPRITE;
protected static const ESCAPE_OVERLAY_SPRITE:Class = RunMode_ESCAPE_OVERLAY_SPRITE;
protected static const LAVA_SPRITE:Class = RunMode_LAVA_SPRITE;
private static const RUN_WIDTH:int = 4000;
private static const RUN_HEIGHT:int = 20;
private static const PLAYER_OFFSET:int = 60;
private var _gameOver:GameOver;
public function RunMode()
{
super();
mode = RUN_MODE;
_ch = ArcU.rand(1,7);
}
override public function create() : void
{
super.create();
Log.CustomMetric("RunStart","Modes");
bg1.vertical = bg2.vertical = bg3.vertical = false;
camera = new Camera(FlxG.width / 2,FlxG.height / 2);
camera.velocity.x = 100;
this.add(camera);
controlCharacter = false;
FlxG.camera.follow(camera);
FlxG.camera.update();
FlxG.worldBounds.width = pixels.width * Config.BLOCK_SIZE;
FlxG.worldBounds.height = pixels.height * Config.BLOCK_SIZE;
spawnChance = 100;
SoundSystem.music(Sound.PLAY_MUSIC2);
}
override protected function getStartX() : int
{
return 36;
}
override protected function getStartY() : int
{
return 48;
}
override public function update() : void
{
if(_complete)
{
this._gameOver.update();
showAchievement();
return;
}
super.update();
if(inMenu)
{
return;
}
if(camera.velocity.x < 150)
{
camera.velocity.x += FlxG.elapsed * 6;
}
if(camera.velocity.x < 200)
{
camera.velocity.x += FlxG.elapsed * 3;
}
else if(camera.velocity.x < 300)
{
camera.velocity.x += FlxG.elapsed * 2;
}
else
{
camera.velocity.x += FlxG.elapsed;
}
if(camera.velocity.x > 500)
{
camera.velocity.x = 500;
}
if(ArcU.rand(1,100) < 5)
{
}
if(player.x < FlxG.camera.scroll.x + PLAYER_OFFSET - 2)
{
player.velocity.x = camera.velocity.x * 1.2;
}
else if(player.x > FlxG.camera.scroll.x + PLAYER_OFFSET + 2)
{
player.velocity.x = camera.velocity.x * 0.9;
}
else
{
player.velocity.x = camera.velocity.x;
player.x = FlxG.camera.scroll.x + PLAYER_OFFSET;
}
if(player.getScreenXY().x < -player.width)
{
Data.hp = 0;
Data.state.die();
}
}
override protected function createLevel() : void
{
tilemap = this.buildLevel();
tilemap.follow();
}
override public function die() : void
{
_complete = true;
inMenu = false;
SoundSystem.play(Sound.WIN);
this._gameOver = new GameOver(cdiamonds.toString(),this.caveHeight.toString() + "M",duration).setCallback(function():void
{
FlxG.mouse.hide();
FlxG.switchState(new RunMode());
},function():void
{
FlxG.mouse.hide();
FlxG.switchState(new MainMenu(0));
});
++Data.deaths;
if(this.caveHeight > Data.maxRunDistance)
{
Data.maxRunDistance = this.caveHeight;
}
DataSerializer.save();
Data.kong();
Newgrounds.medals();
}
override public function reset() : void
{
}
override public function restart() : void
{
var _loc2_:StoryDialog = null;
var _loc1_:Array = checkpoints[checkpoints.length - 1];
player.reset(_loc1_[0] * Config.BLOCK_SIZE + 4,_loc1_[1] * Config.BLOCK_SIZE + 12);
FlxG.camera.focusOn(new FlxPoint(player.x,player.y));
FlxG.camera.follow(player);
Data.hp = Data.hpm;
boss = 0;
spawner = new Object();
pixels = mpixels.clone();
for each(_loc2_ in messages)
{
_loc2_.load();
}
bullets.clear();
enemies.clear();
penemies.clear();
diamonds.clear();
powerups.clear();
damagetexts.clear();
healthbars.clear();
objects.clear();
cobjects.clear();
spawnTop = this.calcTop(Config.SPAWN_PADDING);
spawnBottom = this.calcBottom(Config.SPAWN_PADDING);
despawnTop = player.y / Config.BLOCK_SIZE;
despawnBottom = player.y / Config.BLOCK_SIZE + 1;
}
private function buildLevel() : FlxTilemap
{
var waterFunction:Function;
var map:FlxTilemap = null;
var tileArray:Array = this.createRandomMap();
var tileCSV:String = FlxTilemap.arrayToCSV(tileArray,pixels.width);
map = new FlxTilemap();
map.loadMap(tileCSV,PlayMode["T" + _ch],Config.BLOCK_SIZE,Config.BLOCK_SIZE,0,0,1,80);
map.setTileProperties(52,FlxObject.ANY,function(param1:FlxTile, param2:FlxObject):void
{
var _loc3_:int = 0;
if(param2 is Bullet)
{
ArcParticleSystem.emit(Data.state.particles,"wooden_crate",param1.x,param1.y,10,1,5,400,-100,100,-100,-40,param1.width,param1.height);
map.setTileByIndex(param1.mapIndex,0,true);
_loc3_ = ArcU.rand(0,100);
if(_loc3_ >= 80)
{
if(_loc3_ < 99)
{
addDiamonds(param1.x,param1.y,ArcU.rand(1,2 * _ch));
}
else
{
createHeart(param1.x,param1.y);
}
}
++Data.boxes;
SoundSystem.play(Sound.BOX,0.4);
AchievementSystem.boxes();
}
});
map.setTileProperties(53,FlxObject.ANY,function(param1:FlxTile, param2:FlxObject):void
{
var _loc3_:int = 0;
if(param2 is Missile)
{
ArcParticleSystem.emit(Data.state.particles,"steel_crate",param1.x,param1.y,10,1,5,800,-100,100,-200,-80,param1.width,param1.height);
map.setTileByIndex(param1.mapIndex,0,true);
_loc3_ = ArcU.rand(0,100);
if(_loc3_ >= 50)
{
if(_loc3_ < 99)
{
addDiamonds(param1.x,param1.y,ArcU.rand(1,2 * _ch));
}
else
{
createHeart(param1.x,param1.y);
}
}
++Data.boxes;
SoundSystem.play(Sound.STEELBOX);
AchievementSystem.boxes();
}
});
waterFunction = function(param1:FlxTile, param2:FlxObject):void
{
var _loc3_:Player = param2 as Player;
if(_loc3_ == null)
{
return;
}
_loc3_.inWater();
};
map.setTileProperties(12,FlxObject.NONE,waterFunction,Player);
map.setTileProperties(56,FlxObject.ANY);
return map;
}
private function imageToTileArray(param1:BitmapData, param2:BitmapData) : Array
{
var _loc5_:int = 0;
var _loc6_:uint = 0;
var _loc7_:int = 0;
this.pixels = param1;
this.overpixels = param2;
var _loc3_:Array = new Array();
mpixels = param1.clone();
var _loc4_:int = 0;
while(_loc4_ < param1.height)
{
_loc5_ = 0;
while(_loc5_ < param1.width)
{
_loc6_ = getLayerTile(_loc5_,_loc4_,true);
_loc7_ = colorToTileID(_loc6_,_loc5_,_loc4_);
_loc3_.push(_loc7_);
_loc5_++;
}
_loc4_++;
}
return _loc3_;
}
private function createRandomMap() : Array
{
var _loc3_:int = 0;
var _loc4_:* = 0;
var _loc5_:int = 0;
var _loc10_:int = 0;
var _loc11_:int = 0;
var _loc12_:int = 0;
var _loc1_:BitmapData = new BitmapData(RUN_WIDTH,RUN_HEIGHT,false);
var _loc2_:BitmapData = new BitmapData(RUN_WIDTH,RUN_HEIGHT,false);
_loc3_ = 0;
while(_loc3_ < RUN_WIDTH)
{
_loc1_.setPixel(_loc3_,0,10052885);
_loc1_.setPixel(_loc3_,1,10052885);
if(_loc3_ > 25)
{
_loc5_ = ArcU.rand(1,100);
if(_loc5_ >= 3)
{
if(_loc5_ < 6)
{
_loc1_.setPixel(_loc3_,2,12221184);
}
else if(_loc5_ < 8)
{
_loc1_.setPixel(_loc3_,ArcU.rand(4,16),7929600);
}
else if(_loc5_ < 20)
{
_loc1_.setPixel(_loc3_,ArcU.rand(4,16),15763712);
}
}
}
_loc3_++;
}
var _loc6_:int = -20;
var _loc7_:int = 20;
var _loc8_:int = 2;
var _loc9_:int = 0;
_loc3_ = 0;
while(_loc3_ < RUN_WIDTH)
{
_loc12_ = _loc3_ / 300 * 3 + 1;
if(_loc6_ > 0)
{
if(--_loc6_ == 0)
{
if(_loc7_ < 3)
{
_loc7_ = 3;
}
_loc6_ = -ArcU.rand(10 + _loc12_ * 2,30 + _loc12_ * 2);
}
}
else
{
if(_loc6_ < 0)
{
_loc6_++;
}
else if(_loc6_ == 0)
{
_loc6_ = ArcU.rand(3,8 + _loc12_);
}
if(--_loc7_ <= 0)
{
_loc7_ = ArcU.rand(15,35);
_loc9_ = _loc8_;
_loc11_ = Math.min(10,_loc9_ + 4);
_loc10_ = Math.max(2,_loc9_ - 6);
_loc8_ = ArcU.rand(_loc10_,_loc11_);
if(_loc6_ > -3)
{
_loc6_ = -3;
}
}
_loc4_ = int(RUN_HEIGHT - 1);
for(; _loc4_ >= RUN_HEIGHT - _loc8_; _loc4_--)
{
if(_loc4_ == RUN_HEIGHT - _loc8_)
{
_loc1_.setPixel(_loc3_,_loc4_,5228053);
if(_loc3_ > 25)
{
_loc5_ = ArcU.rand(1,100);
if(_loc1_.getPixel(_loc3_,_loc4_ - 1) != 16777215)
{
continue;
}
if(_loc5_ < 5)
{
_loc1_.setPixel(_loc3_,_loc4_ - 1,9699328);
}
else if(_loc5_ < 7)
{
_loc1_.setPixel(_loc3_,_loc4_ - 1,9517164);
}
else if(_loc5_ < 20)
{
_loc1_.setPixel(_loc3_,_loc4_ - ArcU.rand(1,5),16763904);
}
else if(_loc5_ < 28 && _loc6_ < -2 && _loc7_ > 2)
{
_loc1_.setPixel(_loc3_,_loc4_ - 1,7028992);
if(ArcU.rand(1,8) == 1)
{
_loc1_.setPixel(_loc3_,_loc4_ - 2,7028992);
}
if(ArcU.rand(1,3) == 1)
{
_loc1_.setPixel(_loc3_ + 1,_loc4_ - 1,7028992);
if(ArcU.rand(1,2) == 1)
{
_loc1_.setPixel(_loc3_ + 1,_loc4_ - 2,7028992);
}
if(ArcU.rand(1,3) == 1)
{
_loc1_.setPixel(_loc3_ + 2,_loc4_ - 1,7028992);
if(ArcU.rand(1,7) == 1)
{
_loc1_.setPixel(_loc3_ + 2,_loc4_ - 2,7028992);
}
}
}
}
}
}
else
{
_loc1_.setPixel(_loc3_,_loc4_,10052885);
}
}
}
_loc3_++;
}
return this.imageToTileArray(_loc1_,_loc2_);
}
override public function draw() : void
{
super.draw();
if(_complete && this._gameOver != null)
{
this._gameOver.draw();
}
}
override protected function parallax() : void
{
var _loc1_:Background = null;
var _loc2_:int = 0;
for each(_loc1_ in [bg1,bg2,bg3])
{
_loc2_ = _loc1_.getScreenXY().x;
if(_loc2_ < -360)
{
_loc1_.x = FlxG.camera.scroll.x * _loc1_.scrollFactor.x + (_loc2_ > 0 && _loc2_ < 1000 ? _loc2_ : 0);
}
else if(_loc2_ > 0)
{
_loc1_.x = FlxG.camera.scroll.x * _loc1_.scrollFactor.x - 360 + (_loc2_ > 0 && _loc2_ < 1000 ? _loc2_ : 0);
}
}
}
override public function get caveHeight() : int
{
return player.x / 9;
}
override protected function calcTop(param1:int) : int
{
return FlxG.camera.scroll.x / Config.BLOCK_SIZE - param1;
}
override protected function calcBottom(param1:int) : int
{
return (FlxG.camera.scroll.x + FlxG.width) / Config.BLOCK_SIZE + param1;
}
override protected function spawn(param1:int) : void
{
var _loc2_:Seed = null;
super.spawn(param1);
if(ArcU.rand(1,100) < 6 && param1 > 30)
{
_loc2_ = new Seed(param1 * Config.BLOCK_SIZE + 100,ArcU.rand(4,16) * Config.BLOCK_SIZE,1);
_loc2_.velocity.x = -50;
enemies.add(_loc2_);
}
}
}
}