Skip to content
This repository has been archived by the owner on Mar 30, 2020. It is now read-only.

Commit

Permalink
added logic for the pick up & dropping of Entities by a Player
Browse files Browse the repository at this point in the history
  • Loading branch information
libunamari committed Sep 21, 2014
1 parent a6b5a60 commit 338a684
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/space/world/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public Character(Vector2D pos, int i) {
super(pos, i, "");
}

public void carryItem(Pickup p){
public void pickupItem(Pickup p){
inventory.add(p);
}

public void dropItem(Pickup p){
public void dropItem(Object p){
inventory.remove(p);
}

Expand Down
15 changes: 13 additions & 2 deletions src/space/world/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
public class Player extends Character implements ViewablePlayer{
private int points;
private Room room;
private final float reach = 5f;
private float jumpTime = 0;
private float xRotation = 90;
private float yRotation = 100;

private static final float EYE_HEIGHT = 6;
private static final float JUMP_HEIGHT = 2;

public Player(Vector2D pos,int i, Room r){
public Player(Vector2D pos,int i/*, Room r*/){
super(pos,i);
room = r;
//room = r;
}

private static float DEGREES_TO_RADIANS(float degrees){
Expand Down Expand Up @@ -86,5 +87,15 @@ public boolean isTorchOn() {
public Room getRoom() {
return room;
}

public void setRoom(Room room) {
this.room = room;
}

public boolean withinReach(Vector2D pos){
float difference = Math.abs(getPosition().getX() - pos.getX());
difference += Math.abs(getPosition().getY() - pos.getY());
return difference <= reach;
}

}
3 changes: 3 additions & 0 deletions src/space/world/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,7 @@ public Map<Room, Exit> getExits() {
return exits;
}

public boolean containsEntity(Entity e){
return entities.contains(e);
}
}
16 changes: 16 additions & 0 deletions src/space/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,29 @@ public void movePlayer(Player p, Vector2D newPos){
p.setPosition(newPos);
room.removeFromRoom(p);
entry.getKey().putInRoom(p);
p.setRoom(entry.getKey());
}
return;
}
}
}
}

public void pickUpEntity(Player player, Entity entity){
if(!(entity instanceof Pickup)){return;}
if(player.withinReach(entity.getPosition()) && player.getRoom().containsEntity(entity)){
player.pickupItem((Pickup) entity);
player.getRoom().removeFromRoom(entity);
}
}

public void dropEntity(Player player, Entity entity, Vector2D dropSpot){
if(player.withinReach(dropSpot) && player.getRoom().contains(dropSpot)){
player.dropItem(entity);
entity.setPosition(dropSpot);
player.getRoom().putInRoom(entity);
}
}
@Override
public Room getRoomAt(Vector2D pos) {
for(Room r : rooms.values()){
Expand Down

0 comments on commit 338a684

Please sign in to comment.