Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Add Part 1 of Passive Entities.
Browse files Browse the repository at this point in the history
Adds GlowBat, GlowChicken, GlowHorse, GlowPig, GlowRabbit, GlowSheep, GlowTameable,
GlowAgeable, GlowAmbient, GlowAnimal, BoundingBox, and EntityBoundingBox.

This is part of a larger feature addition to Glowstone to implementing all entities
from the Bukkit API.

This depends on GlowstoneMC/Glowkit#31
  • Loading branch information
gabizou committed Jan 4, 2015
1 parent 0f93dfa commit 60db527
Show file tree
Hide file tree
Showing 17 changed files with 824 additions and 6 deletions.
121 changes: 121 additions & 0 deletions src/main/java/net/glowstone/entity/GlowAgeable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package net.glowstone.entity;

import com.flowpowered.networking.Message;
import net.glowstone.entity.meta.MetadataIndex;
import net.glowstone.entity.meta.MetadataMap;
import net.glowstone.net.message.play.entity.EntityMetadataMessage;
import org.bukkit.Location;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.EntityType;

import java.util.List;

/**
* Represents a creature that ages, such as a sheep.
*/
public class GlowAgeable extends GlowCreature implements Ageable {

private static final int AGE_BABY = -24000;
private static final int AGE_ADULT = 0;
private static final int BREEDING_AGE = 6000;
protected float width, height;
private int age = 0;
private boolean ageLocked = false;

/**
* Creates a new ageable monster.
* @param location The location of the monster.
* @param type The type of monster.
*/
public GlowAgeable(Location location, EntityType type) {
super(location, type);
}

@Override
public void pulse() {
super.pulse();
if (this.ageLocked) {
setScaleForAge(!isAdult());
} else {
int currentAge = this.age;
if (currentAge < AGE_ADULT) {
currentAge++;
setAge(currentAge);
} else if (currentAge > AGE_ADULT) {
currentAge--;
setAge(currentAge);
}
}
}

@Override
public final int getAge() {
return this.age;
}

@Override
public final void setAge(int age) {
this.age = age;
this.setScaleForAge(isAdult());
}

@Override
public final boolean getAgeLock() {
return this.ageLocked;
}

@Override
public final void setAgeLock(boolean ageLocked) {
this.ageLocked = ageLocked;
}

@Override
public final void setBaby() {
if (isAdult()) {
setAge(AGE_BABY);
}
}

@Override
public final void setAdult() {
if (!isAdult()) {
setAge(AGE_ADULT);
}
}

@Override
public final boolean isAdult() {
return this.age >= AGE_ADULT;
}

@Override
public final boolean canBreed() {
return this.age == AGE_ADULT;
}

@Override
public void setBreed(boolean breed) {
if (breed) {
setAge(AGE_ADULT);
} else if (isAdult()) {
setAge(BREEDING_AGE);
}
}

public void setScaleForAge(boolean isAdult) {
setScale(isAdult ? 1.0F : 0.5F);
}

@Override
public List<Message> createSpawnMessage() {
List<Message> messages = super.createSpawnMessage();
MetadataMap map = new MetadataMap(GlowAgeable.class);
map.set(MetadataIndex.AGE, this.getAge());
messages.add(new EntityMetadataMessage(id, map.getEntryList()));
return messages;
}

protected final void setScale(float scale) {
setSize(this.height * scale, this.width * scale);
}
}
11 changes: 11 additions & 0 deletions src/main/java/net/glowstone/entity/GlowAmbient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.glowstone.entity;

import org.bukkit.Location;
import org.bukkit.entity.Ambient;

public abstract class GlowAmbient extends GlowLivingEntity implements Ambient {

public GlowAmbient(Location location) {
super(location);
}
}
20 changes: 20 additions & 0 deletions src/main/java/net/glowstone/entity/GlowAnimal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.glowstone.entity;

import org.bukkit.Location;
import org.bukkit.entity.Animals;
import org.bukkit.entity.EntityType;

/**
* Represents an Animal, such as a Cow
*/
public class GlowAnimal extends GlowAgeable implements Animals {

/**
* Creates a new ageable animal.
* @param location The location of the animal.
* @param type The type of animal.
*/
public GlowAnimal(Location location, EntityType type) {
super(location, type);
}
}
7 changes: 3 additions & 4 deletions src/main/java/net/glowstone/entity/GlowCreature.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
import java.util.List;

/**
* Represents a monster such as a creeper.
* @author Graham Edgecombe
* Represents a creature entity such as a pig.
*/
public final class GlowCreature extends GlowLivingEntity implements Creature {
public class GlowCreature extends GlowLivingEntity implements Creature {

/**
* The type of monster.
*/
private final EntityType type;

/**
* The monster's target.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/net/glowstone/entity/GlowEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import net.glowstone.GlowWorld;
import net.glowstone.entity.meta.MetadataIndex;
import net.glowstone.entity.meta.MetadataMap;
import net.glowstone.entity.physics.BoundingBox;
import net.glowstone.entity.physics.EntityBoundingBox;
import net.glowstone.net.message.play.entity.*;
import net.glowstone.util.Position;
import org.apache.commons.lang.Validate;
Expand Down Expand Up @@ -133,6 +135,11 @@ protected String disambiguate(Entity subject, String metadataKey) {
*/
private int fireTicks = 0;

/**
* The entity's bounding box, or null if it has no physical presence.
*/
private EntityBoundingBox boundingBox;

/**
* Creates an entity and adds it to the specified world.
* @param location The location of the entity.
Expand Down Expand Up @@ -517,6 +524,18 @@ protected boolean teleportToEnd() {
return true;
}

protected void setSize(float xz, float y) {
//todo Size stuff with bounding boxes.
}

protected final void setBoundingBox(double xz, double y) {
boundingBox = new EntityBoundingBox(xz, y);
}

public boolean intersects(BoundingBox box) {
return boundingBox != null && boundingBox.intersects(box);
}

////////////////////////////////////////////////////////////////////////////
// Various properties

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/glowstone/entity/meta/MetadataIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public enum MetadataIndex {
// allowed to override NAME_TAG from LivingEntity
PLAYER_SKIN_FLAGS(10, BYTE, HumanEntity.class),

AGE(12, INT, Ageable.class),
AGE(12, BYTE, Ageable.class),

HORSE_FLAGS(16, INT, Horse.class),
HORSE_TYPE(19, BYTE, Horse.class),
Expand Down Expand Up @@ -102,6 +102,8 @@ public enum MetadataIndex {
ITEM_FRAME_ROTATION(3, BYTE, ItemFrame.class),

ENDER_CRYSTAL_HEALTH(8, INT, EnderCrystal.class),

RABBIT_TYPE(18, BYTE, Rabbit.class),
;

private final int index;
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/net/glowstone/entity/passive/GlowBat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.glowstone.entity.passive;

import com.flowpowered.networking.Message;
import net.glowstone.entity.GlowAmbient;
import net.glowstone.entity.meta.MetadataIndex;
import net.glowstone.entity.meta.MetadataMap;
import net.glowstone.net.message.play.entity.EntityHeadRotationMessage;
import net.glowstone.net.message.play.entity.EntityMetadataMessage;
import net.glowstone.net.message.play.entity.SpawnMobMessage;
import net.glowstone.util.Position;
import org.bukkit.Location;
import org.bukkit.entity.Bat;
import org.bukkit.entity.EntityType;

import java.util.LinkedList;
import java.util.List;

public class GlowBat extends GlowAmbient implements Bat {

private boolean isAwake;

public GlowBat(Location location) {
super(location);
}

@Override
public List<Message> createSpawnMessage() {
List<Message> result = new LinkedList<>();

// spawn mob
int x = Position.getIntX(location);
int y = Position.getIntY(location);
int z = Position.getIntZ(location);
int yaw = Position.getIntYaw(location);
int pitch = Position.getIntPitch(location);
result.add(new SpawnMobMessage(id, getType().getTypeId(), x, y, z, yaw, pitch, pitch, 0, 0, 0, metadata.getEntryList()));

// head facing
result.add(new EntityHeadRotationMessage(id, yaw));
MetadataMap map = new MetadataMap(GlowBat.class);
map.set(MetadataIndex.BAT_HANGING, (byte) (this.isAwake ? 1 : 0));
result.add(new EntityMetadataMessage(id, map.getEntryList()));
return result;
}

@Override
public boolean isAwake() {
return isAwake;
}

@Override
public void setAwake(boolean isAwake) {
this.isAwake = isAwake;
}

@Override
public EntityType getType() {
return EntityType.BAT;
}
}
14 changes: 14 additions & 0 deletions src/main/java/net/glowstone/entity/passive/GlowChicken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.glowstone.entity.passive;

import net.glowstone.entity.GlowAnimal;
import org.bukkit.Location;
import org.bukkit.entity.Chicken;
import org.bukkit.entity.EntityType;

public class GlowChicken extends GlowAnimal implements Chicken {

public GlowChicken(Location location) {
super(location, EntityType.CHICKEN);
setSize(0.3F, 0.7F);
}
}
Loading

0 comments on commit 60db527

Please sign in to comment.