1 /***************************************************************************
2 * Copyright (C) 2010 by the Alterverse team *
3 * email: rynkruger@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write see: *
17 * <http://www.gnu.org/licenses/>. *
18 ***************************************************************************/
20 package org
.alterverse
.world
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Vector
;
25 import javax
.script
.*;
27 public class Area
implements Serializable
{
28 ArrayList
<GameObject
> objects
;
29 GameObject player
= null;
30 Vector
<GameObject
> dellist
;
34 objects
= new ArrayList
<GameObject
>();
35 dellist
=new Vector
<GameObject
>();
39 public void process() {
40 for (int i
= 0; i
< objects
.size();i
++) {
42 if (gravity
&& objects
.get(i
) instanceof MovableObject
&& !((MovableObject
)objects
.get(i
)).hasLanded()) {
43 ((MovableObject
)objects
.get(i
)).fall();
46 objects
.get(i
).process();
48 for (GameObject obj
: dellist
) {
54 public void add(GameObject obj
) {
58 public void addAsPlayer(GameObject obj
) {
63 public void remove(GameObject obj
) {
67 public void save(String file
) {
69 ObjectOutputStream oos
= new ObjectOutputStream(new FileOutputStream(file
));
70 oos
.writeObject(objects
);
72 } catch(Exception x
) {
77 public void load(String file
) {
79 ObjectInputStream ois
= new ObjectInputStream(new FileInputStream(file
));
80 objects
= (ArrayList
<GameObject
>) ois
.readObject();
82 } catch(Exception x
) {
87 public void setGravity(boolean gravity
) {
88 this.gravity
= gravity
;
91 public boolean getGravity() {
95 public ArrayList
<GameObject
> getTouchingObjects(GameObject obj
) {
96 ArrayList
<GameObject
> ret
= new ArrayList
<GameObject
>();
97 for (GameObject i
: objects
) {
100 if (i
.isTouching(obj
))
106 public void setPlayer(GameObject obj
) {
110 public GameObject
getPlayer() {
114 public boolean loadJs(String file
, boolean fixedOnly
) {
116 ScriptEngineManager sm
= new ScriptEngineManager();
117 ScriptEngine e
= sm
.getEngineByName("javascript");
119 for (int i
= 0; i
< objects
.size(); i
++)
120 if (! (objects
.get(i
) instanceof MovableObject
)) {
121 objects
.remove(objects
.get(i
));
127 e
.eval(new FileReader(file
));
129 } catch(Exception x
) {
135 public void saveJs(String file
,boolean fixedOnly
) {
137 FileWriter fw
= new FileWriter(file
);
139 fw
.write("area.setGravity(true);\n");
141 fw
.write("area.setGravity(false);\n");
142 for (GameObject obj
: objects
) {
143 if (fixedOnly
&&obj
instanceof MovableObject
)
145 String var
= "v"+System
.currentTimeMillis();
146 fw
.write(obj
.toJs(var
)+"\n");
147 fw
.write("area.add("+var
+");\n");
150 } catch(Exception x
) {