Skip to content

Commit

Permalink
Refactored Player class
Browse files Browse the repository at this point in the history
Added Level class
Renamed MyWorld to Level0
Updated Mobs
  • Loading branch information
reihoron committed Jun 10, 2024
1 parent b763c41 commit b889e35
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 194 deletions.
15 changes: 3 additions & 12 deletions Bee.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
// }
}
}
4 changes: 2 additions & 2 deletions BlueBee.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public BlueBee() {
super();
direction = 1;
speed = 1;
range = 5;
movement();
range = 300;
}

public void addedToWorld(World w) {
Expand All @@ -23,6 +22,7 @@ public void addedToWorld(World w) {
public void act() {
super.act();
movement();
collision();
}

private void movement() {
Expand Down
4 changes: 3 additions & 1 deletion BlueSpider.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -13,6 +15,6 @@ public void addedToWorld(World w) {

public void act() {
super.act();
super.movement();
}

}
60 changes: 19 additions & 41 deletions MyWorld.java → Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

/**
Expand Down Expand Up @@ -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;
}
}
49 changes: 49 additions & 0 deletions Level0.java
Original file line number Diff line number Diff line change
@@ -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
}
}
60 changes: 27 additions & 33 deletions Level1.java
Original file line number Diff line number Diff line change
@@ -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<scroll.getScrollHeight()-100; j+=300){
for (int i=0; i<scroll.getScrollWidth(); i+=106){
addObject(new Brick(), 0+i, 700);
}
}
scroll = new ImgScroll(this, new GreenfootImage(background), worldSize[0], worldSize[1]);

// Individual Block Placement
blockGeneration = new int[40][10];
int[][] blockGeneration = new int[40][10];
blockGeneration[2][5] = 1;
spawnTerrain(blockGeneration);
}
public void act(){
scroll.scroll(getWidth()/2-player.getX(), getHeight()/2-player.getY());

public void act() {
scroll.scroll(getWidth() / 2 - player.getX(), getHeight() / 2 - player.getY());
}

/**
* NOTE - Use a 2d array of [40][10] for this to work as intended
* Each value in the array represents 64x and 72y
*/
public void spawnTerrain(int[][] identifier){
for (int i = 0; i < identifier.length; i++){
for (int j = 0; j < identifier[i].length; j++){
if (identifier[i][j] == 1){
public void spawnTerrain(int[][] identifier) {
for (int i = 0; i < identifier.length; i++) {
for (int j = 0; j < identifier[i].length; j++) {
if (identifier[i][j] == 1) {
// i represents the X-values and j represents the y-values
addObject(new Brick(), i*64, j*72);
}
if (identifier[i][j] == 2){
addObject(orb = new Orb(), i*64, j*72);
addObject(new Brick(), i * 64, j * 72);
}
if (identifier[i][j] == 2) {
addObject(orb = new Orb(), i * 64, j * 72);
}
}
}
Expand Down
42 changes: 28 additions & 14 deletions Mobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ public abstract class Mobs extends SuperSmoothMover {
private int hp;
private int speed;
private int dmg;
private int direction = 1;

public Mobs() {

enableStaticRotation();
}

public void addedToWorld(World w) {
Expand All @@ -24,19 +25,20 @@ private void gravity() {
}

private void 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());
}
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());
}
}

Expand All @@ -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
Expand Down
Loading

0 comments on commit b889e35

Please sign in to comment.