PageRenderTime 55ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/FallingSandpaper/jni/update.c

http://thelements.googlecode.com/
C | 474 lines | 410 code | 30 blank | 34 comment | 221 complexity | 1941e029d693aa5b8ca4e589a8c3c411 MD5 | raw file
  1. /*
  2. * update.c
  3. * -----------------------------------
  4. * Defines the function UpdateView(), which
  5. * is called every frame to update all the
  6. * particles' positions.
  7. */
  8. #include "update.h"
  9. void UpdateView(void)
  10. {
  11. if (fd == 1) // if the finger is down
  12. {
  13. // If the mouse is above the menu, there are not too many points, and there isn't already sand there, then make a piece of sand
  14. if (ym != 0)
  15. {
  16. int yc;
  17. int xc;
  18. for (yc = size; yc >= -size; yc--)
  19. {
  20. for (xc = -size; xc <= size; xc++)
  21. {
  22. if ((xc * xc) + (yc * yc) <= (size * size))
  23. {
  24. if (solid[celement] != 1 && celement != 16 && celement
  25. != 3) //not wall, eraser, or plant, wind or fuse
  26. {
  27. if (xc + xm < maxx && xc + xm > 0 && yc + ym < maxy
  28. && yc + ym > 0
  29. && allcoords[(int) (xc + xm)][(int) (yc
  30. + ym)] == -1 && rand() % 3 == 1)
  31. {
  32. CreatePoint(xm + xc, ym + yc, celement);
  33. }
  34. }
  35. else if (solid[celement] == 1) //wall or plant or fuse should be drawn solid
  36. {
  37. if (xc + xm < maxx && xc + xm > 0 && yc + ym < maxy
  38. && yc + ym > 0
  39. && allcoords[(int) (xc + xm)][(int) (yc
  40. + ym)] == -1)
  41. {
  42. CreatePoint(xm + xc, ym + yc, celement);
  43. }
  44. }
  45. else if (celement == 16) //wind
  46. {
  47. if (xc + lmx < maxx && xc + lmx > 0 && yc + lmy
  48. < maxy && yc + lmy > 0)
  49. {
  50. if (allcoords[lmx + xc][lmy + yc] != -1)
  51. {
  52. if (fallvel[element[allcoords[lmx + xc][lmy
  53. + yc]]] != 0)
  54. {
  55. xvel[allcoords[lmx + xc][lmy + yc]]
  56. += (xm - lmx);
  57. yvel[allcoords[lmx + xc][lmy + yc]]
  58. += (ym - lmy);
  59. }
  60. }
  61. }
  62. }
  63. else
  64. { //eraser
  65. if (xc + xm < maxx && xc + xm > 0 && yc + ym < maxy
  66. && yc + ym > 0
  67. && allcoords[(int) (xc + xm)][(int) (yc
  68. + ym)] != -1)
  69. {
  70. DeletePoint(allcoords[xm + xc][ym + yc]);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. if (play == 1)
  79. {
  80. int counter;
  81. int rtop; //used to prevent bugs when fire reaches the top
  82. int tempx, tempy, ox, oy; //For speed we're going to create temp variables to store stuff
  83. int oelement; //these are also used to check to see if the element has changed to do stuff about freezing particles
  84. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "0");
  85. // Move the particles and do collisions
  86. for (counter = 0; counter < TPoints; counter++)
  87. {
  88. if (set[counter] == 1 && frozen[counter] < 4)
  89. {
  90. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "1");
  91. int random = rand();
  92. if (element[counter] == 5 && ((random % 7) == 0))
  93. {
  94. unFreezeParticles(x[counter], y[counter]);
  95. DeletePoint(counter);
  96. }
  97. else
  98. {
  99. oy = (int) y[counter];
  100. ox = (int) x[counter];
  101. oldy[counter] = oy;
  102. oldx[counter] = ox;
  103. oelement = element[counter];
  104. if ((int) gravy != 0 && accelcon == 1)
  105. {
  106. y[counter] += ((gravy / 9.8) * fallvel[oelement]
  107. + yvel[counter]);
  108. }
  109. else if (accelcon == 0)
  110. { // no accelerometer control still needs to have stuff fall
  111. y[counter] += fallvel[oelement] + yvel[counter];
  112. }
  113. else
  114. {
  115. y[counter] += yvel[counter]; //with accel tho, it shouldn't
  116. }
  117. if ((int) gravx != 0 && accelcon == 1)
  118. {
  119. x[counter] += ((gravx / 9.8) * fallvel[oelement]
  120. + xvel[counter]);
  121. }
  122. else
  123. {
  124. x[counter] += xvel[counter];
  125. }
  126. if (xvel[counter] > 0)
  127. {
  128. if ((oelement == 15 || oelement == 21) && xvel[counter]
  129. > 5)
  130. {
  131. element[counter] = 0; //change particle to sand if the velocity on the wall is great enough
  132. setBitmapColor((int) x[counter], (int) y[counter],
  133. 0); //Set the color also
  134. }
  135. else if (oelement == 15 || oelement == 21)
  136. {
  137. x[counter] = ox;
  138. y[counter] = oy;
  139. xvel[counter] = 0;
  140. }
  141. else
  142. {
  143. xvel[counter] -= 1;
  144. }
  145. }
  146. //element could have changed so don't use olement anymore
  147. else if (xvel[counter] < 0)
  148. {
  149. if ((element[counter] == 15 || element[counter] == 21)
  150. && xvel[counter] < -5)
  151. {
  152. element[counter] = 0; //change particle to sand if the velocity on the wall is great enough
  153. setBitmapColor((int) x[counter], (int) y[counter],
  154. 0); //Set the color also
  155. }
  156. else if (element[counter] == 15 || element[counter]
  157. == 21)
  158. {
  159. x[counter] = ox;
  160. y[counter] = oy;
  161. xvel[counter] = 0;
  162. }
  163. else
  164. {
  165. xvel[counter] += 1;
  166. }
  167. }
  168. if (yvel[counter] > 0)
  169. {
  170. if ((element[counter] == 15 || element[counter] == 21)
  171. && yvel[counter] > 5)
  172. {
  173. element[counter] = 0; //change particle to sand if the velocity on the wall is great enough
  174. setBitmapColor((int) x[counter], (int) y[counter],
  175. 0); //Set the color also
  176. }
  177. else if (element[counter] == 15 || element[counter]
  178. == 21)
  179. {
  180. x[counter] = ox;
  181. y[counter] = oy;
  182. yvel[counter] = 0;
  183. }
  184. else
  185. {
  186. yvel[counter] -= 1;
  187. }
  188. }
  189. else if (yvel[counter] < 0)
  190. {
  191. if ((element[counter] == 15 || element[counter] == 21)
  192. && yvel[counter] < -5)
  193. {
  194. element[counter] = 0; //change particle to sand if the velocity on the wall is great enough
  195. setBitmapColor((int) x[counter], (int) y[counter],
  196. 0); //Set the color also
  197. }
  198. else if (element[counter] == 15 || element[counter]
  199. == 21)
  200. {
  201. x[counter] = ox;
  202. y[counter] = oy;
  203. yvel[counter] = 0;
  204. }
  205. else
  206. {
  207. yvel[counter] += 1;
  208. }
  209. }
  210. if ((int) y[counter] >= maxy)
  211. {
  212. y[counter] = oy;
  213. yvel[counter] = 0;
  214. xvel[counter] = 0;
  215. }
  216. if ((int) x[counter] >= maxx || (int) x[counter] <= 0)
  217. {
  218. x[counter] = ox;
  219. xvel[counter] = 0;
  220. yvel[counter] = 0;
  221. }
  222. if ((int) y[counter] <= 0) //If the particle is above the top of the screen
  223. {
  224. if (element[counter] == 5) //If it's fire
  225. {
  226. //Delete it
  227. x[counter] = 0;
  228. y[counter] = 0;
  229. element[counter] = 0;
  230. xvel[counter] = 0;
  231. yvel[counter] = 0;
  232. set[counter] = 0;
  233. avail[loq] = counter;
  234. loq++;
  235. setBitmapColor(ox, oy, 3);
  236. allcoords[ox][oy] = -1;
  237. rtop = 1;
  238. }
  239. else //Not fire
  240. {
  241. //Just bounce it back and kill velocity
  242. y[counter] = oy;
  243. yvel[counter] = 0;
  244. xvel[counter] = 0;
  245. if (element[counter] == 18) //If it's steam
  246. {
  247. xvel[counter] = rand() % 3 - 1; //Add a random velocity
  248. }
  249. }
  250. }
  251. if (rtop == 0)
  252. {
  253. tempx = (int) x[counter];
  254. tempy = (int) y[counter];
  255. if (fireburn[element[counter]] == 1) //Fire cycle
  256. {
  257. frozen[counter] = 0;
  258. random = rand();
  259. if (collision[element[allcoords[tempx + 1][tempy]]][element[counter]]
  260. == 6 && random % 3 != 0)
  261. {
  262. element[allcoords[tempx + 1][tempy]] = 5;
  263. setBitmapColor(tempx + 1, tempy, 5);
  264. }
  265. random = rand();
  266. if (collision[element[allcoords[tempx][tempy - 1]]][element[counter]]
  267. == 6 && random % 3 != 0)
  268. {
  269. element[allcoords[tempx][tempy - 1]] = 5;
  270. setBitmapColor(tempx, tempy - 1, 5);
  271. }
  272. random = rand();
  273. if (collision[element[allcoords[tempx - 1][tempy]]][element[counter]]
  274. == 6 && random % 3 != 0)
  275. {
  276. element[allcoords[tempx - 1][tempy]] = 5;
  277. setBitmapColor(tempx - 1, tempy, 5);
  278. }
  279. random = rand();
  280. if (collision[element[allcoords[tempx][tempy + 1]]][element[counter]]
  281. == 6 && random % 3 != 0)
  282. {
  283. element[allcoords[tempx][tempy + 1]] = 5;
  284. setBitmapColor(tempx, tempy + 1, 5);
  285. yvel[allcoords[tempx][tempy + 1]] = 2;
  286. }
  287. random = rand();
  288. if (collision[element[allcoords[tempx + 1][tempy
  289. + 1]]][element[counter]] == 6 && random % 3
  290. != 0)
  291. {
  292. element[allcoords[tempx + 1][tempy + 1]] = 5;
  293. setBitmapColor(tempx + 1, tempy + 1, 5);
  294. yvel[allcoords[tempx][tempy + 1]] = 2;
  295. }
  296. random = rand();
  297. if (collision[element[allcoords[tempx - 1][tempy
  298. + 1]]][element[counter]] == 6 && random % 3
  299. != 0)
  300. {
  301. element[allcoords[tempx - 1][tempy + 1]] = 5;
  302. setBitmapColor(tempx - 1, tempy + 1, 5);
  303. yvel[allcoords[tempx][tempy + 1]] = 2;
  304. }
  305. random = rand();
  306. if (collision[element[allcoords[tempx + 1][tempy
  307. - 1]]][element[counter]] == 6 && random % 3
  308. != 0)
  309. {
  310. element[allcoords[tempx + 1][tempy - 1]] = 5;
  311. setBitmapColor(tempx + 1, tempy - 1, 5);
  312. }
  313. random = rand();
  314. if (collision[element[allcoords[tempx - 1][tempy
  315. - 1]]][element[counter]] == 6 && random % 3
  316. != 0)
  317. {
  318. element[allcoords[tempx - 1][tempy - 1]] = 5;
  319. setBitmapColor(tempx - 1, tempy - 1, 5);
  320. }
  321. }
  322. if (element[counter] == 8) //Spawn cycle
  323. {
  324. frozen[counter] = 0;
  325. int check1, check2, temp;
  326. for (check1 = -2; check1 <= 2; check1++)
  327. {
  328. for (check2 = -2; check2 <= 2; check2++)
  329. {
  330. if (tempx + check1 > 1 && tempx + check1
  331. < maxx && tempy + check2 >= 0
  332. && tempy + check2 < maxy)
  333. {
  334. temp = allcoords[tempx + check1][tempy
  335. + check2];
  336. if (temp != -1 && element[temp] == 7) //There's a generator adjacent
  337. {
  338. element[temp] = 8; //Change the generator to a spawn
  339. spawn[temp] = spawn[counter]; //Make the spawn element the same as this one
  340. }
  341. else if (temp == -1 && rand() % 200
  342. == 0 && loq < TPoints - 1) //There's an empty spot
  343. {
  344. //Change the probability to change how much spawns overall
  345. CreatePoint(tempx + check1, tempy
  346. + check2, spawn[counter]); //1/200 chance of spawning
  347. }
  348. }
  349. }
  350. }
  351. }
  352. if (growing[element[counter]] == 1) //Ice cycle
  353. {
  354. frozen[counter] = 0;
  355. int check1, check2, temp;
  356. for (check1 = -1; check1 <= 1; check1++)
  357. {
  358. for (check2 = -1; check2 <= 1; check2++)
  359. {
  360. temp = allcoords[tempx + check1][tempy
  361. + check2];
  362. if (temp != -1 && element[temp] == 1
  363. && rand() % 10 == 0)
  364. {
  365. element[temp] = element[counter]; //Change water to ice
  366. }
  367. }
  368. }
  369. }
  370. if (condensing[element[counter]] != -1) //Steam cycle
  371. {
  372. frozen[counter] = 0;
  373. if (rand() % 200 == 0) //1/200 chance
  374. {
  375. element[counter] = condensing[element[counter]]; //"Condense" the steam
  376. setBitmapColor(x[counter], y[counter],
  377. element[counter]);
  378. }
  379. }
  380. int atemporary = allcoords[tempx][tempy];
  381. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "start");
  382. //if the space the particle is trying to move to is taken
  383. if (atemporary != -1 && atemporary != counter)
  384. {
  385. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "mid1");
  386. int secondElementTemp = element[atemporary];
  387. collide(counter, atemporary); //collision between the two particles
  388. //if nothing has changed
  389. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "mid");
  390. if (x[counter] == ox && y[counter] == oy
  391. && element[counter] == oelement
  392. && x[atemporary] == tempx && y[atemporary]
  393. == tempy && element[atemporary]
  394. == secondElementTemp)
  395. {
  396. frozen[counter]++; //increase freeze rounds count
  397. }
  398. else
  399. {
  400. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "mid2");
  401. if (x[counter] != ox || y[counter] != oy
  402. || element[counter] != oelement)
  403. {
  404. //unFreeze stuff around the primary particle because stuff has changed with it
  405. //unFreezeParticles(x[counter],y[counter]);
  406. unFreezeParticles(ox, oy);
  407. }
  408. if (x[atemporary] != tempx || y[atemporary]
  409. != tempy || element[atemporary]
  410. != secondElementTemp)
  411. {
  412. //unFreeze stuff around the secondary particle because stuff has changed with it
  413. //unFreezeParticles(x[atemporary],y[atemporary]);
  414. unFreezeParticles(tempx, tempy);
  415. }
  416. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "2");
  417. }
  418. }
  419. else
  420. {
  421. if (atemporary != counter)
  422. {
  423. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "unfreeze stuff");
  424. //Clear the old spot
  425. allcoords[ox][oy] = -1;
  426. setBitmapColor(ox, oy, 3);
  427. //unfreeze particles around old spot
  428. unFreezeParticles(ox, oy);
  429. //Set new spot
  430. allcoords[tempx][tempy] = counter;
  431. setBitmapColor(tempx, tempy, element[counter]);
  432. //unFreeze paritlces around new spot
  433. //unFreezeParticles( tempx, tempy );
  434. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "3");
  435. }
  436. }
  437. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "end");
  438. }
  439. }
  440. rtop = 0;
  441. }
  442. }
  443. }
  444. }