notice: Flash editor updated! Join the development! Thanks to MiniBuilder


embed

FORKED
  1. // forked from grapefrukt's a-steroids4k
  2. /**
  3.  * http://prototyprally.com/a-steroids4k/
  4.  * @author Martin Jonasson (grapefrukt@grapefrukt.com)
  5.  */
  6. /* 
  7. Licensed under the MIT License
  8. Copyright (c) 2009 Martin Jonasson
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. http://prototyprally.com/a-steroids4k/
  25. */
  26. package  {
  27.     import flash.display.BitmapData;
  28.     import flash.display.Sprite;
  29.     import flash.filters.GlowFilter;
  30.     import flash.geom.Matrix;
  31.     import flash.geom.Point;
  32.     import flash.geom.Rectangle;
  33.     import flash.text.TextField;
  34.     import flash.text.TextFormat;
  35.     import flash.utils.getTimer;
  36.     
  37.     /**
  38.      * a-steroids4k a game by grapefrukt (grapefrukt@grapefrukt.com)
  39.      * http://prototyprally.com/a-steroids4k/
  40.      */
  41.     
  42.     [ SWF( backgroundColor = '#2c2c2c', width = '500', height = '500' ) ]
  43.     public class M extends Sprite {
  44.         
  45.         private var dc        :Sprite;
  46.         private var ship    :S;
  47.         public  var txt        :TextField;
  48.         private var points    :int;
  49.         public  var r        :Boolean = false;
  50.         public  var st        :int = -3000;
  51.         private var c        :Vector.<D>;
  52.         
  53.         public function M():void {
  54.             dc = new Sprite;
  55.             c = new Vector.<D>;
  56.             addChild(dc);
  57.             
  58.             ship = new S;
  59.             ship.die();
  60.             addChild(ship);
  61.             
  62.             txt = new TextField();
  63.             txt.textColor = 0xdddddd;
  64.             txt.selectable = false;
  65.             txt.defaultTextFormat = new TextFormat("_sans"10);
  66.             txt.text = "a-steroids4k\nby grapefrukt\nclick to start";
  67.             addChild(txt);
  68.             
  69.             stage.addEventListener("mouseDown", hm);
  70.             stage.addEventListener("mouseUp", hm);
  71.             addEventListener("enterFrame", ef);
  72.             
  73.             stage.quality = "medium";
  74.             
  75.         }
  76.         
  77.         private function hm(e:*):void {
  78.             if(!r && getTimer() - st > 3000){
  79.                 for each(var d:D in c) d.alive = false;
  80.                 for (var i:uint = 0; i < 16; i++) {
  81.                     da();
  82.                 }
  83.                 ship.die();
  84.                 r = true;
  85.                 st = getTimer();
  86.             }
  87.             
  88.             ship.laser = 0;
  89.             if(e.buttonDown && ship.alive && r) ship.laser = 50;
  90.             ship.laser ? ship.lgfx.filters = [new GlowFilter(0xffffff, .61212)] : ship.lgfx.filters = [];
  91.         }
  92.         
  93.         private function ef(e:*):void {
  94.             
  95.             var d:D;
  96.             var d2:D;
  97.             var subresult:Vector.<D>;
  98.             var grav:Point = new Point;
  99.             var grav_l:Number;
  100.             
  101.             txt.x += (ship.x - txt.x + 15) * 0.1;
  102.             txt.y += (ship.y - txt.y - 15) * 0.1;
  103.             
  104.             for each (d in c) {
  105.                 d.u();
  106.                 if (!d.alive) dcr(d);
  107.                 
  108.                 if (r && ship.alive && d.c == 0x5cec33) {
  109.                     grav.x = ship.x - d.x;
  110.                     grav.y = ship.y - d.y;
  111.                     grav_l = grav.length;
  112.                     if (grav_l < 100) {
  113.                         grav.normalize(1 - grav_l / 100);
  114.                         d.vx += grav.x * 0.1;
  115.                         d.vy += grav.y * 0.1;
  116.                     }
  117.                     if (grav_l < 10) {
  118.                         d.alive = false;
  119.                         txt.alpha = 1;
  120.                         txt.text = ++points + " / 999";
  121.                         txt.textColor = d.c;    // using the color from the destructable to save a literal specification
  122.                         if (points >= 999) { // this ends the game
  123.                             r = false
  124.                             points = 0;
  125.                             txt.textColor = 0xdddddd;
  126.                             txt.text = "done!\n" + ((getTimer() - st) / 1000).toFixed(1) + "s";
  127.                             st = getTimer();
  128.                         }
  129.                     }
  130.                 }
  131.                 
  132.                 if (d.touched) {
  133.                     subresult = d.s();
  134.                     
  135.                     if (subresult == null) {
  136.                         d.blit();
  137.                         d.touched = false;
  138.                     } else {
  139.                         for each(d2 in subresult) {
  140.                             d2.touched = false;
  141.                             dca(d2);
  142.                         }
  143.                         dcr(d);
  144.                     }
  145.                 }
  146.                 
  147.             }
  148.             
  149.             if (r) {
  150.                 if(txt.alpha) txt.alpha -= 0.01;
  151.                 ship.u();
  152.             }
  153.             
  154.             if (ship.alive) {
  155.                 
  156.                 var target:D;
  157.                 var coords:Point;
  158.                 
  159.                 var p:Point = new Point(mouseX - ship.x, mouseY - ship.y);
  160.                 if(ship.laser) ship.laser = p.length * 3 + 1;
  161.                 p.normalize(1);
  162.                 
  163.                 ship.vx += p.x * .05;
  164.                 ship.vy += p.y * .05;
  165.                 
  166.                 if (ship.laser && r) {
  167.                     ship.vx -= p.x * .15;
  168.                     ship.vy -= p.y * .15;
  169.                     
  170.                     var steps:uint = 0;
  171.                     while (steps++ < ship.laser *.5) {
  172.                         target = dcght(ship.x + p.x * steps, ship.y + p.y * steps);
  173.                         if (target && target.valid) {
  174.                             coords = target.ghc(ship.x + p.x * steps, ship.y + p.y * steps);
  175.                             if (target.gp(coords.x, coords.y)) break;
  176.                         }
  177.                     }
  178.                     
  179.                     if (target && coords) {
  180.                         ship.laser = steps * 2;
  181.                         if(Math.random() < .5) da(1, ship.x + p.x * steps, ship.y + p.y * steps);
  182.                         target.sp(coords.x, coords.y, false);
  183.                     }
  184.                 }
  185.                 
  186.                 target = dcght(ship.x, ship.y);
  187.                 if (target && r) {
  188.                     coords = target.ghc(ship.x, ship.y);
  189.                     if (coords && target.valid) ship.die();
  190.                 }
  191.             } else {
  192.                 ship.x = mouseX - 5;
  193.                 ship.y = mouseY - 5;
  194.             }
  195.         }
  196.         
  197.         private function da(size:Number = 0, x:Number = 0, y:Number = 0):void {
  198.             var d:D = new D(size || 3 + 40 * Math.random(), size || 3 + 40 * Math.random());
  199.             d.x = x || 500 * Math.random();
  200.             d.y = y || 500 * Math.random();
  201.             dca(d);
  202.         }
  203.             
  204.         public function dca(d:D):void { // destructable collection, add
  205.             c.push(d);
  206.             dc.addChild(d);
  207.         }
  208.         
  209.         public function dcr(d:D):void { // destructable collection, remove
  210.             dc.removeChild(d);
  211.             var i:int = 0;
  212.             for each (var di:D in c) {
  213.                 if (di == d) {
  214.                     c.splice(i, 1);
  215.                     break;
  216.                 }
  217.                 i++;
  218.             }
  219.         }
  220.         
  221.         public function dcght(x:uint, y:uint):D { // destructable collection, get hit target
  222.             var result:Array = dc.getObjectsUnderPoint(new Point(x, y));
  223.             return result[0];
  224.         }
  225.             
  226.     }
  227.     
  228. }
  229. import flash.display.Sprite;
  230. class GO extends Sprite { // GameObject
  231.         
  232.     public var vx:Number = 0;
  233.     public var vy:Number = 0;
  234.     public var vr:Number = 0;
  235.     
  236.     public var alive:Boolean = true;
  237.     public var age:int = 150;
  238.     
  239.     public var bm:uint = 50;
  240.         
  241.     public function u():void {
  242.         x             += vx;
  243.         y             += vy;
  244.         rotation     += vr;
  245.         if (width < 8 && height < 8) {
  246.             age--;
  247.             vx *= 0.99;
  248.             vy *= 0.99;
  249.         }
  250.         if (age < 0) alive = false;
  251.         
  252.         if (x > 500 + bm) {    
  253.             x = -bm;
  254.         } else if (x < -bm) {            
  255.             x = 500 + bm;
  256.         }
  257.         
  258.         if (y > 500 + bm) {
  259.             y = -bm;
  260.         } else if (y < -bm) {            
  261.             y = 500 + bm;
  262.         }
  263.     }
  264. }
  265. import flash.display.Sprite;
  266. import flash.events.Event;
  267. import flash.geom.Point;
  268. import flash.utils.getTimer;
  269. class S extends GO {
  270.     public  var laser    :Number = 0;
  271.     private var deathp    :uint     = 0// death time penalty
  272.     public  var lgfx    :Sprite;
  273.     
  274.     public function S() {
  275.         bm = 0;
  276.         
  277.         graphics.beginFill(0xff0000); //NORMAL_COLOR
  278.         graphics.drawRect( -2, -244);
  279.         
  280.         lgfx = new Sprite;
  281.         lgfx.graphics.beginFill(0x79daff); // DRILL_COLOR
  282.         lgfx.graphics.drawRect( 0, -2.51005);
  283.         lgfx.blendMode = "add";
  284.         lgfx.scaleX = 0;
  285.         addChild(lgfx);
  286.     }
  287.     
  288.     public function die():void {
  289.         alive    = false;
  290.         deathp     = 100// 3s penalty at 30fps
  291.         alpha     = .5;
  292.     }
  293.     
  294.     override public function u():void {
  295.         
  296.         super.u();
  297.         
  298.         vx *= 0.99;
  299.         vy *= 0.99;
  300.         
  301.         rotation = Math.atan2(vy, vx) * 57// approximation of 180 / Math.PI, saves 16 bytes a piece
  302.         
  303.         lgfx.rotation = Math.atan2(mouseY, mouseX) * 57// approximation of 180 / Math.PI, saves 16 bytes
  304.         lgfx.scaleX = laser / 200;
  305.         lgfx.scaleY = Math.sin(getTimer()/50)*0.5 + .75;
  306.         age = 100;
  307.         
  308.         if (deathp && M(parent).r) {
  309.             M(parent).txt.alpha = 1;
  310.             M(parent).txt.textColor = 0xff0000;
  311.             M(parent).txt.text = "spawn in " + deathp;
  312.             alpha = Math.sin(deathp--);
  313.         }
  314.         
  315.         if (deathp == 1) {
  316.             alive = true;
  317.             deathp = 0;
  318.             alpha = 1;
  319.             M(parent).txt.text = "";
  320.         }
  321.         
  322.     }
  323.     
  324. }
  325. import flash.display.BitmapData;
  326. import flash.geom.Point;
  327. import flash.geom.Rectangle;
  328. import flash.filters.GlowFilter;
  329. class D extends GO {
  330.     public var bmp        :BitmapData;
  331.     public var com        :Point;            // centerOfMass
  332.     public var touched    :Boolean     = true;
  333.     public var c        :uint         = 0xeeeeee;
  334.     public static const PX    :uint = 3;            // pixel size
  335.     public static const AC    :uint = 0xff0000;    // active color
  336.     public static const IC    :uint = 0x000000;    // inactive color
  337.     public static const RC    :uint = 0xffff00;    // replace color
  338.     public function D(w:int, h:int) {
  339.         bmp = new BitmapData(w, h, false, AC);
  340.         com = new Point;
  341.         
  342.         filters = [new GlowFilter(0122)];
  343.         
  344.         if (w * h == 1 && Math.random() < .5) c = 0x5cec33;
  345.         
  346.         age += Math.random() * 500;
  347.         
  348.         vx = Math.random() - .5;
  349.         vy = Math.random() - .5;
  350.         vr = Math.random() - .5;
  351.     }
  352.     public function sp(x:uint, y:uint, value:Boolean):void {    // set pixel
  353.         if (x >= 0 && x < bmp.width && y >= 0 && y < bmp.height) {
  354.             bmp.setPixel(x, y, value ? AC : IC);
  355.             touched = true;
  356.         }
  357.     }
  358.     public function gp(x:uint, y:uint):uint {    // get pixel
  359.         var result:uint;
  360.         if (x >= 0 && x < bmp.width && y >= 0 && y < bmp.height) result = bmp.getPixel(x, y);
  361.         return result;
  362.     }
  363.     public function blit():void {
  364.         graphics.clear();            
  365.         for (var iy:uint = 0; iy < bmp.height; iy++) {
  366.             var ix:uint = 0;
  367.             var w:uint = 0;
  368.             
  369.             while(ix < bmp.width){
  370.                 while (bmp.getPixel(ix + w, iy) && ix + w < bmp.width) w++;
  371.                 if (w == 0) {
  372.                     ix++;
  373.                 } else {
  374.                     graphics.beginFill(c);
  375.                     graphics.drawRect(ix * PX - com.x, iy * PX - com.y, w * PX, PX * 1.1);
  376.                     graphics.endFill();
  377.                     ix += w;
  378.                     w = 0;
  379.                 }
  380.             }
  381.         }
  382.     }
  383.     public function cpb(bmpd:BitmapData):BitmapData {    // copy bitmap
  384.         var pixelcount    :uint = bmp.width * bmp.height;
  385.         var ix            :uint = 0;
  386.         var iy            :uint = 0;
  387.         
  388.         for (var i:uint = 0; i < pixelcount; i++) {
  389.             ix = i % bmp.width;
  390.             iy = i / bmp.width;
  391.             bmp.setPixel(ix, iy, bmpd.getPixel(ix, iy) == RC ? AC : IC); // this is horrible, but it saves 6 bytes!
  392.         }
  393.         
  394.         um();
  395.         blit();
  396.         
  397.         return bmp;
  398.     }
  399.     /**
  400.      * Gets the hit coordinates for a given point, the coords sent in 
  401.      * should be in the stage's coordinate space
  402.      * @param    x
  403.      * @param    y
  404.      * @return    the coordinates of the hit voxel
  405.      */
  406.     public function ghc(x:uint, y:uint):Point {
  407.         var hitpos:Point = globalToLocal(new Point(x, y));
  408.         hitpos.x = (hitpos.x + com.x) / PX;
  409.         hitpos.y = (hitpos.y + com.y) / PX;
  410.         return hitpos;
  411.     }
  412.     public function s():Vector.<D> { // s
  413.         var ix:uint = 0;
  414.         var iy:uint = 0;
  415.         var rect:Rectangle;
  416.         var rect2:Rectangle;
  417.         
  418.         var parts:Vector.<D> = new Vector.<D>;
  419.         
  420.         if (alive) {
  421.             for (iy = 0; iy < bmp.height; iy++) {
  422.                 for (ix = 0; ix < bmp.width; ix++) {
  423.                     if (bmp.getPixel(ix, iy) == AC) {
  424.                         //trace("found set pixel at:", ix, iy);
  425.                         //trace("image is:", bmp.width, bmp.height);
  426.                         bmp.floodFill(ix, iy, RC);
  427.                         rect = bmp.getColorBoundsRect(0xffffff, RC, true);
  428.                         
  429.                         // checks if the currently colored segment is the only one in this destructable
  430.                         rect2 = bmp.getColorBoundsRect(0xffffff, AC,  true);
  431.                         
  432.                         // if it is, we skip the entire sting
  433.                         if (rect2.width == 0 && rect2.height == 0 && parts.length == 0) {
  434.                             // color the bitmap back to keep it until next time
  435.                             bmp.floodFill(ix, iy, AC);
  436.                             
  437.                             // only had one segment, escape the loop
  438.                             return null;
  439.                             
  440.                         } else {
  441.                             var nd:D = _s(rect, ix, iy);
  442.                             if (nd) parts.push(nd);
  443.                             ix = rect2.x;
  444.                             iy = rect2.y;
  445.                         }
  446.                     }
  447.                 }
  448.             }
  449.         }
  450.         
  451.         return parts;
  452.     }
  453.     private function _s(rect:Rectangle, firstX:uint, firstY:uint):D {
  454.         //trace("sting region at", rect);
  455.         
  456.         if (!(rect.width + rect.height)) return null;
  457.         
  458.         var nd:D = new D(rect.width, rect.height);
  459.         var pos:Point = localToGlobal(new Point(rect.x * PX - com.x, rect.y * PX - com.y));
  460.         
  461.         nd.x         = pos.x;
  462.         nd.y         = pos.y;
  463.         nd.rotation = rotation;
  464.         nd.vx    = vx * (.9 + Math.random() * .2);
  465.         nd.vy    = vy * (.9 + Math.random() * .2);
  466.         nd.vr    = vr * (.9 + Math.random() * .2);
  467.         
  468.         var bmpData:BitmapData = new BitmapData(rect.width, rect.height);
  469.         bmpData.copyPixels(bmp, rect, new Point);        
  470.         nd.cpb(bmpData);
  471.         bmp.floodFill(firstX, firstY, IC);
  472.         
  473.         return nd;
  474.     }
  475.     private function um():void {    // update mass
  476.         
  477.         var points:uint = 0;
  478.         
  479.         var pixelcount    :uint = bmp.width * bmp.height;
  480.         var ix            :uint = 0;
  481.         var iy            :uint = 0;
  482.         
  483.         for (var i:uint = 0; i < pixelcount; i++) {
  484.             ix = i % bmp.width;
  485.             iy = i / bmp.width;
  486.             if (bmp.getPixel(ix, iy) == AC) {
  487.                 com.x += ix;
  488.                 com.y += iy;
  489.                 points++;
  490.             }
  491.         }
  492.         
  493.         com.x = com.x / points * PX + PX / 2;
  494.         com.y = com.y / points * PX + PX / 2;
  495.         
  496.         x = localToGlobal(com).x;
  497.         y = localToGlobal(com).y;
  498.     }
  499.     
  500.     public function get valid():Boolean {
  501.         return bmp.width > 1 && bmp.height > 1;
  502.     }
  503. }
noswf
  1. /**
  2.  * http://meyermike.com
  3.  * @author Mike Meyer (mike@meyermike.com)
  4.  */
  5. /* 
  6. Licensed under the MIT License
  7. Copyright (c) 2009 Mike Meyer
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. */
  24. package  {
  25.     import flash.display.BitmapData;
  26.     import flash.display.Sprite;
  27.     import flash.filters.GlowFilter;
  28.     import flash.geom.Matrix;
  29.     import flash.geom.Point;
  30.     import flash.geom.Rectangle;
  31.     import flash.text.TextField;
  32.     import flash.text.TextFormat;
  33.     import flash.utils.getTimer;
  34.         
  35.     [ SWF( backgroundColor = '#330066', width = '100', height = '100' ) ]
  36.     public class Main extends Sprite {
  37.         
  38.         private var dc        :Sprite;
  39.         private var ship    :S;
  40.         private var points    :int;
  41.         public  var r        :Boolean = false;
  42.         public  var st        :int = -3000;
  43.         private var c        :Vector.<D>;
  44.         
  45.         public function Main():void {
  46.                         trace("HI");
  47.                         
  48.             dc = new Sprite;
  49.             c = new Vector.<D>;
  50.             addChild(dc);
  51.             
  52.             ship = new S;
  53.             ship.die();
  54.             addChild(ship);
  55.                         
  56.             stage.addEventListener("mouseDown", HandleMouse);
  57.             stage.addEventListener("mouseUp", HandleMouse);
  58.             addEventListener("enterFrame", EnterFrame);
  59.         }
  60.         
  61.         private function HandleMouse(e:*):void {
  62.             if(!r && getTimer() - st > 3000){
  63.                 for each(var d:D in c) d.alive = false;
  64.                 for (var i:uint = 0; i < 16; i++) {
  65.                     da();
  66.                 }
  67.                 ship.die();
  68.                 r = true;
  69.                 st = getTimer();
  70.             }
  71.             
  72.             ship.laser = 0;
  73.             if(e.buttonDown && ship.alive && r) ship.laser = 50;
  74.         }
  75.         
  76.         private function EnterFrame(e:*):void {
  77.             
  78.             var d:D;
  79.             var d2:D;
  80.             var subresult:Vector.<D>;
  81.             var grav:Point = new Point;
  82.             var grav_l:Number;
  83.             
  84.             for each (d in c) {
  85.                 d.u();
  86.                 if (!d.alive) dcr(d);
  87.                 
  88.                 if (r && ship.alive && d.c == 0x5cec33) {
  89.                     grav.x = ship.x - d.x;
  90.                     grav.y = ship.y - d.y;
  91.                     grav_l = grav.length;
  92.                     if (grav_l < 100) {
  93.                         grav.normalize(1 - grav_l / 100);
  94.                         d.vx += grav.x * 0.1;
  95.                         d.vy += grav.y * 0.1;
  96.                     }
  97.                     if (grav_l < 10) {
  98.                         d.alive = false;
  99.                         if (points > 300) { // this ends the game
  100.                             r = false
  101.                             points = 0;
  102.                             st = getTimer();
  103.                         }
  104.                     }
  105.                 }
  106.                 
  107.                 if (d.touched) {
  108.                     subresult = d.s();
  109.                     
  110.                     if (subresult == null) {
  111.                         d.blit();
  112.                         d.touched = false;
  113.                     } else {
  114.                         for each(d2 in subresult) {
  115.                             d2.touched = false;
  116.                             dca(d2);
  117.                         }
  118.                         dcr(d);
  119.                     }
  120.                 }
  121.                 
  122.             }
  123.             
  124.             if (r) {
  125.                 ship.u();
  126.             }
  127.             
  128.             if (ship.alive) {
  129.                 
  130.                 var target:D;
  131.                 var coords:Point;
  132.                 
  133.                 var p:Point = new Point(mouseX - ship.x, mouseY - ship.y);
  134.                 if(ship.laser) ship.laser = p.length * 3 + 1;
  135.                 p.normalize(1);
  136.                 
  137.                 ship.vx += p.x * .05;
  138.                 ship.vy += p.y * .05;
  139.                 
  140.                 if (ship.laser && r) {
  141.                     ship.vx -= p.x * .15;
  142.                     ship.vy -= p.y * .15;
  143.                     
  144.                     var steps:uint = 0;
  145.                     while (steps++ < ship.laser *.5) {
  146.                         target = dcght(ship.x + p.x * steps, ship.y + p.y * steps);
  147.                         if (target && target.valid) {
  148.                             coords = target.ghc(ship.x + p.x * steps, ship.y + p.y * steps);
  149.                             if (target.gp(coords.x, coords.y)) break;
  150.                         }
  151.                     }
  152.                     
  153.                     if (target && coords) {
  154.                         ship.laser = steps * 2;
  155.                         if(Math.random() < .5) da(1, ship.x + p.x * steps, ship.y + p.y * steps);
  156.                         target.sp(coords.x, coords.y, false);
  157.                     }
  158.                 }
  159.                 
  160.                 target = dcght(ship.x, ship.y);
  161.                 if (target && r) {
  162.                     coords = target.ghc(ship.x, ship.y);
  163.                     if (coords && target.valid) ship.die();
  164.                 }
  165.             } else {
  166.                 ship.x = mouseX - 5;
  167.                 ship.y = mouseY - 5;
  168.             }
  169.         }
  170.         
  171.         private function da(size:Number = 0, x:Number = 0, y:Number = 0):void {
  172.             var d:D = new D(size || 3 + 40 * Math.random(), size || 3 + 40 * Math.random());
  173.             d.x = x || 500 * Math.random();
  174.             d.y = y || 500 * Math.random();
  175.             dca(d);
  176.         }
  177.             
  178.         public function dca(d:D):void { // destructable collection, add
  179.             c.push(d);
  180.             dc.addChild(d);
  181.         }
  182.         
  183.         public function dcr(d:D):void { // destructable collection, remove
  184.             dc.removeChild(d);
  185.             var i:int = 0;
  186.             for each (var di:D in c) {
  187.                 if (di == d) {
  188.                     c.splice(i, 1);
  189.                     break;
  190.                 }
  191.                 i++;
  192.             }
  193.         }
  194.         
  195.         public function dcght(x:uint, y:uint):D { // destructable collection, get hit target
  196.             var result:Array = dc.getObjectsUnderPoint(new Point(x, y));
  197.             return result[0];
  198.         }
  199.             
  200.     }
  201.     
  202. }
  203. import flash.display.Sprite;
  204. class GO extends Sprite { // GameObject
  205.         
  206.     public var vx:Number = 0;
  207.     public var vy:Number = 0;
  208.     public var vr:Number = 0;
  209.     
  210.     public var alive:Boolean = true;
  211.     public var age:int = 150;
  212.     
  213.     public var bm:uint = 50;
  214.         
  215.     public function u():void {
  216.         x             += vx;
  217.         y             += vy;
  218.         rotation     += vr;
  219.         if (width < 8 && height < 8) {
  220.             age--;
  221.             vx *= 0.99;
  222.             vy *= 0.99;
  223.         }
  224.         if (age < 0) alive = false;
  225.         
  226.         if (x > 500 + bm) {    
  227.             x = -bm;
  228.         } else if (x < -bm) {            
  229.             x = 500 + bm;
  230.         }
  231.         
  232.         if (y > 500 + bm) {
  233.             y = -bm;
  234.         } else if (y < -bm) {            
  235.             y = 500 + bm;
  236.         }
  237.     }
  238. }
  239. import flash.display.Sprite;
  240. import flash.events.Event;
  241. import flash.geom.Point;
  242. import flash.utils.getTimer;
  243. class S extends GO {
  244.     public  var laser    :Number = 0;
  245.     private var deathp    :uint     = 0// death time penalty
  246.     
  247.     public function S() {
  248.         bm = 0;
  249.         
  250.         graphics.beginFill(0xff0000); //NORMAL_COLOR
  251.         graphics.drawRect( -0.5, -0.511);
  252.     }
  253.     
  254.     public function die():void {
  255.         alive    = false;
  256.         deathp     = 100// 3s penalty at 30fps
  257.         alpha     = .5;
  258.     }
  259.     
  260.     override public function u():void {
  261.         
  262.         super.u();
  263.         
  264.         vx *= 0.99;
  265.         vy *= 0.99;
  266.         
  267.         age = 100;
  268.         
  269.         if (deathp && Main(parent).r) {
  270.             alpha = Math.sin(deathp--);
  271.         }
  272.         
  273.         if (deathp == 1) {
  274.             alive = true;
  275.             deathp = 0;
  276.             alpha = 1;
  277.         }
  278.         
  279.     }
  280.     
  281. }
  282. import flash.display.BitmapData;
  283. import flash.geom.Point;
  284. import flash.geom.Rectangle;
  285. import flash.filters.GlowFilter;
  286. class D extends GO {
  287.     public var bmp        :BitmapData;
  288.     public var com        :Point;            // centerOfMass
  289.     public var touched    :Boolean     = true;
  290.     public var c        :uint         = 0xeeeeee;
  291.     public static const PX    :uint = 1;            // pixel size
  292.     public static const AC    :uint = 0xff0000;    // active color
  293.     public static const IC    :uint = 0x000000;    // inactive color
  294.     public static const RC    :uint = 0xffff00;    // replace color
  295.     public function D(w:int, h:int) {
  296.         bmp = new BitmapData(w, h, false, AC);
  297.         com = new Point;
  298.         
  299.         filters = [new GlowFilter(0122)];
  300.         
  301.         if (w * h == 1 && Math.random() < .5) c = 0x5cec33;
  302.         
  303.         age += Math.random() * 50;
  304.         
  305.         vx = Math.random() - .5;
  306.         vy = Math.random() - .5;
  307.         vr = Math.random() - .5;
  308.     }
  309.     public function sp(x:uint, y:uint, value:Boolean):void {    // set pixel
  310.         if (x >= 0 && x < bmp.width && y >= 0 && y < bmp.height) {
  311.             bmp.setPixel(x, y, value ? AC : IC);
  312.             touched = true;
  313.         }
  314.     }
  315.     public function gp(x:uint, y:uint):uint {    // get pixel
  316.         var result:uint;
  317.         if (x >= 0 && x < bmp.width && y >= 0 && y < bmp.height) result = bmp.getPixel(x, y);
  318.         return result;
  319.     }
  320.     public function blit():void {
  321.         graphics.clear();            
  322.         for (var iy:uint = 0; iy < bmp.height; iy++) {
  323.             var ix:uint = 0;
  324.             var w:uint = 0;
  325.             
  326.             while(ix < bmp.width){
  327.                 while (bmp.getPixel(ix + w, iy) && ix + w < bmp.width) w++;
  328.                 if (w == 0) {
  329.                     ix++;
  330.                 } else {                    
  331.                     graphics.beginFill(c);
  332.                     graphics.drawRect(ix * PX - com.x, iy * PX - com.y, w * PX, PX * 1.1);
  333.                     graphics.endFill();
  334.                     ix += w;
  335.                     w = 0;
  336.                 }
  337.             }
  338.         }
  339.     }
  340.     public function cpb(bmpd:BitmapData):BitmapData {    // copy bitmap
  341.         var pixelcount    :uint = bmp.width * bmp.height;
  342.         var ix            :uint = 0;
  343.         var iy            :uint = 0;
  344.         
  345.         for (var i:uint = 0; i < pixelcount; i++) {
  346.             ix = i % bmp.width;
  347.             iy = i / bmp.width;
  348.             bmp.setPixel(ix, iy, bmpd.getPixel(ix, iy) == RC ? AC : IC); // this is horrible, but it saves 6 bytes!
  349.         }
  350.         
  351.         um();
  352.         blit();
  353.         
  354.         return bmp;
  355.     }
  356.     /**
  357.      * Gets the hit coordinates for a given point, the coords sent in 
  358.      * should be in the stage's coordinate space
  359.      * @param    x
  360.      * @param    y
  361.      * @return    the coordinates of the hit voxel
  362.      */
  363.     public function ghc(x:uint, y:uint):Point {
  364.         var hitpos:Point = globalToLocal(new Point(x, y));
  365.         hitpos.x = (hitpos.x + com.x) / PX;
  366.         hitpos.y = (hitpos.y + com.y) / PX;
  367.         return hitpos;
  368.     }
  369.     public function s():Vector.<D> { // s
  370.         var ix:uint = 0;
  371.         var iy:uint = 0;
  372.         var rect:Rectangle;
  373.         var rect2:Rectangle;
  374.         
  375.         var parts:Vector.<D> = new Vector.<D>;
  376.         
  377.         if (alive) {
  378.             for (iy = 0; iy < bmp.height; iy++) {
  379.                 for (ix = 0; ix < bmp.width; ix++) {
  380.                     if (bmp.getPixel(ix, iy) == AC) {
  381.                         //trace("found set pixel at:", ix, iy);
  382.                         //trace("image is:", bmp.width, bmp.height);
  383.                         bmp.floodFill(ix, iy, RC);
  384.                         rect = bmp.getColorBoundsRect(0xffffff, RC, true);
  385.                         
  386.                         // checks if the currently colored segment is the only one in this destructable
  387.                         rect2 = bmp.getColorBoundsRect(0xffffff, AC,  true);
  388.                         
  389.                         // if it is, we skip the entire sting
  390.                         if (rect2.width == 0 && rect2.height == 0 && parts.length == 0) {
  391.                             // color the bitmap back to keep it until next time
  392.                             bmp.floodFill(ix, iy, AC);
  393.                             
  394.                             // only had one segment, escape the loop
  395.                             return null;
  396.                             
  397.                         } else {
  398.                             var nd:D = _s(rect, ix, iy);
  399.                             if (nd) parts.push(nd);
  400.                             ix = rect2.x;
  401.                             iy = rect2.y;
  402.                         }
  403.                     }
  404.                 }
  405.             }
  406.         }
  407.         
  408.         return parts;
  409.     }
  410.     private function _s(rect:Rectangle, firstX:uint, firstY:uint):D {
  411.         //trace("sting region at", rect);
  412.         
  413.         if (!(rect.width + rect.height)) return null;
  414.         
  415.         var nd:D = new D(rect.width, rect.height);
  416.         var pos:Point = localToGlobal(new Point(rect.x * PX - com.x, rect.y * PX - com.y));
  417.         
  418.         nd.x         = pos.x;
  419.         nd.y         = pos.y;
  420.         nd.rotation = rotation;
  421.         nd.vx    = vx * (.9 + Math.random() * .2);
  422.         nd.vy    = vy * (.9 + Math.random() * .2);
  423.         nd.vr    = vr * (.9 + Math.random() * .2);
  424.         
  425.         var bmpData:BitmapData = new BitmapData(rect.width, rect.height);
  426.         bmpData.copyPixels(bmp, rect, new Point);        
  427.         nd.cpb(bmpData);
  428.         bmp.floodFill(firstX, firstY, IC);
  429.         
  430.         return nd;
  431.     }
  432.     private function um():void {    // update mass
  433.         
  434.         var points:uint = 0;
  435.         
  436.         var pixelcount    :uint = bmp.width * bmp.height;
  437.         var ix            :uint = 0;
  438.         var iy            :uint = 0;
  439.         
  440.         for (var i:uint = 0; i < pixelcount; i++) {
  441.             ix = i % bmp.width;
  442.             iy = i / bmp.width;
  443.             if (bmp.getPixel(ix, iy) == AC) {
  444.                 com.x += ix;
  445.                 com.y += iy;
  446.                 points++;
  447.             }
  448.         }
  449.         
  450.         com.x = com.x / points * PX + PX / 2;
  451.         com.y = com.y / points * PX + PX / 2;
  452.         
  453.         x = localToGlobal(com).x;
  454.         y = localToGlobal(com).y;
  455.     }
  456.     
  457.     public function get valid():Boolean {
  458.         return bmp.width > 1 && bmp.height > 1;
  459.     }
  460. }
noswf
Get Adobe Flash Player