Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.Graphics;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.TimerEvent;
- import flash.utils.Timer;
- import nape.geom.Vec2;
- import nape.phys.Body;
- import nape.phys.BodyType;
- import nape.phys.Material;
- import nape.shape.Circle;
- import nape.shape.Polygon;
- import nape.space.Broadphase;
- import nape.space.Space;
- import nape.util.ShapeDebug;
- [SWF(width=800, height=600, backgroundColor=0xdddddd, frameRate=30)]
- /**
- *
- */
- public class Nape2KinematicBody extends Sprite
- {
- //
- static public const APP_WIDTH:int = 800;
- static public const APP_HEIGHT:int = 600;
- //
- private var _space:Space;
- private var _timeStep:Number = 1/30.0;
- private var _velocityIterations:int = 10;
- private var _positionIterations:int = 10;
- private var _debug:ShapeDebug;
- private var _trampoline:Body;
- private var _conveyorBelt:Body;
- private var _elevator:Body;
- private var _elevatorVelocityUp:Vec2 = new Vec2(0, -100);
- private var _elevatorVelocityDown:Vec2 = new Vec2(0, 200);
- private var _elevatorSurfaceVelocity:Vec2 = new Vec2(-200, 0);
- private var _trampolineSurfaceVelocity:Vec2 = new Vec2(-300, 350);
- private var _conveyorBeltVelocity:Vec2 = new Vec2(150, 0);
- private var _timer:Timer = new Timer(1000, 0);
- private var _holdOn:Boolean = false;
- public function Nape2KinematicBody()
- {
- if (stage) init();
- else addEventListener(Event.ADDED_TO_STAGE, init);
- }
- /**
- */
- private function init(event:Event = null):void
- {
- removeEventListener(Event.ADDED_TO_STAGE, init);
- _space = new Space(new Vec2(0, 400), Broadphase.SWEEP_AND_PRUNE);
- createBodies();
- initDebugDraw();
- addEventListener(Event.ENTER_FRAME, enterFrameHandler);
- }
- /**
- */
- private function createBodies():void
- {
- // Динамические тела
- var cube:Body = new Body(BodyType.DYNAMIC);
- cube.shapes.add(new Polygon(Polygon.box(100, 50)));
- var ball:Body = new Body(BodyType.DYNAMIC);
- ball.shapes.add(new Circle(10));
- // Кинемат. тело - трамплин
- _trampoline = new Body(BodyType.KINEMATIC);
- _trampoline.shapes.add(new Polygon(Polygon.box(300, 50)));
- // Кинемат. тело - конвейер
- _conveyorBelt = new Body(BodyType.KINEMATIC);
- _conveyorBelt.shapes.add(new Polygon(Polygon.box(600, 50)));
- // Кинемат. тело - лифт
- _elevator = new Body(BodyType.KINEMATIC);
- _elevator.shapes.add(new Polygon(Polygon.box(300, 50)));
- // Статические тела - стены
- var wallLeft:Body = new Body(BodyType.STATIC);
- wallLeft.shapes.add(new Polygon(Polygon.box(50, 400)));
- var wallRight:Body = new Body(BodyType.STATIC);
- wallRight.shapes.add(new Polygon(Polygon.box(50, 600)));
- // Устанавливаем позицию телам
- _elevator.position.setxy(650, 500);
- _trampoline.position.setxy(350, 250);
- _conveyorBelt.position.setxy(200, 450);
- cube.position.setxy(600, 450);
- ball.position.setxy(550, 450);
- wallLeft.position.setxy(0, 200);
- wallLeft.rotation = -0.45;
- wallRight.position.setxy(800, 300);
- // Добавляем тела в мир
- _elevator.space = _space;
- _trampoline.space = _space;
- _conveyorBelt.space = _space;
- cube.space = _space;
- ball.space = _space;
- wallLeft.space = _space;
- wallRight.space = _space;
- // Устанавливаем нужные скорости
- _elevator.velocity.set(_elevatorVelocityUp);
- _trampoline.surfaceVel.set(_trampolineSurfaceVelocity);
- _conveyorBelt.kinematicVel.set(_conveyorBeltVelocity);
- }
- /**
- */
- private function initDebugDraw():void
- {
- _debug = new ShapeDebug(APP_WIDTH, APP_HEIGHT, 0x000000);
- addChild(_debug.display);
- }
- /**
- */
- private function enterFrameHandler(event:Event):void
- {
- _space.step(_timeStep, _velocityIterations, _positionIterations);
- _debug.clear();
- _debug.draw(_space);
- _debug.flush();
- updateKinematicPosition();
- }
- /**
- */
- private function updateKinematicPosition():void
- {
- // Если лифт в ожидании то ничего не проверяем
- if (_holdOn) return;
- // если лифт достиг верхней границы то останавливаем его и скидываем с него груз
- if (_elevator.position.y <= 150)
- {
- _elevator.velocity.setxy(0, 0);
- _elevator.surfaceVel.set(_elevatorSurfaceVelocity);
- _holdOn = true;
- _timer.addEventListener(TimerEvent.TIMER, throwGoodsComplete);
- _timer.delay = 3000;
- _timer.start();
- }
- // если лифт достиг нижней границы то останавливаем его и ждем погрузки
- else if (_elevator.position.y >= 500)
- {
- _elevator.velocity.setxy(0, 0);
- _holdOn = true;
- _timer.addEventListener(TimerEvent.TIMER, waitForGoods);
- _timer.delay = 5000;
- _timer.start();
- }
- }
- /**
- */
- private function throwGoodsComplete(event:TimerEvent):void
- {
- _elevator.velocity.set(_elevatorVelocityDown);
- _elevator.surfaceVel.setxy(0, 0);
- _holdOn = false;
- _timer.removeEventListener(TimerEvent.TIMER, throwGoodsComplete);
- _timer.stop();
- }
- /**
- */
- private function waitForGoods(event:TimerEvent):void
- {
- _elevator.velocity.set(_elevatorVelocityUp);
- _holdOn = false;
- _timer.removeEventListener(TimerEvent.TIMER, waitForGoods);
- _timer.stop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement