Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUC...
If you think the Android project Station42 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.station42.world;
/*fromwww.java2s.com*/import java.util.ArrayList;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.station42.base.Engine;
import com.station42.base.Entity;
import com.station42.optimizations.RoomResident;
publicclass Room {
publicstaticfinalint ROOM_SIZE = 256;
publicstatic Entity spawn(Engine engine, Rectangle bounds,
boolean up, boolean right, boolean down, boolean left, Object... otherComponents) {
Room room = new Room(bounds, up, right, down, left);
Entity roomEntity = new Entity("Room", otherComponents);
roomEntity.setComponent(Room.class, room);
engine.spawnEntity(roomEntity);
for (Entity wall : room.walls) {
for (Object component : otherComponents) {
wall.setComponent(component.getClass(), component);
}
engine.spawnEntity(wall);
}
return roomEntity;
}
public Rectangle bounds;
ArrayList<Entity> walls = new ArrayList<Entity>();
public ArrayList<Entity> residents = new ArrayList<Entity>();
publicvoid clearResidents() {
for (int i = residents.size() - 1;i >= 0;i--) {
removeResident(residents.get(i));
}
}
publicvoid addResident(Entity resident) {
residents.add(resident);
RoomResident residential = resident.getComponent(RoomResident.class);
if (residential != null) {
residential.addRoom(this);
}
}
publicvoid removeResident(Entity resident) {
residents.remove(resident);
RoomResident residential = resident.getComponent(RoomResident.class);
if (residential != null) {
residential.removeRoom(this);
}
}
public Room(Rectangle bounds, boolean upClosed, boolean rightClosed, boolean bottomClosed, boolean leftClosed) {
this.bounds = bounds;
addWall(bounds,
0, upClosed);
addWall(bounds,
1, rightClosed);
addWall(bounds,
2, bottomClosed);
addWall(bounds,
3, leftClosed);
}
privatevoid addWall(Rectangle bounds, int direction, boolean closed) {
int minX = (int)bounds.x;
int minY = (int)bounds.y;
int maxX = (int)(bounds.x + bounds.width);
int maxY = (int)(bounds.y + bounds.height);
int xMod, yMod;
int x0, x1, y0, y1;
Vector2 in;
switch (direction) {
case 0: // Top wall
x0 = minX;
x1 = maxX;
y0 = maxY;
y1 = maxY;
xMod = 64;
yMod = 0;
in = new Vector2(0, -1);
break;
case 1: // Right wall
x0 = maxX;
x1 = maxX;
y0 = maxY;
y1 = minY;
xMod = 0;
yMod = -64;
in = new Vector2(-1, 0);
break;
case 2: // Bottom wall
x1 = minX;
x0 = maxX;
y0 = minY;
y1 = minY;
xMod = -64;
yMod = 0;
in = new Vector2(0, 1);
break;
case 3: // Left wall
x0 = minX;
x1 = minX;
y0 = minY;
y1 = maxY;
xMod = 0;
yMod = 64;
in = new Vector2(1, 0);
break;
default:
thrownew RuntimeException("BAD VALUE");
}
if (closed) {
walls.add(new Entity("Room wall", new Wall(this, new Vector2(x0, y0),
new Vector2(x1, y1),
in)));
} else {
walls.add(new Entity("Room wall", new Wall(this, new Vector2(x0, y0),
new Vector2((x0 + x1) / 2 - xMod, (y0 + y1) / 2 - yMod),
in)));
walls.add(new Entity("Room wall", new Wall(this, new Vector2((x0 + x1) / 2 + xMod, (y0 + y1) / 2 + yMod),
new Vector2(x1, y1),
in)));
}
}
public ArrayList<Entity> getWalls() {
return walls;
}
publicint getCenterX() {
return (int) (bounds.x + bounds.width / 2);
}
publicint getCenterY() {
return (int) (bounds.y + bounds.height / 2);
}
}