/*********************************************** * CS2510 Spring 2011 * Lecture #7.2 * Rockets and World Interactions ***********************************************/ import tester.*; import image.*; import world.*; // Represents a RocketWorld, providing the necessary images // We must extend World, but we leave the implementation // to other sub-classes. abstract class BaseRocket extends World{ // Image of the rocket Image rocket = new FromFile("rocket.png"); // Image of rocket flames Image flame = new FromFile("flame.png"); } // The first iteration of Rocket: Taking off class TakeOff extends BaseRocket{ int h; TakeOff(int h){ this.h = h; } // Draw the rocket into an EmptyScene Scene onDraw(){ return new EmptyScene(200, 500) .placeImage(rocket, 100, 500-this.h); } // Move the rocket up on every Tick TakeOff onTick(){ return new TakeOff(this.h + 10); } } // The second iteration of Rocket: Landing class Landing extends BaseRocket{ int h; Landing(int h){ this.h = h; } // Draw the rocket into an EmptyScene Scene onDraw(){ return new EmptyScene(200, 500) .placeImage(rocket, 100, 500-this.h); } // Move the rocket down until it lands Landing onTick(){ if(this.h <= 50){ return this; }else{ return new Landing(this.h - 10); } } } // The final iteration of Rocket: Land and Taking off class LandAndTakeOff extends BaseRocket{ int h; boolean landing; LandAndTakeOff(int h, boolean landing){ this.h = h; this.landing = landing; } // Draw the rocket, and possibly flames Scene onDraw(){ return this.addFlame(new EmptyScene(200, 500) .placeImage(rocket, 100, 500-this.h)); } // Add the flames to the given Scene if this rocket isn't landing Scene addFlame(Scene scn){ if(this.landing){ return scn; }else{ return scn.placeImage(flame, 100, 550-this.h); } } // On tick, decide which direction to move, and whether or not // we are landing LandAndTakeOff onTick(){ if(this.h <= 50){ return new LandAndTakeOff(51, false); }else{ if(this.landing){ return new LandAndTakeOff(this.h - 10, true); }else{ return new LandAndTakeOff(this.h + 10, false); } } } } // Example of Mouse World interactions class Balloon extends World{ int x; int y; Balloon(int x, int y){ this.x = x; this.y = y; } // Draw this Balloon into the empty scene Scene onDraw(){ return new EmptyScene(400, 400) .placeImage(new Circle(20, "solid", "red"), this.x, this.y); } // Move the new Balloon up into the Sky Balloon onTick(){ return new Balloon(this.x, this.y - 2); } // Place the new Balloon where the Mouse was Pressed Balloon onMouse(int xx, int yy, String me){ if(me.equals("button-down")){ return new Balloon(xx, yy); }else{ return this; } } } // Examples class... Run/instantiated by FunJava class WorldExamples{ // Rocket World Examples //** Uncomment the BigBangs to get them to run BaseRocket toff = new TakeOff(50); //World endTOff = toff.bigBang(); BaseRocket land = new Landing(500); //World endLand = land.bigBang(); BaseRocket landtoff = new LandAndTakeOff(500, true); //World endLTOff = landtoff.bigBang(); // Mouse/Balloon Example World mw = new Balloon(200, 200); //** Uncomment the BigBang to get it to run //World endMw = mw.bigBang(); }