From b889e35dbd1dbfd016ec7d35dd926fda137f551c Mon Sep 17 00:00:00 2001 From: Adrian400811 Date: Mon, 10 Jun 2024 09:10:23 -0400 Subject: [PATCH] Refactored Player class Added Level class Renamed MyWorld to Level0 Updated Mobs --- Bee.java | 15 +--- BlueBee.java | 4 +- BlueSpider.java | 4 +- MyWorld.java => Level.java | 60 +++++---------- Level0.java | 49 ++++++++++++ Level1.java | 60 +++++++-------- Mobs.java | 42 +++++++---- Player.java | 87 +++++++++++++-------- RedBee.java | 16 +--- Spider.java | 15 ++++ project.greenfoot | 150 +++++++++++++++++++++++++------------ 11 files changed, 308 insertions(+), 194 deletions(-) rename MyWorld.java => Level.java (54%) create mode 100755 Level0.java diff --git a/Bee.java b/Bee.java index 9cb3dfc..40a83c8 100755 --- a/Bee.java +++ b/Bee.java @@ -4,7 +4,7 @@ public abstract class Bee extends Mobs { private final int hp; private final int speed; private final int dmg; - private World w; + private Level w; public Bee() { super(); @@ -14,21 +14,12 @@ public Bee() { } public void addedToWorld(World w) { - this.w = w; + this.w = (Level) w; } @Override public void act() { + attack(); collision(); - boundary(); - } - - private void boundary() { - if (w == null) { - } -// ImgScroll bounds = (ImgScroll) getOneIntersectingObject(ImgScroll.class); -// if (bounds == null) { -// w.removeObject(this); -// } } } diff --git a/BlueBee.java b/BlueBee.java index 71d3e27..9a9e64f 100755 --- a/BlueBee.java +++ b/BlueBee.java @@ -11,8 +11,7 @@ public BlueBee() { super(); direction = 1; speed = 1; - range = 5; - movement(); + range = 300; } public void addedToWorld(World w) { @@ -23,6 +22,7 @@ public void addedToWorld(World w) { public void act() { super.act(); movement(); + collision(); } private void movement() { diff --git a/BlueSpider.java b/BlueSpider.java index c51cc62..e05f74c 100755 --- a/BlueSpider.java +++ b/BlueSpider.java @@ -1,6 +1,8 @@ import greenfoot.World; public class BlueSpider extends Spider { + private final double speed = 0.5; + private final int direction = 1; private World w; public BlueSpider() { @@ -13,6 +15,6 @@ public void addedToWorld(World w) { public void act() { super.act(); + super.movement(); } - } diff --git a/MyWorld.java b/Level.java similarity index 54% rename from MyWorld.java rename to Level.java index 90dc9c2..9fd2a66 100755 --- a/MyWorld.java +++ b/Level.java @@ -2,57 +2,28 @@ import greenfoot.GreenfootImage; import greenfoot.World; -/** - * Write a description of class MyWorld here. - * - * @author (your name) - * @version (a version number or a date) - */ -public class MyWorld extends World { - private final ImgScroll scroll; - private final Player player; +public class Level extends World { + protected final ImgScroll scroll; private final int[] worldSize = {2560, 720}; + private final String background = "2dPixelForestBackground.png"; + protected Player player; private Orb orb; - /** - * Constructor for objects of class MyWorld. - */ - public MyWorld() { - // Create a new world with 600x400 cells with a cell size of 1x1 pixels. + public Level() { super(1280, 720, 1, false); - addObject(player = new Player(), 100, 622); - scroll = new ImgScroll(this, new GreenfootImage("2dPixelForestBackground.png"), worldSize[0], worldSize[1]); - // Flooring - for (int j = 0; j < scroll.getScrollHeight() - 100; j += 300) { - for (int i = 0; i < scroll.getScrollWidth(); i += 106) { - addObject(new Brick(), i, 700); - } - } - placeBlock(); - addObject(new BlueBee(), 800, 600); - addObject(new RedBee(), 100, 600); - addObject(new Spider(), 1200, 600); + scroll = new ImgScroll(this, new GreenfootImage(background), worldSize[0], worldSize[1]); } public void act() { scroll.scroll(getWidth() / 2 - player.getX(), getHeight() / 2 - player.getY()); - checkNext(); - } - - public void loadLevel() { - // placeholder if we ever store map data in csv } - private void placeBlock() { - int[][] blockGeneration = new int[40][10]; - blockGeneration[10][5] = 1; - blockGeneration[11][5] = 1; - blockGeneration[12][5] = 1; - blockGeneration[28][5] = 1; - blockGeneration[10][6] = 2; - blockGeneration[10][9] = 1; - blockGeneration[5][7] = 1; - spawnTerrain(blockGeneration); + public void spawnFloor() { + for (int j = 0; j < scroll.getScrollHeight() - 100; j += 300) { + for (int i = 0; i < scroll.getScrollWidth(); i += 106) { + addObject(new Brick(), i, 700); + } + } } /** @@ -83,4 +54,11 @@ public void checkNext() { public int[] getWorldSize() { return worldSize; } + + public int[] getMapBoundary() { + int[] mapBoundary = new int[2]; + mapBoundary[0] = scroll.getScrolledX(); + mapBoundary[1] = scroll.getScrollWidth() + scroll.getScrolledX(); + return mapBoundary; + } } diff --git a/Level0.java b/Level0.java new file mode 100755 index 0000000..9e0d8b2 --- /dev/null +++ b/Level0.java @@ -0,0 +1,49 @@ +import greenfoot.GreenfootImage; + +/** + * Write a description of class Level0 here. + * + * @author (your name) + * @version (a version number or a date) + */ +public class Level0 extends Level { + private final ImgScroll scroll; + private final Player player; + private final int[] worldSize = {2560, 720}; + private final String background = "2dPixelForestBackground.png"; + private Orb orb; + + /** + * Constructor for objects of class Level0. + */ + public Level0() { + super(); + spawnFloor(); + addObject(player = new Player(), 100, 622); + scroll = new ImgScroll(this, new GreenfootImage(background), worldSize[0], worldSize[1]); + + int[][] blockGeneration = new int[40][10]; + blockGeneration[10][5] = 1; + blockGeneration[11][5] = 1; + blockGeneration[12][5] = 1; + blockGeneration[28][5] = 1; + blockGeneration[10][6] = 2; + blockGeneration[10][9] = 1; + blockGeneration[5][7] = 1; + spawnTerrain(blockGeneration); + + int[][] mobGeneration = new int[40][10]; + addObject(new BlueBee(), 800, 600); + addObject(new RedBee(), 100, 600); + addObject(new Spider(), 1200, 600); + } + + public void act() { + scroll.scroll(getWidth() / 2 - player.getX(), getHeight() / 2 - player.getY()); + checkNext(); + } + + public void loadLevel() { + // placeholder if we ever store map data in csv + } +} diff --git a/Level1.java b/Level1.java index 004ac6f..aa49e17 100755 --- a/Level1.java +++ b/Level1.java @@ -1,56 +1,50 @@ -import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) +import greenfoot.GreenfootImage; /** * Write a description of class Level1 here. - * - * @author (your name) + * + * @author (your name) * @version (a version number or a date) */ -public class Level1 extends World -{ - private ImgScroll scroll; - private int[][] blockGeneration; +public class Level1 extends Level { + private final ImgScroll scroll; + private final Player player; + private final int[] worldSize = {2560, 720}; + private final String background = "2dSpaceBackground.png"; private Orb orb; - private Player player; - + /** * Constructor for objects of class Level1. - * */ - public Level1() - { - super(1280, 720, 1, false); + public Level1() { + super(); + spawnFloor(); addObject(player = new Player(), 100, 622); - scroll = new ImgScroll(this, new GreenfootImage("2dSpaceBackground.png"), 2560, 720); - // Flooring - for (int j=0; j w.getWidth()) { - setLocation(w.getWidth(), getY()); - } - if (getY() < 0) { - setLocation(getX(), 0); - } - if (getY() > w.getHeight()) { - setLocation(getX(), w.getHeight()); - } + if (w == null) { + return; + } + if (getX() < 0) { + setLocation(0, getY()); + } + if (getX() > w.getWidth()) { + setLocation(w.getWidth(), getY()); + } + if (getY() < 0) { + setLocation(getX(), 0); + } + if (getY() > w.getHeight()) { + setLocation(getX(), w.getHeight()); } } @@ -49,6 +51,18 @@ public void collision() { } } + protected void idle() { + if (getWorld() == null) { + return; + } + turnTowards(direction * 99999, getY()); + if (getOneObjectAtOffset(direction * getImage().getWidth() + 1, 0, Brick.class) != null) { + direction *= -1; + } else { + move(speed); + } + } + /** * Get distance to a given Player * Inspired by Gevater_Tod4177 on greenfoot.org diff --git a/Player.java b/Player.java index fa41557..8e38564 100755 --- a/Player.java +++ b/Player.java @@ -10,13 +10,18 @@ */ public class Player extends Actor { private static int speed; + private final int dmg; private int hp; private World w; private int jumpActs = 0; + public Player() { + hp = 5; + dmg = 1; + } + public void addedToWorld(World w) { this.w = w; - hp = 10; } /** @@ -25,7 +30,15 @@ public void addedToWorld(World w) { */ public void act() { jumpActs--; - // Movement + movement(); + jump(); + fall(); + collision(); + boundary(); + checkHP(); + } + + private void movement() { if (Greenfoot.isKeyDown("D")) { setLocation(getX() + 8, getY()); speed = 8; @@ -34,51 +47,65 @@ public void act() { setLocation(getX() - 8, getY()); speed = -8; } - // Jump - if (Greenfoot.isKeyDown("Space") && getOneObjectAtOffset(0, (getImage().getHeight()/2)+1, Brick.class) != null){ + } + + private void jump() { + if (Greenfoot.isKeyDown("Space") && getOneObjectAtOffset(0, (getImage().getHeight() / 2) + 1, Brick.class) != null) { jumpActs = 30; } - if (jumpActs>0){ - if (jumpActs > 15){ - setLocation(getX(), getY() - 8); - } + if (jumpActs > 15) { + setLocation(getX(), getY() - 8); } - // Fall - if (getOneObjectAtOffset(0, (getImage().getHeight()/2)+1, Brick.class) == null && jumpActs<15){ - setLocation(getX(), getY()+8); + } + + private void fall() { + if (getOneObjectAtOffset(0, (getImage().getHeight() / 2) + 1, Brick.class) == null && jumpActs < 15) { + setLocation(getX(), getY() + 8); } - // Boundary - if (w != null) { - if (getX() < 0) { - setLocation(0, getY()); - } - if (getX() > w.getWidth()) { - setLocation(w.getWidth(), getY()); - } - if (getY() < 0) { - setLocation(getX(), 0); - } - if (getY() > w.getHeight()) { - setLocation(getX(), w.getHeight()); - } + } + + private void boundary() { + if (w == null) { + return; } - // Collision Detection + if (getX() < 0) { + setLocation(0, getY()); + } + if (getX() > w.getWidth()) { + setLocation(w.getWidth(), getY()); + } + if (getY() < 0) { + setLocation(getX(), 0); + } + if (getY() > w.getHeight()) { + setLocation(getX(), 0); + } + } + + private void collision() { if (getOneObjectAtOffset(getImage().getWidth() / 2, 0, Brick.class) != null) { setLocation(getX() - speed, getY()); } if (getOneObjectAtOffset(-(getImage().getWidth() / 2), 0, Brick.class) != null) { setLocation(getX() - speed, getY()); } - if (getOneObjectAtOffset(0, -(getImage().getHeight()/2), Brick.class) != null){ - setLocation(getX(), getY()+6); + if (getOneObjectAtOffset(0, -(getImage().getHeight() / 2), Brick.class) != null) { + setLocation(getX(), getY() + 6); jumpActs = 0; } - if (getOneObjectAtOffset(0, (getImage().getHeight()/2), Brick.class) != null){ - setLocation(getX(), getY()-1); + if (getOneObjectAtOffset(0, (getImage().getHeight() / 2), Brick.class) != null) { + setLocation(getX(), getY() - 1); jumpActs = 0; } } + private void checkHP() { + if (hp > 0) { + return; + } + w.removeObject(this); + } + public int getSpeed() { return speed; } diff --git a/RedBee.java b/RedBee.java index 3b217bf..53fe734 100755 --- a/RedBee.java +++ b/RedBee.java @@ -1,16 +1,15 @@ public class RedBee extends Bee { private final int range; + private final int direction = 1; private int hp; private int baseSpeed; private int speed; - private int direction; public RedBee() { super(); hp = 2; baseSpeed = 1; range = 100; - direction = 1; } public RedBee(int range) { @@ -21,24 +20,13 @@ public RedBee(int range) { public void act() { super.act(); speed = baseSpeed; - if (getPlayer(range) != null && getDistance(getPlayer(range)) < range) { + if (getPlayer(range) != null) { sprint(); } else { idle(); } } - private void idle() { - if (getWorld() == null) { - return; - } - turnTowards(direction * 999, getY()); - if (getOneObjectAtOffset(direction * getImage().getWidth(), 0, Brick.class) != null) { - direction *= -1; - } else { - move(speed); - } - } /** * Move towards the nearest Player using getPlayer() diff --git a/Spider.java b/Spider.java index 8fe2bd4..8437f55 100755 --- a/Spider.java +++ b/Spider.java @@ -7,6 +7,7 @@ public class Spider extends Mobs { private World w; private long startTime; private boolean oob = false; + private int direction = 1; public Spider() { hp = 2; @@ -19,9 +20,23 @@ public void addedToWorld(World w) { } public void act() { + movement(); + attack(); timeout(); } + protected void movement() { + if (getWorld() == null) { + return; + } + turnTowards(direction * 999, getY()); + if (getOneObjectAtOffset(direction * getImage().getWidth(), 0, Brick.class) != null) { + direction *= -1; + } else { + move(speed); + } + } + public void timeout() { if (w == null) { return; diff --git a/project.greenfoot b/project.greenfoot index 62d8979..1e207e5 100755 --- a/project.greenfoot +++ b/project.greenfoot @@ -1,63 +1,84 @@ #Greenfoot project file class.Brick.image=brick.jpg -class.MyWorld.image=2dPixelForestBackground\ (1).png +class.Level0.image=2dPixelForestBackground\ (1).png class.Orb.image=ball.png class.Player.image=ppl1.png -dependency1.from=Level1 +dependency1.from=Level dependency1.to=ImgScroll dependency1.type=UsesDependency -dependency10.from=MyWorld -dependency10.to=Level1 +dependency10.from=Level0 +dependency10.to=RedBee dependency10.type=UsesDependency -dependency11.from=Player -dependency11.to=Brick +dependency11.from=Level0 +dependency11.to=Spider dependency11.type=UsesDependency -dependency12.from=Mobs -dependency12.to=Player +dependency12.from=Level1 +dependency12.to=ImgScroll dependency12.type=UsesDependency -dependency13.from=Mobs -dependency13.to=Brick +dependency13.from=Level1 +dependency13.to=Player dependency13.type=UsesDependency -dependency14.from=RedBee -dependency14.to=Player +dependency14.from=Level1 +dependency14.to=Orb dependency14.type=UsesDependency -dependency15.from=RedBee +dependency15.from=Level1 dependency15.to=Brick dependency15.type=UsesDependency -dependency16.from=Orb -dependency16.to=Player +dependency16.from=Player +dependency16.to=Brick dependency16.type=UsesDependency -dependency2.from=Level1 -dependency2.to=Orb +dependency17.from=Mobs +dependency17.to=Player +dependency17.type=UsesDependency +dependency18.from=Mobs +dependency18.to=Brick +dependency18.type=UsesDependency +dependency19.from=Bee +dependency19.to=Level +dependency19.type=UsesDependency +dependency2.from=Level +dependency2.to=Player dependency2.type=UsesDependency -dependency3.from=Level1 -dependency3.to=Player +dependency20.from=RedBee +dependency20.to=Player +dependency20.type=UsesDependency +dependency21.from=RedBee +dependency21.to=Brick +dependency21.type=UsesDependency +dependency22.from=RedSpider +dependency22.to=Player +dependency22.type=UsesDependency +dependency23.from=Orb +dependency23.to=Player +dependency23.type=UsesDependency +dependency3.from=Level +dependency3.to=Orb dependency3.type=UsesDependency -dependency4.from=Level1 +dependency4.from=Level dependency4.to=Brick dependency4.type=UsesDependency -dependency5.from=MyWorld -dependency5.to=ImgScroll +dependency5.from=Level +dependency5.to=Level1 dependency5.type=UsesDependency -dependency6.from=MyWorld -dependency6.to=Player +dependency6.from=Level0 +dependency6.to=ImgScroll dependency6.type=UsesDependency -dependency7.from=MyWorld -dependency7.to=Orb +dependency7.from=Level0 +dependency7.to=Player dependency7.type=UsesDependency -dependency8.from=MyWorld -dependency8.to=Brick +dependency8.from=Level0 +dependency8.to=Orb dependency8.type=UsesDependency -dependency9.from=MyWorld -dependency9.to=RedBee +dependency9.from=Level0 +dependency9.to=BlueBee dependency9.type=UsesDependency -editor.fx.0.height=733 -editor.fx.0.width=810 -editor.fx.0.x=1424 -editor.fx.0.y=423 +editor.fx.0.height=0 +editor.fx.0.width=0 +editor.fx.0.x=0 +editor.fx.0.y=0 height=1042 -package.numDependencies=16 -package.numTargets=13 +package.numDependencies=23 +package.numTargets=18 project.charset=UTF-8 publish.hasSource=false publish.locked=true @@ -80,33 +101,68 @@ target1.width=120 target1.x=0 target1.y=0 target10.height=70 -target10.name=RedBee +target10.name=Mobs target10.showInterface=false -target10.type=ClassTarget +target10.type=AbstractTarget target10.width=120 target10.x=0 target10.y=0 target11.height=70 -target11.name=Level1 +target11.name=Spider target11.showInterface=false target11.type=ClassTarget target11.width=120 target11.x=0 target11.y=0 target12.height=70 -target12.name=MyWorld +target12.name=RedSpider target12.showInterface=false target12.type=ClassTarget target12.width=120 target12.x=0 target12.y=0 target13.height=70 -target13.name=Orb +target13.name=Level target13.showInterface=false target13.type=ClassTarget target13.width=120 target13.x=0 target13.y=0 +target14.height=70 +target14.name=Level0 +target14.showInterface=false +target14.type=ClassTarget +target14.width=120 +target14.x=0 +target14.y=0 +target15.height=70 +target15.name=RedBee +target15.showInterface=false +target15.type=ClassTarget +target15.width=120 +target15.x=0 +target15.y=0 +target16.height=70 +target16.name=Level1 +target16.showInterface=false +target16.type=ClassTarget +target16.width=120 +target16.x=0 +target16.y=0 +target17.height=70 +target17.name=Orb +target17.showInterface=false +target17.type=ClassTarget +target17.width=120 +target17.x=0 +target17.y=0 +target18.height=70 +target18.name=MyWorld +target18.showInterface=false +target18.type=ClassTarget +target18.width=120 +target18.x=0 +target18.y=0 target2.height=70 target2.name=Player target2.showInterface=false @@ -129,14 +185,14 @@ target4.width=150 target4.x=0 target4.y=0 target5.height=70 -target5.name=Level1 +target5.name=Tile target5.showInterface=false target5.type=ClassTarget target5.width=120 target5.x=0 target5.y=0 target6.height=70 -target6.name=MyWorld +target6.name=GreenBee target6.showInterface=false target6.type=ClassTarget target6.width=120 @@ -150,21 +206,21 @@ target7.width=120 target7.x=0 target7.y=0 target8.height=70 -target8.name=Brick +target8.name=BlueSpider target8.showInterface=false target8.type=ClassTarget target8.width=120 target8.x=0 target8.y=0 target9.height=70 -target9.name=Mobs +target9.name=Brick target9.showInterface=false -target9.type=AbstractTarget +target9.type=ClassTarget target9.width=120 target9.x=0 target9.y=0 version=3.1.0 width=1920 -world.lastInstantiated=MyWorld +world.lastInstantiated=Level0 xPosition=864 yPosition=317