Advertisement
Guest User

Untitled

a guest
Feb 21st, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.  
  4.     import flash.display.Graphics;
  5.     import flash.display.Sprite;
  6.     import flash.events.Event;
  7.     import flash.events.TimerEvent;
  8.     import flash.utils.Timer;
  9.  
  10.     import nape.geom.Vec2;
  11.     import nape.phys.Body;
  12.     import nape.phys.BodyType;
  13.     import nape.phys.Material;
  14.     import nape.shape.Circle;
  15.     import nape.shape.Polygon;
  16.     import nape.space.Broadphase;
  17.     import nape.space.Space;
  18.     import nape.util.ShapeDebug;
  19.  
  20.     [SWF(width=800, height=600, backgroundColor=0xdddddd, frameRate=30)]
  21.     /**
  22.      *
  23.      */
  24.     public class Nape2KinematicBody extends Sprite
  25.     {
  26.         //
  27.         static public const APP_WIDTH:int = 800;
  28.         static public const APP_HEIGHT:int = 600;
  29.  
  30.         //
  31.         private var _space:Space;
  32.         private var _timeStep:Number = 1/30.0;
  33.         private var _velocityIterations:int = 10;
  34.         private var _positionIterations:int = 10;
  35.  
  36.         private var _debug:ShapeDebug;
  37.  
  38.         private var _trampoline:Body;
  39.         private var _conveyorBelt:Body;
  40.         private var _elevator:Body;
  41.  
  42.         private var _elevatorVelocityUp:Vec2 = new Vec2(0, -100);
  43.         private var _elevatorVelocityDown:Vec2 = new Vec2(0, 200);
  44.         private var _elevatorSurfaceVelocity:Vec2 = new Vec2(-200, 0);
  45.         private var _trampolineSurfaceVelocity:Vec2 = new Vec2(-300, 350);
  46.         private var _conveyorBeltVelocity:Vec2 = new Vec2(150, 0);
  47.  
  48.         private var _timer:Timer = new Timer(1000, 0);
  49.         private var _holdOn:Boolean = false;
  50.  
  51.         public function Nape2KinematicBody()
  52.         {
  53.             if (stage) init();
  54.             else addEventListener(Event.ADDED_TO_STAGE, init);
  55.         }
  56.  
  57.         /**
  58.          */
  59.         private function init(event:Event = null):void
  60.         {
  61.             removeEventListener(Event.ADDED_TO_STAGE, init);
  62.  
  63.             _space = new Space(new Vec2(0, 400), Broadphase.SWEEP_AND_PRUNE);
  64.             createBodies();
  65.             initDebugDraw();
  66.  
  67.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  68.         }
  69.  
  70.         /**
  71.          */
  72.         private function createBodies():void
  73.         {
  74.             // Динамические тела
  75.             var cube:Body = new Body(BodyType.DYNAMIC);
  76.             cube.shapes.add(new Polygon(Polygon.box(100, 50)));
  77.  
  78.             var ball:Body = new Body(BodyType.DYNAMIC);
  79.             ball.shapes.add(new Circle(10));
  80.  
  81.             // Кинемат. тело - трамплин
  82.             _trampoline = new Body(BodyType.KINEMATIC);
  83.             _trampoline.shapes.add(new Polygon(Polygon.box(300, 50)));
  84.  
  85.             // Кинемат. тело - конвейер
  86.             _conveyorBelt = new Body(BodyType.KINEMATIC);
  87.             _conveyorBelt.shapes.add(new Polygon(Polygon.box(600, 50)));
  88.  
  89.             // Кинемат. тело - лифт
  90.             _elevator = new Body(BodyType.KINEMATIC);
  91.             _elevator.shapes.add(new Polygon(Polygon.box(300, 50)));
  92.  
  93.             // Статические тела - стены
  94.             var wallLeft:Body = new Body(BodyType.STATIC);
  95.             wallLeft.shapes.add(new Polygon(Polygon.box(50, 400)));
  96.  
  97.             var wallRight:Body = new Body(BodyType.STATIC);
  98.             wallRight.shapes.add(new Polygon(Polygon.box(50, 600)));
  99.  
  100.             // Устанавливаем позицию телам
  101.             _elevator.position.setxy(650, 500);
  102.             _trampoline.position.setxy(350, 250);
  103.             _conveyorBelt.position.setxy(200, 450);
  104.             cube.position.setxy(600, 450);
  105.             ball.position.setxy(550, 450);
  106.             wallLeft.position.setxy(0, 200);
  107.             wallLeft.rotation = -0.45;
  108.             wallRight.position.setxy(800, 300);
  109.  
  110.             // Добавляем тела в мир
  111.             _elevator.space = _space;
  112.             _trampoline.space = _space;
  113.             _conveyorBelt.space = _space;
  114.             cube.space = _space;
  115.             ball.space = _space;
  116.             wallLeft.space = _space;
  117.             wallRight.space = _space;
  118.  
  119.             // Устанавливаем нужные скорости
  120.             _elevator.velocity.set(_elevatorVelocityUp);
  121.             _trampoline.surfaceVel.set(_trampolineSurfaceVelocity);
  122.             _conveyorBelt.kinematicVel.set(_conveyorBeltVelocity);
  123.         }
  124.  
  125.         /**
  126.          */
  127.         private function initDebugDraw():void
  128.         {
  129.             _debug = new ShapeDebug(APP_WIDTH, APP_HEIGHT, 0x000000);
  130.             addChild(_debug.display);
  131.         }
  132.  
  133.         /**
  134.          */
  135.         private function enterFrameHandler(event:Event):void
  136.         {
  137.             _space.step(_timeStep, _velocityIterations, _positionIterations);
  138.  
  139.             _debug.clear();
  140.             _debug.draw(_space);
  141.             _debug.flush();
  142.  
  143.             updateKinematicPosition();
  144.         }
  145.  
  146.         /**
  147.          */
  148.         private function updateKinematicPosition():void
  149.         {
  150.              // Если лифт в ожидании то ничего не проверяем
  151.             if (_holdOn) return;
  152.  
  153.             // если лифт достиг верхней границы то останавливаем его и скидываем с него груз
  154.             if (_elevator.position.y <= 150)
  155.             {
  156.                 _elevator.velocity.setxy(0, 0);
  157.  
  158.                 _elevator.surfaceVel.set(_elevatorSurfaceVelocity);
  159.  
  160.                 _holdOn = true;
  161.                 _timer.addEventListener(TimerEvent.TIMER, throwGoodsComplete);
  162.                 _timer.delay = 3000;
  163.                 _timer.start();
  164.             }
  165.             // если лифт достиг нижней границы то останавливаем его и ждем погрузки
  166.             else if (_elevator.position.y >= 500)
  167.             {
  168.                 _elevator.velocity.setxy(0, 0);
  169.  
  170.                 _holdOn = true;
  171.                 _timer.addEventListener(TimerEvent.TIMER, waitForGoods);
  172.                 _timer.delay = 5000;
  173.                 _timer.start();
  174.             }
  175.         }
  176.  
  177.         /**
  178.          */
  179.         private function throwGoodsComplete(event:TimerEvent):void
  180.         {
  181.             _elevator.velocity.set(_elevatorVelocityDown);
  182.             _elevator.surfaceVel.setxy(0, 0);
  183.  
  184.             _holdOn = false;
  185.             _timer.removeEventListener(TimerEvent.TIMER, throwGoodsComplete);
  186.             _timer.stop();
  187.         }
  188.  
  189.         /**
  190.          */
  191.         private function waitForGoods(event:TimerEvent):void
  192.         {
  193.             _elevator.velocity.set(_elevatorVelocityUp);
  194.  
  195.             _holdOn = false;
  196.             _timer.removeEventListener(TimerEvent.TIMER, waitForGoods);
  197.             _timer.stop();
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement