1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | //1. Module name module laserbolt; //2. Imports import arc.scenegraph.all, arc.physics.shapes.box, arc.sound, arc.texture, arc.types; import spacearea; //3. A laser bolt, fired from the player ship. class LaserBolt : arc.scenegraph.sprite.Sprite { public: //4. loads the graphics for the bolt static void loadResources() { // Frame is derived from MultiParentNode, so we will only need // to have one instance, which will serve to display the bolt // for all instances of LaserBolt LaserBolt.beamFrame = new Frame(Texture("astbin/ship/laser.png")); } //5. creates a new laserbolt at a given position, rotation // and adds it to the spacearea node. static LaserBolt addLaser(inout Point translation, Radians rotation) { LaserBolt bolt = new LaserBolt(translation, rotation); area.addChild(bolt); return bolt; } private: //6. this is serves as a callback to the physics collision handler // it is registered in the constructor and will be called whenever // there's a new collision between this object and another body // // if a laserbolt hits something, remove it void onCollideStart(Body a, Body b) { if(this.parent is area) area.removeChild(this); } //7. new laser at position and angle this(inout Point translation, Radians rotation) { // LaserBolt derives from Sprite and can call Sprite's // constructor with a Transform and a 'content' node. // Since we want the laser bolt to collide with objects, we // use one of the physics nodes as a transform: physics = new Box(beamFrame.frame.size, 10); physics.sigCollideStart.connect(&onCollideStart); physics.translation = translation; physics.rotation = rotation; physics.velocity = Point.fromPolar(0.5, rotation - PI/2); // the 'content' will just be the previously constructed // beamFrame - it just draws the beam super(physics, beamFrame); } private: //8. Laser bolt vars Body physics; static Frame beamFrame; } |