Added loading and saving of areas using javascript.
[alterverse.git] / src / org / alterverse / world / Area.java
blobf69992168f7fb1c9b7ab5c85d57187f0cd38c75d
1 /***************************************************************************
2 * Copyright (C) 2010 by the Alterverse team *
3 * email: rynkruger@gmail.com *
4 * *
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. *
9 * *
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. *
14 * *
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;
22 import java.io.*;
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;
31 boolean gravity;
33 public Area() {
34 objects = new ArrayList<GameObject>();
35 dellist=new Vector<GameObject>();
36 gravity = false;
39 public void process() {
40 for (int i = 0; i < objects.size();i++) {
41 // gravity stuff
42 if (gravity&& objects.get(i) instanceof MovableObject && !((MovableObject)objects.get(i)).hasLanded()) {
43 ((MovableObject)objects.get(i)).fall();
45 // other stuff
46 objects.get(i).process();
48 for (GameObject obj : dellist) {
49 objects.remove(obj);
51 dellist.clear();
54 public void add(GameObject obj) {
55 objects.add(obj);
58 public void addAsPlayer(GameObject obj) {
59 add(obj);
60 setPlayer(obj);
63 public void remove(GameObject obj) {
64 dellist.add(obj);
67 public void save(String file) {
68 try {
69 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
70 oos.writeObject(objects);
71 oos.close();
72 } catch(Exception x) {
73 x.printStackTrace();
77 public void load(String file) {
78 try {
79 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
80 objects = (ArrayList<GameObject>) ois.readObject();
81 ois.close();
82 } catch(Exception x) {
83 x.printStackTrace();
87 public void setGravity(boolean gravity) {
88 this.gravity = gravity;
91 public boolean getGravity() {
92 return gravity;
95 public ArrayList<GameObject> getTouchingObjects(GameObject obj) {
96 ArrayList<GameObject> ret = new ArrayList<GameObject>();
97 for (GameObject i: objects) {
98 if (i==obj)
99 continue;
100 if (i.isTouching(obj))
101 ret.add(i);
103 return ret;
106 public void setPlayer(GameObject obj) {
107 player=obj;
110 public GameObject getPlayer() {
111 return player;
114 public boolean loadJs(String file, boolean fixedOnly) {
115 try {
116 ScriptEngineManager sm = new ScriptEngineManager();
117 ScriptEngine e = sm.getEngineByName("javascript");
118 if (fixedOnly) {
119 for (int i = 0; i < objects.size(); i++)
120 if (! (objects.get(i) instanceof MovableObject)) {
121 objects.remove(objects.get(i));
122 i--;
124 } else
125 objects.clear();
126 e.put("area",this);
127 e.eval(new FileReader(file));
128 return true;
129 } catch(Exception x) {
130 x.printStackTrace();
131 return false;
135 public void saveJs(String file,boolean fixedOnly) {
136 try {
137 FileWriter fw = new FileWriter(file);
138 if (getGravity())
139 fw.write("area.setGravity(true);\n");
140 else
141 fw.write("area.setGravity(false);\n");
142 for (GameObject obj: objects) {
143 if (fixedOnly&&obj instanceof MovableObject)
144 continue;
145 String var = "v"+System.currentTimeMillis();
146 fw.write(obj.toJs(var)+"\n");
147 fw.write("area.add("+var+");\n");
149 fw.close();
150 } catch(Exception x) {
151 x.printStackTrace();