Im making a simulation and I want all of my actors under the Vehicle superclass to speed up when a "Blood Moon" event occurs but idk why my codes not speeding them up.
code relating to Blood Moon in World class:
Effect Code (Superclass)
BloodMoon class (subclass under Effect)
Vehicle class code:
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | public class VehicleWorld extends World { private GreenfootImage background; // Color Constants public static Color GREY_BORDER = new Color ( 108 , 108 , 108 ); public static Color GREY_STREET = new Color ( 88 , 88 , 88 ); public static Color YELLOW_LINE = new Color ( 255 , 216 , 0 ); public static Color GREEN_STREET = new Color ( 114 , 135 , 0 ); public static Color BROWN_LINE = new Color ( 101 , 67 , 33 ); public static Color BROWN_BORDER = new Color ( 101 , 67 , 33 ); // Instance variables / Objects private boolean twoWayTraffic, splitAtCenter; private int laneHeight, laneCount, spaceBetweenLanes; private int [] lanePositionsY; private VehicleSpawner[] laneSpawners; //Static Variables private static boolean bloodMooning; /** * Constructor for objects of class MyWorld. * */ public VehicleWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 800 , 600 , 1 , false ); setPaintOrder (Villager. class , EvilWitch. class , GoodWitch. class , Bus. class , Car. class , Ambulance. class , Prowler. class ); bloodMooning = false ; // set up background GreenfootImage background = new GreenfootImage( "background.png" ); background.scale(getWidth(), getHeight()); setBackground(background); // Set critical variables laneCount = 6 ; laneHeight = 48 ; spaceBetweenLanes = 6 ; splitAtCenter = false ; twoWayTraffic = false ; setActOrder(Pedestrian. class ); // Init lane spawner objects laneSpawners = new VehicleSpawner[laneCount]; // Prepare lanes method - draws the lanes lanePositionsY = prepareLanes ( this , background, laneSpawners, 222 , laneHeight, laneCount, spaceBetweenLanes, twoWayTraffic, splitAtCenter); } public void act () { spawn(); } private void spawn () { // Chance to spawn a vehicle if (Greenfoot.getRandomNumber ( 60 ) == 0 ){ int lane = Greenfoot.getRandomNumber(laneCount); if (!laneSpawners[lane].isTouchingVehicle()){ int vehicleType = Greenfoot.getRandomNumber( 5 ); if (vehicleType == 0 ){ addObject( new Car(laneSpawners[lane]), 0 , 0 ); } else if (vehicleType == 1 ){ addObject( new Bus(laneSpawners[lane]), 0 , 0 ); } else if (vehicleType == 2 ){ addObject( new Ambulance(laneSpawners[lane]), 0 , 0 ); } else if (vehicleType == 3 ){ addObject( new Prowler(laneSpawners[lane]), 0 , 0 ); } else if (vehicleType == 4 ){ addObject( new RootedCorpse(laneSpawners[lane]), 0 , 0 ); } } } // Chance to spawn a Pedestrian if (Greenfoot.getRandomNumber ( 80 ) == 0 ){ int xSpawnLocation = Greenfoot.getRandomNumber ( 600 ) + 100 ; // random between 99 and 699, so not near edges boolean spawnAtTop = true ; //Greenfoot.getRandomNumber(2) == 0 ? true : false; if (spawnAtTop){ addObject ( new Villager ( 1 ), xSpawnLocation, 50 ); } else { addObject ( new Villager (- 1 ), xSpawnLocation, 550 ); } } else if (Greenfoot.getRandomNumber ( 160 ) == 0 ){ int xSpawnLocation = Greenfoot.getRandomNumber ( 600 ) + 100 ; // random between 99 and 699, so not near edges boolean spawnAtTop = true ; //Greenfoot.getRandomNumber(2) == 0 ? true : false; if (spawnAtTop){ addObject ( new EvilWitch ( 1 ), xSpawnLocation, 50 ); } else { addObject ( new EvilWitch (- 1 ), xSpawnLocation, 550 ); } } else if (Greenfoot.getRandomNumber ( 160 ) == 0 ){ int xSpawnLocation = Greenfoot.getRandomNumber ( 600 ) + 100 ; // random between 99 and 699, so not near edges boolean spawnAtTop = true ; //Greenfoot.getRandomNumber(2) == 0 ? true : false; if (spawnAtTop){ addObject ( new GoodWitch ( 1 ), xSpawnLocation, 50 ); } else { addObject ( new GoodWitch (- 1 ), xSpawnLocation, 550 ); } } //BloodMoon Stuff if (!bloodMooning && Greenfoot.getRandomNumber( 240 ) == 0 ){ addObject ( new BloodMoon( 240 ), 400 , 300 ); bloodMooning = true ; } if (bloodMooning && getObjects(BloodMoon. class ).size() == 0 ){ bloodMooning = false ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class effect here. * * @author (your name) * @version (a version number or a date) */ public class Effect extends Actor { protected GreenfootImage image; protected void fade ( int timeLeft, int fadeTime){ double percent = timeLeft / ( double )fadeTime; int newTransparency = ( int )(percent * 255 ); image.setTransparency (newTransparency); } } |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.ArrayList; /** * Write a description of class BloodMoon here. * * @author (your name) * @version (a version number or a date) */ public class BloodMoon extends Effect { private int duration; public BloodMoon ( int duration){ this .duration = duration; } public void addedToWorld (World w){ image = drawSnow (w.getWidth(), w.getHeight(), 200 ); setImage(image); ArrayList<Vehicle> vehicles = (ArrayList<Vehicle>) w.getObjects(Vehicle. class ); for (Vehicle v : vehicles){ v.speedUp(); } } /** * Act - do whatever the BloodMoon wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (duration == 0 ){ getWorld().removeObject( this ); } else if (duration <= 180 ){ fade (duration, 180 ); } duration--; } /** * density should be 1-100. 100 will be almost completely white */ public static GreenfootImage drawSnow ( int width, int height, int density){ Color[] swatch = new Color [ 64 ]; int red = 128 ; int blue = 192 ; for ( int i = 0 ; i < swatch.length/ 2 ; i++){ swatch[i] = new Color ( 255 , 0 , 0 ); } for ( int i = swatch.length/ 2 ; i < swatch.length; i++){ swatch[i] = new Color ( 255 , 0 , 0 ); } // The temporary image, my canvas for drawing GreenfootImage temp = new GreenfootImage (width, height); //temp.setColor (Color.BLACK); //temp.fill(); // Run this loop one time per "density" for ( int i = 0 ; i < density; i++){ for ( int j = 0 ; j < 150 ; j++){ // draw 100 circles int randSize; // Choose a random colour from my swatch, and set its tranparency randomly int randColor = Greenfoot.getRandomNumber(swatch.length);; int randTrans = Greenfoot.getRandomNumber( 220 ) + 35 ; // around half transparent temp.setColor (swatch[randColor]); //setTransparency(randTrans); // random locations for our dot int randX = Greenfoot.getRandomNumber (width); int randY = Greenfoot.getRandomNumber (height); int tempVal = Greenfoot.getRandomNumber( 250 ); if (tempVal >= 1 ){ //randSize = 2; temp.drawRect (randX, randY, 0 , 0 ); } else { randSize = Greenfoot.getRandomNumber ( 2 ) + 2 ; temp.fillOval (randX, randY, randSize, randSize); } // silly way to draw a dot.. } } return temp; } } |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * This is the superclass for Vehicles. * */ public abstract class Vehicle extends SuperSmoothMover { protected double maxSpeed; protected double speed; protected int direction; // 1 = right, -1 = left protected boolean moving; protected int yOffset; protected VehicleSpawner origin; protected abstract boolean checkHitPedestrian (); public Vehicle (VehicleSpawner origin) { this .origin = origin; moving = true ; if (origin.facesRightward()){ direction = 1 ; } else { direction = - 1 ; getImage().mirrorHorizontally(); } } public void addedToWorld (World w){ setLocation (origin.getX() - (direction * 100 ), origin.getY() - yOffset); } /** * A method used by all Vehicles to check if they are at the edge. * * Note that this World is set to unbounded (The World's super class is (int, int, int, FALSE) which means * that objects should not be stopped from leaving the World. However, this introduces a challenge as there * is the potential for objects to disappear off-screen but still be fully acting and thus wasting resources * and affecting the simulation even though they are not visible. */ protected boolean checkEdge() { if (direction == 1 ) { // if moving right, check 200 pixels to the right (above max X) if (getX() > getWorld().getWidth() + 200 ){ return true ; } } else { // if moving left, check 200 pixels to the left (negative values) if (getX() < - 200 ){ return true ; } } return false ; } /** * Method that deals with movement. Speed can be set by individual subclasses in their constructors */ public void drive() { boolean bloodMooning = VehicleWorld.isBloodMooning(); // Ahead is a generic vehicle - we don't know what type BUT // since every Vehicle "promises" to have a getSpeed() method, // we can call that on any vehicle to find out it's speed Vehicle ahead = (Vehicle) getOneObjectAtOffset (direction * ( int )(speed + getImage().getWidth()/ 2 + 4 ), 0 , Vehicle. class ); if (ahead == null ) { speed = maxSpeed; } else { speed = ahead.getSpeed(); } move (speed * direction); // what I need help on if (bloodMooning) { speed = maxSpeed* 3 ; } } /** * An accessor that can be used to get this Vehicle's speed. Used, for example, when a vehicle wants to see * if a faster vehicle is ahead in the lane. */ public double getSpeed(){ return speed; } public void speedUp () { speed = maxSpeed* 3 ; } } |