/** * This class will generate a map containing worms * rocks, an otter and a crab * * @author Derek Peacock * @version 1.0 */ public class Sandscape { // instance variables - replace the example below with your own private SandWorld world; private Crab scott; private Otter otter; private byte noWorms; /** * Constructor for objects of class Sandscape */ public Sandscape(SandWorld world) { this.world = world; } private void addActors(String [] rows) { byte rowNo = 0; int width = world.getWidth() / rows[0].length(); int height = world.getHeight() / rows.length; for(String row : rows) { for(byte pos = 0; pos < row.length(); pos++) { char c = row.charAt(pos); int x = (pos + 1) * width - width / 2; int y = (rowNo + 1) * height - height / 2; if(c == 'C') { scott = new Crab(); world.addObject(scott, x, y); } else if(c == '#') { Rock rock = new Rock(); world.addObject(rock, x, y); } else if(c == '$') { Worm worm = new Worm(); world.addObject(worm, x, y); noWorms++; } else if(c == 'O') { otter = new Otter(); world.addObject(otter, x, y); } } rowNo++; } } public Crab getCrab() { return scott; } public Otter getOtter() { return otter; } public int getNoWorms() { return noWorms; } /** * Define a map containing one otter (O) * one Crab (C), and a number of Rocks (#) * and worms ($). Each cell is 60 pixels * added to a world 960 x 720 pixels */ public void setupLevel1() { String [] rows = { "................", "..O.............", "................", ".............$..", "................", "................", "...##...........", "........$.......", "..$.........#...", "....$.....####..", "............C...", "....$...........", }; addActors(rows); } /** * Define a map containing one otter (O) * one Crab (C), and a number of Rocks (#) * and worms ($). Each cell is 60 pixels * added to a world 960 x 720 pixels */ public void setupLevel2() { String [] rows = { "............O...", "................", "................", ".$...........$..", "................", "...........$....", ".##......##.....", "........$.......", "..$.............", "....C.....$$....", "................", "....$...........", }; addActors(rows); } /** * Define a map containing one otter (O) * one Crab (C), and a number of Rocks (#) * and worms ($). Each cell is 60 pixels * added to a world 960 x 720 pixels */ public void setupQuickLevel() { String [] rows = { "................", "..O.............", "................", "................", "................", "................", "...##...........", "................", "............#...", "..........####..", ".........$..C...", "................", }; addActors(rows); } }