Added loading and saving of areas using javascript.
[alterverse.git] / examples / AlienRemake.java
blob84a734fc8e36e0577399fefff74ed4c7d3e08409
1 import bcl.bcl;
2 import org.alterverse.control.*;
3 import org.alterverse.speech.tts;
4 import org.alterverse.sound.*;
5 import org.alterverse.game.*;
6 import org.alterverse.shapes.*;
7 import org.alterverse.world.*;
9 public class AlienRemake extends GameContext {
10 GameEngine engine;
11 int count;
12 Area area;
13 Sound theme;
14 double x;
15 // classes
16 class Alien extends MovableObject {
17 Sound move;
18 Sound land;
19 Sound explode;
20 public Alien(Shape shape) {
21 super(area,shape);
22 land=new Sound("sounds/land.ogg");
23 move=new Sound("sounds/alien.ogg");
24 explode = new Sound("sounds/explode.ogg");
25 // add sounds to list which should be moved with object
26 addSound(move);
27 addSound(explode);
28 addSound(land);
29 move.setPitch(Math.random()*0.4+0.9);
30 move.loop();
33 public void process() {
34 double nz = Math.random()*0.1;
35 double nx = Math.random()*1-0.5;
36 setPosition(getX()+nx,getY(),getZ()+nz);
37 if (getZ()>0) {
38 move.stop();
39 land.play();
40 area.remove(this);
41 removeSounds();
45 public void removeSounds() {
46 for (Sound s: getSounds())
47 SoundManager.removeSound(s);
50 public void onBump(GameObject othr) {
51 super.onBump(othr);
53 if (othr instanceof Beam) {
54 move.stop();
55 explode.play();
56 area.remove(this);
57 removeSounds();
62 class Beam extends MovableObject {
63 Sound laser;
64 public Beam(Shape shape) {
65 super(area,shape);
66 laser=new Sound("sounds/laser.ogg");
67 addSound(laser);
68 laser.play();
71 public void process() {
72 setPosition(getX(),getY(),getZ()-1);
73 if (getY()>20) {
74 area.remove(this);
75 removeSounds();
79 public void removeSounds() {
80 for (Sound s:getSounds())
81 SoundManager.removeSound(s);
84 public void onBump(GameObject othr) {
85 area.remove(this);
86 removeSounds();
90 public AlienRemake() {
91 engine = new GameEngine(this);
92 area = new Area();
93 area.setGravity(false);
94 tts.speak("Welcome to alien remake! ");
95 x=0;
96 count=0;
97 theme = new Sound("sounds/theme.mp3", true);
98 theme.setGain(0.5);
99 theme.loop();
100 engine.run();
103 public void process() {
104 count++;
105 // keys
106 if (bcl.isKeyDown(Keys.LEFT)) {
107 x-=0.125;
108 SoundManager.setListenerPosition(x,0,0);
110 if (bcl.isKeyDown(Keys.RIGHT)) {
111 x+=0.125;
112 SoundManager.setListenerPosition(x,0,0);
114 // other proccessing
115 if (count%150==0) {
116 // spaun new alien
117 double lx = Math.random()*10-5;
118 double lz = Math.random()*10-20;
119 area.add(new Alien(new Box(lx,-1,lz,2,2,2)));
121 area.process();
124 public void keyDown(int key) {
125 if (bcl.isKeyDown(Keys.LSHIFT))
126 switch(key) {
127 case 'q':
128 engine.quit();
129 break;
130 case 's':
131 area.saveJs("space.sav");
132 break;
133 case 'l':
134 area.loadJs("space.sav");
135 break;
137 else
138 switch(key) {
139 case 'i':
140 tts.speak(""+count);
141 break;
142 case ' ':
143 area.add(new Beam(new Box(x-0.125,-1,0,0.25,1,0.5)));
144 break;
148 public void destroy() {
149 theme.stop();
150 theme.destroy();}
152 public static void main(String [] args) {
153 new AlienRemake();