ロックマン from ニコニコモンズ
package
{
import flash.display.*;
import flash.events.Event;
import flash.text.*;
[SWF(width="320", height="240", backgroundColor="0x000000", frameRate="30")]
public class MegaManTest extends Sprite
{
private var megaman:MegaMan;
private var count:int = 100;
private var test_col:TestCollision = new TestCollision();
function MegaManTest()
{
var tx:TextField = new TextField();
tx.width = 320;
tx.text = " Sprite pattern of MegaMan is distributed under niconi-commons\n"+
"license by CAPCOM Co., Ltd.\n"+
"* DO NOT USE FOR COMMERCIAL PURPOSE *";
tx.textColor = 0xffffff;
addChild(tx);
megaman = new MegaMan();
megaman.collision = test_col;
megaman.y = -80;
megaman.x = 160;
megaman.dir = MegaMan.D_LEFT;
megaman.jump();
megaman.run();
addChild(megaman);
var preview:Bitmap = new Bitmap(megaman.sprite.pool);
addChild(preview);
preview.y = 100;
preview.x = 20;
preview.scaleX = 0.3;
preview.scaleY = 0.3;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
if (count == 50) {
megaman.dir = MegaMan.D_LEFT;
megaman.run();
} else if (count == 200){
megaman.dir = MegaMan.D_RIGHT;
megaman.run();
} else if (count == 0 || count == 150) {
megaman.stand();
}
if (count == 90 || count == 240 || count == 280)
megaman.jump();
megaman.tick();
count = (count+1) % 300;
}
}
}
interface ICollision
{
function checkFloor(x1:Number, y1:Number, x2:Number, y2:Number):CollisionResult;
}
class TestCollision implements ICollision
{
public function checkFloor(x1:Number, y1:Number, x2:Number, y2:Number):CollisionResult
{
if (y1 < y2 && y2 > 100) {
var r:CollisionResult = new CollisionResult();
r.py = y2 -= 100;
return r;
}
return null;
}
}
class CollisionResult
{
public var px:Number = 0, py:Number = 0;
}
class MegaMan extends flash.display.Sprite
{
import flash.display.*;
import flash.geom.*;
public static const S_STAND:uint = 0x01;
public static const S_RUN:uint = 0x02;
public static const S_JUMP:uint = 0x04;
public static const D_LEFT:uint = 1;
public static const D_RIGHT:uint = 2;
private static const RUN_PTN:Array = [20, 21, 22, 23];
private static const STD_PTN:Array = [0, 0, 0, 1, 0, 0];
private static const FOOT_Y:int = 33;
private var mSpr:IndexedSprite;
private var mState:uint;
private var mDir:uint;
private var mV:Point = new Point(0, 0);
private var mVfix:Point = new Point(0, 0);
private var mRunSpeed:int = 3;
private var mAnimationCount:int;
private var mCollision:ICollision = null;
function MegaMan()
{
mDir = D_RIGHT;
mState = S_STAND;
mAnimationCount = 0;
var pool:SpritePool = new SpritePool(MegaManPatternData);
mSpr = new IndexedSprite(
pool,
MegaManPatternData.CELL_WIDTH,
MegaManPatternData.CELL_HEIGHT,
MegaManPatternData.CELL_COLS
);
addChild(mSpr);
}
public function set collision(c:ICollision):void {
mCollision = c;
}
public function set dir(d:uint):void {
mDir = d;
}
private function ifChangeState(s:uint):void
{
if (mState != s) {
mState = s;
mAnimationCount = 0;
}
}
public function run():void {
if (mState != S_JUMP)
ifChangeState(S_RUN);
mV.x = mRunSpeed * ((mDir==D_LEFT) ? -1 : 1);
}
public function stand():void {
if (mState != S_JUMP)
ifChangeState(S_STAND);
mV.x = 0;
}
public function get sprite():IndexedSprite
{
return mSpr;
}
public function jump():void
{
if (mState != S_JUMP) {
mAnimationCount = 0;
mV.y = -5;
mState = S_JUMP;
}
}
private function land():void
{
mState = (mV.x!=0) ? S_RUN : S_STAND;
}
public function tick():void
{
checkFloorCollosion();
switch(mState) {
case S_STAND:
mSpr.index = STD_PTN[ int(mAnimationCount/6)%6 ];
break;
case S_RUN:
mSpr.index = RUN_PTN[ int(mAnimationCount/4)%4 ];
break;
case S_JUMP:
if (mAnimationCount > 5) {
mV.y += 1;
if (mV.y > 8) mV.y = 8;
}
mSpr.index = 10;
break;
}
mSpr.scaleX = (mDir == D_RIGHT) ? -1 : 1;
mSpr.x = -32 * mSpr.scaleX;
mAnimationCount++;
updatePosition();
}
private static const FOOT_W:int = 3;
private function checkFloorCollosion():void
{
if (!mCollision) return;
var r:CollisionResult;
if (mState == S_JUMP) {
r = mCollision.checkFloor(x-FOOT_W, y + FOOT_Y, x-FOOT_W+mV.x, y + FOOT_Y+mV.y);
if (!r)
r = mCollision.checkFloor(x+FOOT_W, y + FOOT_Y, x+mV.x+FOOT_W, y + FOOT_Y+mV.y);
if (r) {
land();
mV.y -= r.py;
mVfix.y = -mV.y;
}
}
}
private function updatePosition():void
{
x += mV.x;
y += mV.y;
mV.x += mVfix.x;
mV.y += mVfix.y;
mVfix.x = 0;
mVfix.y = 0;
}
}
class IndexedSprite extends flash.display.Sprite
{
import flash.display.*;
import flash.geom.*;
private var mPool:BitmapData;
private var mCellWidth:uint;
private var mCellHeight:uint;
private var mCols:uint = 0;
private var mBmp:Bitmap;
private var mBuf:BitmapData;
private var mRc:Rectangle;
private var mPt:Point = new Point(0, 0);
function IndexedSprite(pool:BitmapData, cw:uint, ch:uint, cols:uint)
{
mPool = pool;
mCellWidth = cw;
mCellHeight = ch;
mCols = cols;
mBuf = new BitmapData(cw, ch);
mBmp = new Bitmap(mBuf);
addChild(mBmp);
mRc = new Rectangle(0, 0, cw, ch);
this.index = 0;
}
public function set index(i:int):void
{
mRc.x = (i%mCols) * mCellWidth;
mRc.y = int(i/mCols) * mCellHeight;
mBuf.copyPixels(mPool, mRc, mPt);
}
public function get pool():BitmapData
{
return mPool;
}
}
class SpritePool extends flash.display.BitmapData
{
import flash.utils.ByteArray;
function SpritePool(src:*)
{
var ba:ByteArray = B64.decode(src.DATA);
ba.uncompress();
var w:int = ba.readUnsignedShort();
var h:int = ba.readUnsignedShort();
super(w, h, true, 0);
var pal_length:int = ba.readUnsignedByte();
var pal:Array = new Array(pal_length);
for (var i:int = 0;i < pal_length;i++) {
pal[i] = ba.readUnsignedInt();
}
lock();
var x:int, y:int, k:uint;
for (y = 0;y < h;y++) {
for (x = 0;x < w;x += 2) {
k = ba.readUnsignedByte();
setPixel32(x , y, uint(pal[k&0x0f]));
setPixel32(x+1, y, uint(pal[(k&0xf0)>>4]));
}
}
unlock();
}
}
class B64
{
import flash.utils.ByteArray;
public static function decode(raw:String):ByteArray
{
var res:ByteArray = new ByteArray();
var d1:int, d2:int, d3:int, d4:int;
var len:int = raw.length;
for (var i:int = 0;i < len;i += 4) {
d1 = v(raw.charCodeAt(i )); d2 = v(raw.charCodeAt(i+1));
d3 = v(raw.charCodeAt(i+2)); d4 = v(raw.charCodeAt(i+3));
if (d2 == 64) break;
res.writeByte( (d1 << 2) + ((d2 & 0x30) >> 4) );
if (d3 == 64) break;
res.writeByte( ((d2 & 0x0f) << 4) + ((d3 & 0x3c) >> 2) );
if (d4 == 64) break;
res.writeByte( ((d3 & 0x03) << 6) + d4 );
}
res.position = 0;
return res;
}
public static function v(c:int):int {
return (c==61) ? 64 : (c>=65 && c<=90) ? (c-65) : (c>=97 && c <= 122) ? (c-71) : (c>=48) ? (c+4) : (c==43) ? 62 : 63;
}
}
class MegaManPatternData
{
public static const CELL_WIDTH:int = 64;
public static const CELL_HEIGHT:int = 64;
public static const CELL_COLS:int = 10;
public static const DATA:String =
'eNrtnU2W27oRhUWfPhmjiKe5AbYXAOJpkJnaphcgW1xJNvA2k2VkPRlmCQ4AynYGQpUO0Ax/dL8X9yAlkMRl4Vfi5Ye/Pvz1t0PgR/z393/9+HH4x39+JP79zwMAAAAAAHhmqJkzLpVef/0FlOUPUBeXSi9P7RWa1uv54lLp5am8QkXO+2auuFR6BelXeYXO8uXr4lLp'+
'VINF9X3kCtny5L1v54pLpacGREvq98AVcrz54cqVv/hx5OJ8+SlKfP/tlmzhUv1E/S40nDj9xnHk4nz5KWo5ASkkwIJDjFS/h/Rj7j9diLh4vrwK84IUNdbmD6C6tkY/Vdv2pfqJ+l+H64mE+0MF5RV5TSka9MsLpHTov7viCkidpzi3k+on3r8jXXS+gZXHjbHh/09R'+
'k08wRafv46krncJKnac4OZbqJw/f4QYY2zfvHnfOa5uirc7qp0wb7v/YmbZMQKnzlCbH6uf1lwroupbI2uxNLI/H9mttjDqdLx4+cKRR276oC5Q6T3Fy7Lydrr9wCqB8F3qnfAOriYfxgyhGXT79fJfyL+iXqYCw9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