Skip to content

Commit

Permalink
Merge branch 'rc' into Dz1
Browse files Browse the repository at this point in the history
  • Loading branch information
danielzzhuang committed Jun 11, 2024
2 parents ef9bc6e + b19e547 commit 144d006
Show file tree
Hide file tree
Showing 24 changed files with 174 additions and 108 deletions.
2 changes: 1 addition & 1 deletion Bee.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public abstract class Bee extends Mobs {
public Bee() {
super();
hp = 2;
speed = 1;
speed = 2;
dmg = 1;
}

Expand Down
2 changes: 1 addition & 1 deletion BlueBee.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class BlueBee extends Bee {
public BlueBee() {
super();
direction = 1;
speed = 1;
speed = 2;
range = 300;
}

Expand Down
Empty file modified Button.java
100644 → 100755
Empty file.
29 changes: 18 additions & 11 deletions Coin.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.Actor;
import greenfoot.GreenfootImage;

/**
* Write a description of class Coin here.
*
* @author (your name)
*
* @author (your name)
* @version (a version number or a date)
*/
public class Coin extends Collection
{
public class Coin extends Actor {
private GreenfootImage image;

public Coin(){
image = new GreenfootImage("coin.png");
image.scale(45,55);
setImage(image);
}


/**
* Act - do whatever the Coin wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/

public void act()
{
if (isTouching(Player.class)){
public void act() {
if (isTouching(Player.class)) {
getWorld().removeObject(this);
Level.addToTotalCoin();
}
}


public boolean isBeingTouched() {
return isTouching(Player.class);
}
}
103 changes: 59 additions & 44 deletions Level.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,38 @@
import greenfoot.Greenfoot;
import greenfoot.GreenfootImage;
import greenfoot.World;
import greenfoot.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Level extends World {
protected final ImgScroll scroll;
protected static int totalCoins = 0;
private final int[] worldSize = {2560, 720};
private final String background = "2dPixelForestBackground.png";
private final Font font = new Font("Arial", 18);
private final int level = 0;
protected ImgScroll scroll;
protected Player player;
private Orb orb;
private Font font = new Font("Arial", 18);
protected SuperDisplayLabel coinLabel = new SuperDisplayLabel(Color.BLACK, Color.WHITE, font);
protected static int totalCoins = 0;
protected static int numOfCrown=0;
private Orb orb;

public Level() {
super(1280, 720, 1, false);
scroll = new ImgScroll(this, new GreenfootImage(background), worldSize[0], worldSize[1]);
addObject(coinLabel, 1100, 10);
coinLabel.update("Coins: " + totalCoins);
}

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

public void spawnFloor() {
for (int j = 0; j < scroll.getScrollHeight() - 100; j += 300) {
for (int i = 0; i < scroll.getScrollWidth(); i += 106) {
public void spawnFloor(ImgScroll sc) {
for (int j = 0; j < sc.getScrollHeight() - 100; j += 300) {
for (int i = 0; i < sc.getScrollWidth(); i += 63) {
addObject(new Brick(), i, 700);
}
}
}

/**
* NOTE - Use a 2d array of [40][10] for this to work as intended
* Each value in the array represents 64x and 64y
*/
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 * 64);
}
if (identifier[i][j] == 2) {
addObject(orb = new Orb(), i * 64, j * 64);
}
if (identifier[i][j] == 3) {
addObject(new Coin(), i * 64, j * 64);
}
if (identifier[i][j] == 4) {
addObject(new Crown(), i * 64, j * 64);
}
}
}
}

public void checkNext() {
if (orb.isBeingTouched()) {
Level1 world = new Level1();
Expand All @@ -73,10 +50,48 @@ public int[] getMapBoundary() {
mapBoundary[1] = scroll.getScrollWidth() + scroll.getScrolledX();
return mapBoundary;
}
public static void addCrown(){
numOfCrown++;

public int[][] loadLevel(int level) {
ArrayList<String> data = new ArrayList<String>();
Scanner scan = null;
try {
scan = new Scanner(new File("level" + level + ".csv"));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
while (scan.hasNextLine()) {
data.add(scan.nextLine());
}

int[][] blocks = new int[120][20];
for (String line : data) {
StringTokenizer st = new StringTokenizer(line, ",");
ArrayList<String> lineData = new ArrayList<String>();
while (st.hasMoreTokens()) {
lineData.add(st.nextToken());
}
blocks[Integer.parseInt(lineData.get(0))][Integer.parseInt(lineData.get(1))] = Integer.parseInt(lineData.get(2));
}
return blocks;
}
public static void addToTotalCoin(){
totalCoins++;

/**
* 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++) {
Actor a = switch (identifier[i][j]) {
case 1 -> new Brick();
case 2 -> orb = new Orb();
case 3 -> new Coin();
default -> null;
};
if (a != null) {
addObject(a, i * 64, j * 64);
}
}
}
}
}
34 changes: 11 additions & 23 deletions Level0.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import greenfoot.GreenfootImage;
import greenfoot.*;

/**
* Write a description of class Level0 here.
Expand All @@ -11,44 +10,33 @@ 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);
String background = "2dPixelForestBackground.png";
scroll = new ImgScroll(this, new GreenfootImage(background), worldSize[0], worldSize[1]);
spawnFloor(scroll);
addObject(player = new Player(), 100, 622);
addObject(coinLabel, 1100, 10);
coinLabel.update("Coins: " + totalCoins);

int[][] blockGeneration = new int[40][10];
blockGeneration[10][5] = 1;
blockGeneration[11][5] = 1;
blockGeneration[12][5] = 1;
blockGeneration[28][5] = 1;
blockGeneration[20][6] = 2;
blockGeneration[10][9] = 1;
blockGeneration[5][7] = 1;
blockGeneration[5][8] = 3;
spawnTerrain(blockGeneration);


int[][] mobGeneration = new int[40][10];
int[][] blockGeneration = loadLevel(0);
spawnTerrain(blockGeneration);
addObject(new BlueBee(), 800, 600);
addObject(new RedBee(), 100, 600);
addObject(new Spider(), 750, 600);
addObject(new Mites(), 150, 600);
}

public void act() {
coinLabel.update("Coins: " + totalCoins);
coinLabel.setLocation(getWidth()/2, 20);
scroll.scroll(getWidth() / 2 - player.getX(), getHeight() / 2 - player.getY());
coinLabel.update("Coins: " + totalCoins);
coinLabel.setLocation(getWidth() / 2, 20);
checkNext();
}

public void loadLevel() {
// placeholder if we ever store map data in csv
}
}
26 changes: 5 additions & 21 deletions Level1.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public class Level1 extends Level {
*/
public Level1() {
super();
spawnFloor();
addObject(player = new Player(), 100, 622);
scroll = new ImgScroll(this, new GreenfootImage(background), worldSize[0], worldSize[1]);
spawnFloor(scroll);
addObject(player = new Player(), 100, 622);
addObject(coinLabel, 1100, 10);
coinLabel.update("Coins: " + totalCoins);

// Individual Block Placement
int[][] blockGeneration = new int[40][10];
Expand All @@ -30,25 +32,7 @@ public Level1() {

public void act() {
coinLabel.update("Coins: " + totalCoins);
coinLabel.setLocation(getWidth()/2, 20);
coinLabel.setLocation(getWidth() / 2, 20);
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) {
// 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);
}
}
}
}
}
53 changes: 53 additions & 0 deletions Mites.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import greenfoot.GreenfootImage;
import greenfoot.World;

public class Mites extends Mobs {
private final int hp;
private final int dmg;
private final int speed;
private final int direction = 1;
private final GreenfootImage image;
private Level w;
private int movementAct;
private int jumpAct;

public Mites() {
hp = 2;
dmg = 1;
speed = 2;
image = new GreenfootImage("images/mites.png");
}

public void addedToWorld(World w) {
this.w = (Level) w;
}

public void act() {
movementAct--;
jumpAct--;
movement();
bounceWall();
collision();
fall();
}

private void flipImage() {
image.mirrorHorizontally();
}

private void fall() {
if (getOneObjectAtOffset(0, (getImage().getHeight() / 2) + 1, Brick.class) == null && jumpAct < 15) {
setLocation(getX() + (speed * direction), getY() + 5);
}
}

private void movement() {
if (movementAct < 0 && getOneObjectAtOffset(0, (getImage().getHeight() / 2) + 1, Brick.class) != null) {
jumpAct = 30;
movementAct = 60;
}
if (jumpAct > 15) {
setLocation(getX() + (speed * direction), getY() - 5);
}
}
}
10 changes: 8 additions & 2 deletions Mobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public abstract class Mobs extends SuperSmoothMover {
public World w;
private int hp;
private int speed;
private int speed = 2;
private int dmg;
private int direction = 1;

Expand Down Expand Up @@ -53,11 +53,17 @@ public void collision() {
}
}

protected void bounceWall() {
if (getOneObjectAtOffset(direction * getImage().getWidth() + 1, 0, Brick.class) != null) {
direction *= -1;
}
}

protected void idle() {
if (getWorld() == null) {
return;
}
turnTowards(direction * 99999, getY());
turnTowards(getX() + direction, getY());
if (getOneObjectAtOffset(direction * getImage().getWidth() + 1, 0, Brick.class) != null) {
direction *= -1;
} else {
Expand Down
Loading

0 comments on commit 144d006

Please sign in to comment.