/**
* Copyright smirnov48 ( http://wonderfl.net/user/smirnov48 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/4Ubp
*/
/**
* Copyright smirnov48 ( http://wonderfl.net/user/smirnov48 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
*
* Pacman Proto 2: new graphics, start button, new movement
*
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.text.TextField;
import flash.ui.Keyboard;
[SWF( frameRate="30", backgroundColor="0x000000" )]
public class PacmanFL extends Sprite {
private var newScreen:Sprite = new Sprite();
private var playerSpawns:Array = new Array();
private var fruitSpawns:Array = new Array();
private var ghostSpawns:Array = new Array();
private var ghosts:Array = new Array();
private var player:Player;
/*
0 - empty
1 - wall
2 - pacman start
3 - fruit
4 - enemy spawn
5 - pill
6 - tablet(on init in all empty cells)
7 - no tablet
*/
private var level:Array = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 5, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 5, 1],
[1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 1, 1, 2, 1, 1, 0, 1, 1, 7, 7, 1, 1, 0, 1, 1, 2, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 4, 7, 4, 7, 1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 0, 1, 7, 4, 7, 4, 1, 0, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1],
[1, 1, 0, 1, 0, 1, 0, 0, 1, 3, 3, 1, 0, 0, 1, 0, 1, 0, 1, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1],
[1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
public function PacmanFL() {
graphics.beginFill(0);
graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
graphics.endFill();
addChild(newScreen);
init();
spawnPlayer();
spawnGhosts();
addEventListener(Event.ENTER_FRAME, tick);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
showStartButton();
}
private function init():void {
for (var i:int = 0; i < level.length; i++) {
for (var j:int = 0; j < level[0].length; j++) {
if (level[i][j] == 0) {
level[i][j] = 6;
}
if (level[i][j] == 2) {
playerSpawns.push(new Point(i, j));
level[i][j] = 6;
}
if (level[i][j] == 3) {
fruitSpawns.push(new Point(i, j));
level[i][j] = 6;
}
if (level[i][j] == 4) {
ghostSpawns.push(new Point(i, j));
level[i][j] = 7;
}
}
}
makeLevel();
}
private function spawnPlayer():void {
var position:int = Math.floor(Math.random() * playerSpawns.length);
player = new Player(playerSpawns[position], level);
newScreen.addChild(player);
}
private function spawnGhosts():void {
for (var i:int = 0; i < ghostSpawns.length; i++) {
var ghost:Ghost = new Ghost(ghostSpawns[i], level, ghosts, player);
newScreen.addChild(ghost);
ghosts.push(ghost);
}
}
private function updateGhosts():void {
for (var i:int = 0; i < ghosts.length; i++) {
(ghosts[i] as Ghost).update();
}
}
private function makeLevel():void {
for (var i:int = 0; i < level.length; i++) {
for (var j:int = 0; j < level[0].length; j++) {
if (level[i][j] == 1) {
var wall:Wall = new Wall();
wall.x = j * 16;
wall.y = i * 16;
newScreen.addChild(wall);
}
if (level[i][j] == 5) {
var pill:Pill = new Pill();
pill.x = j * 16;
pill.y = i * 16;
newScreen.addChild(pill);
level[i][j] = pill;
}
if (level[i][j] == 6) {
var tablet:Tablet = new Tablet();
tablet.x = j * 16;
tablet.y = i * 16;
newScreen.addChild(tablet);
level[i][j] = tablet;
}
}
}
}
private function onKeyDown(e:KeyboardEvent):void {
var x:int = 0;
var y:int = 0;
if (e.keyCode == Keyboard.UP) {
y--;
}
if (e.keyCode == Keyboard.DOWN) {
y++;
}
if (e.keyCode == Keyboard.LEFT) {
x--;
}
if (e.keyCode == Keyboard.RIGHT) {
x++;
}
player.move(x,y);
}
private function showStartButton():void {
var startButton:Sprite = new Sprite();
startButton.graphics.lineStyle(3, 0x00afaf);
startButton.graphics.beginFill(0x0a6f6f);
startButton.graphics.drawRoundRect(115,105,90,30, 6);
startButton.graphics.endFill();
var text:TextField = new TextField();
text.textColor = 0x00afaf;
text.text = 'Click to start';
text.selectable = false;
text.x = 125;
text.y = 112;
startButton.addChild(text);
startButton.addEventListener(MouseEvent.CLICK, function (e:Event):void {
startButton.parent.removeChild(startButton);
GameState.gameStarted = true;
});
addChild(startButton);
}
private function tick(e:Event):void {
if (GameState.gameStarted) {
updateGhosts();
player.update();
}
}
}
}
import flash.display.Sprite;
import flash.geom.Point;
internal class Wall extends Sprite {
function Wall() {
graphics.lineStyle(4, 0x00ffff);
graphics.beginFill(0x00afaf);
graphics.drawRoundRect(2, 2, 12, 12, 4);
graphics.endFill();
graphics.lineStyle(1, 0x308f8f);
graphics.drawRoundRect(2, 2, 12, 12, 4);
cacheAsBitmap = true;
}
}
internal class Tablet extends Sprite {
function Tablet() {
graphics.lineStyle(1, 0xffffff);
graphics.beginFill(0xafafaf);
graphics.drawCircle(8, 8, 2);
graphics.endFill();
cacheAsBitmap = true;
}
}
internal class Pill extends Sprite {
function Pill() {
graphics.lineStyle(2, 0x0000ff);
graphics.beginFill(0x0f0faf);
graphics.drawCircle(8, 8, 3);
graphics.endFill();
cacheAsBitmap = true;
}
}
internal class Player extends Sprite {
private var vx:int = 0;
private var vy:int = 0;
private var vel:int = 2;
private var level:Array;
function Player(position:Point, level:Array) {
this.level = level;
graphics.lineStyle(2, 0xFFFF00);
graphics.beginFill(0xFFFF00);
graphics.drawCircle(8, 8, 7);
graphics.endFill();
cacheAsBitmap = true;
x = position.y * 16;
y = position.x * 16;
}
public function move(x:int, y:int):void {
vx = x;
vy = y;
}
public function update():void {
x += vx * vel;
y += vy * vel;
var cell:* = level[Math.floor((y+8)/16)][Math.floor((x+8)/16)];
if (cell != 1) {
if (cell != 0 && cell as Sprite) {
(cell as Sprite).parent.removeChild((cell as Sprite));
cell = 0;
}
} else {
x -= vx * vel;
y -= vy * vel;
vx = 0;
vy = 0;
}
}
}
internal class Ghost extends Sprite {
private var vx:int = 0;
private var vy:int = 0;
private var vel:int = 2;
private var level:Array;
private var ghosts:Array;
private var player:Player;
function Ghost(position:Point, level:Array, ghosts:Array, player:Player) {
this.level = level;
this.ghosts = ghosts;
this.player = player;
graphics.lineStyle(2, 0xff0000);
graphics.beginFill(0xaf0f0f);
graphics.drawCircle(8, 8, 8);
graphics.endFill();
graphics.beginFill(0xaf0f0f);
graphics.drawRect(0, 8, 16, 8);
graphics.endFill();
cacheAsBitmap = true;
x = position.y * 16;
y = position.x * 16;
}
public function update():void {
var dx:int = Math.abs(player.y - y);
var dy:int = Math.abs(player.x - x);
if (dx < dy) {
if (player.x - x >= 0) {
vx = 1;
} else {
vx = -1;
}
vy = 0;
}
if (dx > dy) {
vx = 0;
if (player.y - y >= 0) {
vy = 1;
} else {
vy = -1;
}
}
x += vx * vel;
y += vy * vel;
var cell:* = level[Math.floor((y+8)/16)][Math.floor((x+8)/16)];
if (cell == 1) {
x -= vx * vel;
y -= vy * vel;
vx = 0;
vy = 0;
}
}
}
internal class GameState {
static public var gameStarted:Boolean = false;
}