/android/LGame-Android-0.3.1/src/org/loon/framework/android/game/srpg/SRPGData.java

http://loon-simple.googlecode.com/ · Java · 453 lines · 331 code · 52 blank · 70 comment · 88 complexity · 9c308ec042e3244d6106c1b2c7ff4785 MD5 · raw file

  1. package org.loon.framework.android.game.srpg;
  2. import org.loon.framework.android.game.srpg.actor.SRPGActor;
  3. import org.loon.framework.android.game.srpg.actor.SRPGActors;
  4. import org.loon.framework.android.game.srpg.actor.SRPGPosition;
  5. import org.loon.framework.android.game.srpg.actor.SRPGStatus;
  6. import org.loon.framework.android.game.utils.collection.ArrayByte;
  7. /**
  8. * Copyright 2008 - 2011
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  11. * use this file except in compliance with the License. You may obtain a copy of
  12. * the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations under
  20. * the License.
  21. *
  22. * @project loonframework
  23. * @author chenpeng
  24. * @email?ceponline@yahoo.com.cn
  25. * @version 0.1
  26. */
  27. // SRPG???????????????
  28. public class SRPGData {
  29. private SRPGActor[] actorArray;
  30. private SRPGStatus[] statusArray;
  31. private static SRPGData instance;
  32. private int size;
  33. public final static SRPGData getInstnace() {
  34. if (instance == null) {
  35. instance = new SRPGData();
  36. }
  37. return instance;
  38. }
  39. SRPGData() {
  40. }
  41. /**
  42. * ?????????,???????
  43. *
  44. * @param saveName
  45. * @param actors
  46. */
  47. public void initActors(SRPGActors actors) {
  48. this.size = actors.size();
  49. this.actorArray = actors.getActors();
  50. }
  51. /**
  52. * ??????????
  53. *
  54. * @return
  55. * @throws Exception
  56. */
  57. public byte[] savePosition() throws Exception {
  58. ArrayByte bytes = new ArrayByte();
  59. try {
  60. bytes.writeInt(actorArray.length);
  61. for (int i = 0; i < actorArray.length; i++) {
  62. SRPGActor actor = actorArray[i];
  63. if (actor != null) {
  64. bytes.writeInt(i);
  65. bytes.writeInt(actor.getDirection());
  66. bytes.writeInt(actor.getPosX());
  67. bytes.writeInt(actor.getPosY());
  68. }
  69. }
  70. } catch (Exception ex) {
  71. throw new Exception("An exception when the save srpg game data : "
  72. + ex.getMessage());
  73. }
  74. return bytes.getData();
  75. }
  76. /**
  77. * ??????????
  78. *
  79. * @return
  80. * @throws Exception
  81. */
  82. public SRPGPosition[] loadPosition(byte[] res) throws Exception {
  83. ArrayByte bytes = new ArrayByte(res);
  84. SRPGPosition[] positions = null;
  85. try {
  86. int size = bytes.readInt();
  87. positions = new SRPGPosition[size];
  88. for (int i = 0; i < size; i++) {
  89. positions[i] = new SRPGPosition();
  90. int id = bytes.readInt();
  91. int d = bytes.readInt();
  92. int posX = bytes.readInt();
  93. int posY = bytes.readInt();
  94. positions[i].number = id;
  95. positions[i].setPos(posX, posY);
  96. positions[i].vector = d;
  97. }
  98. } catch (Exception ex) {
  99. throw new Exception(
  100. "An exception when the loading srpg game data : "
  101. + ex.getMessage());
  102. }
  103. return positions;
  104. }
  105. /**
  106. * ????????
  107. *
  108. * @return
  109. * @throws Exception
  110. */
  111. public byte[] saveStatus() throws Exception {
  112. ArrayByte bytes = new ArrayByte();
  113. try {
  114. checkInitStatus();
  115. bytes.writeInt(statusArray.length);
  116. for (int i = 0; i < statusArray.length; i++) {
  117. SRPGStatus status = statusArray[i];
  118. if (status != null) {
  119. bytes.writeUTF(status.name);
  120. bytes.writeUTF(status.jobname);
  121. bytes.writeInt(status.number);
  122. bytes.writeInt(status.level);
  123. bytes.writeInt(status.exp);
  124. bytes.writeInt(status.hp);
  125. bytes.writeInt(status.max_hp);
  126. bytes.writeInt(status.max_mp);
  127. bytes.writeInt(status.strength);
  128. bytes.writeInt(status.vitality);
  129. bytes.writeInt(status.agility);
  130. bytes.writeInt(status.magic);
  131. bytes.writeInt(status.resume);
  132. bytes.writeInt(status.mind);
  133. bytes.writeInt(status.sp);
  134. bytes.writeInt(status.dexterity);
  135. bytes.writeInt(status.regeneration);
  136. bytes.writeInt(status.move);
  137. bytes.writeInt(status.movetype);
  138. bytes.writeInt(status.team);
  139. bytes.writeInt(status.group);
  140. bytes.writeInt(status.action);
  141. bytes.writeInt(status.leader);
  142. bytes.writeBoolean(status.isComputer);
  143. if (status.status != null) {
  144. bytes.writeInt(status.status.length);
  145. for (int j = 0; j < status.status.length; j++) {
  146. bytes.writeInt(status.status[j]);
  147. }
  148. } else {
  149. bytes.writeInt(0);
  150. }
  151. if (status.substatus != null) {
  152. bytes.writeInt(status.substatus.length);
  153. for (int j = 0; j < status.substatus.length; j++) {
  154. bytes.writeInt(status.substatus[j]);
  155. }
  156. } else {
  157. bytes.writeInt(0);
  158. }
  159. if (status.ability != null) {
  160. bytes.writeInt(status.ability.length);
  161. for (int j = 0; j < status.ability.length; j++) {
  162. bytes.writeInt(status.ability[j]);
  163. }
  164. } else {
  165. bytes.writeInt(0);
  166. }
  167. if (status.guardelement != null) {
  168. bytes.writeInt(status.guardelement.length);
  169. for (int j = 0; j < status.guardelement.length; j++) {
  170. bytes.writeInt(status.guardelement[j]);
  171. }
  172. } else {
  173. bytes.writeInt(0);
  174. }
  175. if (status.skill != null) {
  176. bytes.writeInt(status.skill.length);
  177. for (int j = 0; j < status.skill.length; j++) {
  178. bytes.writeInt(status.skill[j]);
  179. }
  180. } else {
  181. bytes.writeInt(0);
  182. }
  183. if (status.computer != null) {
  184. bytes.writeInt(status.computer.length);
  185. for (int j = 0; j < status.computer.length; j++) {
  186. bytes.writeInt(status.computer[j]);
  187. }
  188. } else {
  189. bytes.writeInt(0);
  190. }
  191. }
  192. }
  193. } catch (Exception ex) {
  194. throw new Exception("An exception when the save srpg game data : "
  195. + ex.getMessage());
  196. }
  197. return bytes.getData();
  198. }
  199. /**
  200. * ????????
  201. *
  202. * @param res
  203. * @return
  204. * @throws Exception
  205. */
  206. public boolean loadStatus(byte[] res) throws Exception {
  207. ArrayByte bytes = new ArrayByte(res);
  208. try {
  209. int size = bytes.readInt();
  210. if (statusArray != null) {
  211. if (size != statusArray.length) {
  212. statusArray = new SRPGStatus[size];
  213. }
  214. } else {
  215. statusArray = new SRPGStatus[size];
  216. }
  217. for (int i = 0; i < statusArray.length; i++) {
  218. SRPGStatus status = statusArray[i];
  219. if (status != null) {
  220. status.name = bytes.readUTF();
  221. status.jobname = bytes.readUTF();
  222. status.number = bytes.readInt();
  223. status.level = bytes.readInt();
  224. status.exp = bytes.readInt();
  225. status.hp = bytes.readInt();
  226. status.max_hp = bytes.readInt();
  227. status.max_mp = bytes.readInt();
  228. status.strength = bytes.readInt();
  229. status.vitality = bytes.readInt();
  230. status.agility = bytes.readInt();
  231. status.magic = bytes.readInt();
  232. status.resume = bytes.readInt();
  233. status.mind = bytes.readInt();
  234. status.sp = bytes.readInt();
  235. status.dexterity = bytes.readInt();
  236. status.regeneration = bytes.readInt();
  237. status.move = bytes.readInt();
  238. status.movetype = bytes.readInt();
  239. status.team = bytes.readInt();
  240. status.group = bytes.readInt();
  241. status.action = bytes.readInt();
  242. status.leader = bytes.readInt();
  243. status.isComputer = bytes.readBoolean();
  244. int count = bytes.readInt();
  245. if (count == 0) {
  246. status.status = null;
  247. } else {
  248. status.status = new int[count];
  249. for (int j = 0; j < count; j++) {
  250. status.status[j] = bytes.readInt();
  251. }
  252. }
  253. count = bytes.readInt();
  254. if (count == 0) {
  255. status.substatus = null;
  256. } else {
  257. status.substatus = new int[count];
  258. for (int j = 0; j < count; j++) {
  259. status.substatus[j] = bytes.readInt();
  260. }
  261. }
  262. count = bytes.readInt();
  263. if (count == 0) {
  264. status.ability = null;
  265. } else {
  266. status.ability = new int[count];
  267. for (int j = 0; j < count; j++) {
  268. status.ability[j] = bytes.readInt();
  269. }
  270. }
  271. count = bytes.readInt();
  272. if (count == 0) {
  273. status.guardelement = null;
  274. } else {
  275. status.guardelement = new int[count];
  276. for (int j = 0; j < count; j++) {
  277. status.guardelement[j] = bytes.readInt();
  278. }
  279. }
  280. count = bytes.readInt();
  281. if (count == 0) {
  282. status.skill = null;
  283. } else {
  284. status.skill = new int[count];
  285. for (int j = 0; j < count; j++) {
  286. status.skill[j] = bytes.readInt();
  287. }
  288. }
  289. count = bytes.readInt();
  290. if (count == 0) {
  291. status.computer = null;
  292. } else {
  293. status.computer = new int[count];
  294. for (int j = 0; j < count; j++) {
  295. status.computer[j] = bytes.readInt();
  296. }
  297. }
  298. }
  299. }
  300. } catch (Exception ex) {
  301. throw new Exception(
  302. "An exception when loading the srpg game data : "
  303. + ex.getMessage());
  304. }
  305. return true;
  306. }
  307. /**
  308. * ????ID?????
  309. *
  310. * @param i
  311. * @param status1
  312. */
  313. public void setStatus(int i, SRPGStatus status1) {
  314. checkInitStatus();
  315. if (i < statusArray.length) {
  316. statusArray[i] = status1;
  317. }
  318. }
  319. public void setStatus(int index, SRPGActor actor) {
  320. setStatus(index, actor.getActorStatus());
  321. }
  322. /**
  323. * ????ID?????
  324. *
  325. * @param i
  326. * @return
  327. */
  328. public SRPGStatus getStatus(int i) {
  329. checkInitStatus();
  330. if (i < statusArray.length) {
  331. return statusArray[i];
  332. } else {
  333. return null;
  334. }
  335. }
  336. /**
  337. * ??????ID??????????
  338. *
  339. * @param i
  340. * @return
  341. */
  342. public SRPGStatus getCopyStatus(int i) {
  343. checkInitStatus();
  344. if (i < statusArray.length && statusArray[i] != null) {
  345. return new SRPGStatus(statusArray[i]);
  346. } else {
  347. return null;
  348. }
  349. }
  350. public int getMatchStatusIndex(int i) {
  351. checkInitStatus();
  352. for (int j = 0; j < statusArray.length; j++) {
  353. if (statusArray[j].number == i) {
  354. return j;
  355. }
  356. }
  357. return -1;
  358. }
  359. public SRPGStatus getMatchStatus(int i) {
  360. int j = getMatchStatusIndex(i);
  361. if (j != -1) {
  362. return statusArray[j];
  363. } else {
  364. return null;
  365. }
  366. }
  367. public void setDefaultStatus(int i, SRPGStatus status1) {
  368. SRPGStatus status2 = null;
  369. if (status1 != null) {
  370. status2 = new SRPGStatus(status1);
  371. status2.defaultStatus();
  372. status2.team = 0;
  373. status2.group = 0;
  374. }
  375. setStatus(i, status2);
  376. }
  377. public void allDefaultStatus() {
  378. checkInitStatus();
  379. for (int i = 0; i < statusArray.length; i++)
  380. if (statusArray[i] != null) {
  381. SRPGStatus status1 = new SRPGStatus(statusArray[i]);
  382. status1.defaultStatus();
  383. status1.team = 0;
  384. status1.group = 0;
  385. setStatus(i, status1);
  386. }
  387. }
  388. private void checkInitStatus() {
  389. if (statusArray == null) {
  390. statusArray = new SRPGStatus[size];
  391. for (int i = 0; i < size; i++) {
  392. if (actorArray[i] != null) {
  393. statusArray[i] = actorArray[i].getActorStatus();
  394. }
  395. }
  396. }
  397. }
  398. public int size() {
  399. return size;
  400. }
  401. }