Skip to content

Commit

Permalink
Commented ItemEntity.java
Browse files Browse the repository at this point in the history
also added a little thing to spark.java
  • Loading branch information
Davideesk committed Jul 25, 2013
1 parent 658241a commit 4972668
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 55 deletions.
107 changes: 52 additions & 55 deletions src/com/mojang/ld22/entity/ItemEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,79 @@
import com.mojang.ld22.sound.Sound;

public class ItemEntity extends Entity {
private int lifeTime;
protected int walkDist = 0;
protected int dir = 0;
public int hurtTime = 0;
protected int xKnockback, yKnockback;
public double xa, ya, za;
public double xx, yy, zz;
public Item item;
private int time = 0;
private int lifeTime; // the life time of this entity in the level
public double xa, ya, za; // the x, y, and z acceleration
public double xx, yy, zz; // the x, y, and z coordinates
public Item item; // the item that this entity is based off of.
private int time = 0; // time it has lasted in the level

public ItemEntity(Item item, int x, int y) {
this.item = item;
xx = this.x = x;
yy = this.y = y;
xr = 3;
yr = 3;
this.item = item; // assigns the item
xx = this.x = x; // assigns the x coordinate
yy = this.y = y; // assigns the y coordinate
xr = 3; // x radius (size)
yr = 3; // y radius (size)

zz = 2;
xa = random.nextGaussian() * 0.3;
ya = random.nextGaussian() * 0.2;
za = random.nextFloat() * 0.7 + 1;
zz = 2; // z coordinate
xa = random.nextGaussian() * 0.3; // random direction for x acceleration
ya = random.nextGaussian() * 0.2; // random direction for y acceleration
za = random.nextFloat() * 0.7 + 1; // random direction for z acceleration

lifeTime = 60 * 10 + random.nextInt(60);
lifeTime = 60 * 10 + random.nextInt(60); // sets the lifetime of the item. min = 600 ticks, max = 629 ticks.
}

/** Update method, updates (ticks) 60 times a second */
public void tick() {
time++;
if (time >= lifeTime) {
remove();
return;
time++; // increases time by 1
if (time >= lifeTime) { // if the time is larger or equal to lifeTime then...
remove(); // remove from the world
return; // skip the rest of the code
}
xx += xa;
yy += ya;
zz += za;
if (zz < 0) {
zz = 0;
za *= -0.5;
xa *= 0.6;
ya *= 0.6;
xx += xa; // moves the xx coordinate by the x acceleration
yy += ya; // moves the yy coordinate by the y acceleration
zz += za; // moves the zz coordinate by the z acceleration
if (zz < 0) { //if zz is smaller than 0
zz = 0; // zz now will be 0
za *= -0.5; // multiplies the z acceleration by -0.5
xa *= 0.6; // multiplies the x acceleration by 0.6
ya *= 0.6; // multiplies the y acceleration by 0.6
}
za -= 0.15;
int ox = x;
int oy = y;
int nx = (int) xx;
int ny = (int) yy;
int expectedx = nx - x;
int expectedy = ny - y;
move(nx - x, ny - y);
int gotx = x - ox;
int goty = y - oy;
xx += gotx - expectedx;
yy += goty - expectedy;

if (hurtTime > 0) hurtTime--;
za -= 0.15; // minuses the z acceleration by 0.15
int ox = x; // x coordinate
int oy = y; // y coordinate
int nx = (int) xx; // integer conversion of xx
int ny = (int) yy; // integer conversion of yy
int expectedx = nx - x; // the difference of nx and x
int expectedy = ny - y; // the difference of ny and y
move(nx - x, ny - y); // moves the ItemEntity
int gotx = x - ox; // the difference between the new x and ox
int goty = y - oy; // the difference between the new y and oy
xx += gotx - expectedx; // new xx position based on the difference between gotx and expectedx
yy += goty - expectedy; // new yy position based on the difference between goty and expectedy
}

public boolean isBlockableBy(Mob mob) {
return false;
return false; // mobs cannot block this
}

public void render(Screen screen) {
if (time >= lifeTime - 6 * 20) {
if (time / 6 % 2 == 0) return;
/* this first part is for the blinking effect */
if (time >= lifeTime - 6 * 20) { // if time is larger or equal to lifeTime - 6 * 20 then...
if (time / 6 % 2 == 0) return; // if the remainder of (time/6)/2 = 0 then skip the rest of the code.
}
screen.render(x - 4, y - 4, item.getSprite(), Color.get(-1, 0, 0, 0), 0);
screen.render(x - 4, y - 4 - (int) (zz), item.getSprite(), item.getColor(), 0);
screen.render(x - 4, y - 4, item.getSprite(), Color.get(-1, 0, 0, 0), 0); // render the shadow
screen.render(x - 4, y - 4 - (int) (zz), item.getSprite(), item.getColor(), 0); // render the item based on the item's sprite and color
}

protected void touchedBy(Entity entity) {
if (time > 30) entity.touchItem(this);
if (time > 30) entity.touchItem(this); // if time is above 30, it will call the touchItem() method in an entity (Player.java)
}

/** What happens when the player takes the item */
public void take(Player player) {
Sound.pickup.play();
player.score++;
item.onTake(this);
remove();
Sound.pickup.play(); // plays a sound
player.score++; // increase the player's score by 1
item.onTake(this); // calls the onTake() method in Item.java
remove(); // removes this from the world
}
}
1 change: 1 addition & 0 deletions src/com/mojang/ld22/entity/Spark.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public boolean isBlockableBy(Mob mob) {

/** Renders the spark on the screen */
public void render(Screen screen) {
/* this first part is for the blinking effect */
if (time >= lifeTime - 6 * 20) {// if time is larger or equal to lifeTime - 6 * 20 then...
if (time / 6 % 2 == 0) return; // if the remainder of (time/6)/2 = 0 then skip the rest of the code.
}
Expand Down

0 comments on commit 4972668

Please sign in to comment.