Added example jsav file.
[alterverse.git] / examples / WorldExample.java
blob9be1ef07195e082f2b887e6e6cc152eef9a71880
1 import bcl.bcl;
2 import org.alterverse.world.*;
3 import org.alterverse.shapes.*;
4 import org.alterverse.sound.*;
5 import org.alterverse.game.*;
6 import org.alterverse.control.*;
7 import org.alterverse.speech.*;
9 public class WorldExample extends GameContext {
10 boolean editing = false;
11 GameEngine engine;
12 Area area;
13 class Floor extends GameObject {
14 public Floor(Shape shape) {
15 super(area,shape);
19 class Stair extends Floor {
20 public Stair(Shape shape) {
21 super(shape);
25 class Wall extends GameObject {
26 public Wall(Shape shape) {
27 super(area,shape);
31 class Machine extends GameObject {
32 Sound sound;
33 public Machine(Shape shape) {
34 super(area,shape);
35 sound = new Sound("sounds/machine1.ogg");
36 sound.loop();
37 addSound(sound);
38 updateData();
42 class Human extends DirectionalObject {
43 Sound land;
44 Sound wall;
45 Sound watch;
46 Sound stair;
47 public Human(Shape shape) {
48 super(area,shape);
49 land = new Sound("sounds/step.ogg");
50 wall = new Sound("sounds/wall.ogg");
51 addSound(wall);
52 addSound(land);
53 watch = new Sound("sounds/watch.ogg");
54 addSound(watch);
55 stair = new Sound("sounds/stair.ogg");
56 addSound(stair);
57 setStepupSize(0.5);
60 public void onFall(double distance) {
63 public void updateListener() {
64 super.updateData();
65 SoundManager.setListenerPosition(getX(),getY(),getZ());
68 public void onBump(GameObject othr) {
69 if (othr instanceof Wall)
70 wall.play();
71 else if (othr instanceof Stair)
72 stair.play();
73 else if (othr instanceof Floor)
74 land.play();
75 if (othr instanceof Human)
76 watch.play();
77 super.onBump(othr);
81 Player player;
82 double lx,ly,lz,ux,uy,uz;
83 public WorldExample() {
84 engine = new GameEngine(this);
85 lx=0;
86 ly=0;
87 lz=0;
88 ux=0;
89 uy=0;
90 uz=0;
91 area = new Area();
92 area.add(new Floor(new Box(-30,-2,-20,60,2,22)));
93 area.add(new Floor(new Box(-30,-7,-19,60,2,40)));
94 area.add(new Stair(new Box(-2,-1.5,2,4,1,1)));
95 area.add(new Stair(new Box(-2,-2,3,4,1,1)));
96 area.add(new Stair(new Box(-2,-2.5,4,4,1,1)));
97 area.add(new Stair(new Box(-2,-3,5,4,1,1)));
98 area.add(new Stair(new Box(-2,-3.5,6,4,1,1)));
99 area.add(new Stair(new Box(-2,-4,7,4,1,1)));
100 area.add(new Stair(new Box(-2,-4.5,8,4,1,1)));
101 area.add(new Stair(new Box(-2,-5,9,4,1,1)));
102 area.add(new Stair(new Box(-2,-5.5,10,4,1,1)));
103 area.add(new Wall(new Box(-30,0,-3,30,10,1)));
104 area.add(new Wall(new Box(-1,0,-6,1,10,4)));
105 area.add(new Wall(new Box(-1,0,-12,1,10,5)));
106 area.add(new Wall(new Box(-10,0,-12,1,10,10)));
107 area.add(new Wall(new Box(-9,0,-12,8,10,1)));
108 area.add(new Wall(new Box(1,0,-3,10,10,1)));
109 area.add(new Machine(new Box(-9,0,-11,2,2,1)));
110 Wall w = new Wall(new Box(1.0,0.0,-6.0,2.0,4.0,2.0));
111 w.addChild(new Wall(new Box(5.0,0.0,-6.0,2.0,4.0,2.0)));
112 w.addShape(new Box(9.0,0.0,-6.0,2.0,4.0,2.0));
113 area.add(w);
114 player =new Player(new Box(-0.2,2,0.2,0.4,1.9,0.4));
115 area.add(player);
116 area.add(new Nonplayer(new Box(-5,1,1,0.4,1.9,0.4)));
117 area.add(new Nonplayer(new Box(5,1,1,0.4,1.9,0.4)));
118 area.setGravity(true);
119 tts.speak("Starting up", 2);
121 engine.run();
124 public void process() {
125 area.process();
128 public class Nonplayer extends Human {
129 int timer=0;
130 public Nonplayer(Shape shape) {
131 super(shape);
134 public void process() {
135 timer++;
136 if (timer%20==0) {
137 double dx = (Math.random()*2)-1;
138 double dz = (Math.random()*2)-1;
139 setX(getX()+dx);
140 setZ(getZ()+dz);
145 public class Player extends Human {
146 public Player(Shape shape) {
147 super(shape);
148 setSpeed(0.5);
151 public void updateData() {
152 super.updateData();
153 SoundManager.setListenerPosition(getX(), getY(), getZ());
154 Point p = new Point(getDirection(),1);
155 SoundManager.setListenerOrientation(p.x,p.y,p.z,0,1,0);
159 public void keyDown(int key) {
160 if (editing) {
161 switch(key) {
162 case 's':
163 if (player.getSolid())
164 player.setSolid(false);
165 else
166 player.setSolid(true);
167 tts.speak(player.getSolid()? "Solid":"Non-solid", 2);
168 break;
169 case Keys.UP:
170 case 'i':
171 if (bcl.isKeyDown(Keys.LSHIFT))
172 player.setY(player.getY()+0.5);
173 player.updateListener();
174 break;
175 case Keys.DOWN:
176 case 'k':
177 if (bcl.isKeyDown(Keys.LSHIFT))
178 player.setY(player.getY()-0.5);
179 player.updateListener();
180 break;
183 switch(key) {
184 case 'e':
185 if (bcl.isKeyDown(Keys.LSHIFT) && bcl.isKeyDown(Keys.LALT)) {
186 if (editing)
187 editing = false;
188 else
189 editing = true;
190 tts.speak(editing? "Edit mode": "normal mode", 2);
191 area.setGravity(! editing);
193 break;
194 case Keys.LEFT:
195 case 'j':
196 if (bcl.isKeyDown(Keys.LSHIFT))
197 player.turnLeft();
198 else if (bcl.isKeyDown(Keys.LALT))
199 player.turnLeft90();
200 else
201 player.moveLeft();
202 break;
203 case Keys.RIGHT:
204 case 'l':
205 if (bcl.isKeyDown(Keys.LSHIFT))
206 player.turnRight();
207 else if (bcl.isKeyDown(Keys.LALT))
208 player.turnRight90();
209 else
210 player.moveRight();
211 break;
212 case Keys.UP:
213 case 'i':
214 if (! bcl.isKeyDown(Keys.LSHIFT))
215 player.moveForward();
216 break;
217 case Keys.DOWN:
218 case 'k':
219 if (! bcl.isKeyDown(Keys.LSHIFT))
220 player.moveBackward();
221 break;
222 case 'd':
223 tts.speak(""+player.getDirection(),2);
224 break;
225 case 'w':
226 tts.speak("position: " + player.getX()+", "+player.getY()+", " + player.getZ(),2);
227 break;
228 case 'a':
229 lx=player.getX();
230 ly = player.getY()-3.0;
231 lz = player.getZ();
232 break;
233 case 'b':
234 ux = player.getX();
235 uy=player.getY();
236 uz = player.getZ();
237 break;
238 case 'c':
239 GameObject gobj = new GameObject(area,new BoundBox(lx,ly,lz,ux,uy,uz));
240 GameObject candidate = area.getTouchingObjects(gobj).get(0);
241 ly = candidate.minY();
242 uy = candidate.maxY();
243 candidate.split(lx,ly,lz,ux,uy,uz);
244 break;
245 case 'q':
246 engine.quit();
247 break;
251 public static void main (String [] args) {
252 new WorldExample();