fork download
  1. class Beta implements Task {
  2. Maru maru;
  3. Field field;
  4. Group<Teki> tekis;
  5. Group<Flag> flags;
  6.  
  7. Beta(Maru maru, Field field, Group<Teki> tekis, Group<Flag> flags) {
  8. this.maru = maru;
  9. this.field = field;
  10. this.tekis = tekis;
  11. this.flags = flags;
  12. }
  13. void draw() {
  14. }
  15. void field_with_tekis() {
  16. for (Teki teki : tekis) {
  17. if (teki.x() < field.x()) {
  18. teki.x(field.w());
  19. } else if (teki.x() > field.w()) {
  20. teki.x(field.y());
  21. }
  22. if (teki.y() < field.y()) {
  23. teki.y(field.h());
  24. } else if (teki.y() > field.h()) {
  25. teki.y(field.y());
  26. }
  27. }
  28. }
  29. void maru_with_tekis() {
  30. if(maru.isMuteki())return;
  31.  
  32. for (Teki teki : tekis) {
  33. if (maru.r() + teki.r() > dist(maru.x(), maru.y(), teki.x(), teki.y())) {
  34. field.alert();
  35. teki.damaged();
  36. maru.damaged();
  37. }
  38. }
  39. }
  40. boolean isHit(Sprite a, Sprite b){
  41. return abs(a.cx()-b.cx()) < a.w2() + b.w2() && abs(a.cy()-b.cy()) < a.h2() + b.h2();
  42. }
  43. void maru_with_flags() {
  44. for (Flag flag : flags) {
  45. if(flag.isTouched()) continue;
  46.  
  47. if (isHit(maru, flag)) {
  48. flag.touch();
  49. }
  50. }
  51. }
  52. void update() {
  53. field_with_tekis();
  54. maru_with_tekis();
  55. maru_with_flags();
  56. }
  57. boolean isEnd() {
  58. return false;
  59. }
  60. void mousePressed() {
  61. }
  62. }
  63. enum State {
  64. Normal, Damaged,
  65. }
  66. class Field extends Sprite {
  67. color normal;
  68. color damaged;
  69. State state;
  70.  
  71. int count;
  72. final int max = 10;
  73.  
  74. Field(float x, float y, float w, float h) {
  75. this.position = new PVector(x, y);
  76. this.w = w;
  77. this.h = h;
  78. this.d = w;
  79. this.r = w/2;
  80. this.normal = color(0, 0, 100);
  81. this.damaged = color(60, 100, 100);
  82. this.state = State.Normal;
  83. }
  84. void draw() {
  85. switch(state) {
  86. case Normal:
  87. noStroke();
  88. fill(normal);
  89. rect(x(), y(), w(), h());
  90. break;
  91.  
  92. case Damaged:
  93. noStroke();
  94. fill(damaged);
  95. rect(x(), y(), w(), h());
  96. break;
  97. }
  98. }
  99. void alert() {
  100. state = State.Damaged;
  101. count = 0;
  102. }
  103. void update() {
  104. switch(state) {
  105. case Normal:
  106. break;
  107.  
  108. case Damaged:
  109. count++;
  110. if (count > max) {
  111. state = State.Normal;
  112. }
  113. break;
  114. }
  115. }
  116. boolean isEnd() {
  117. return false;
  118. }
  119. void mousePressed() {
  120. }
  121. }
  122. class Flag extends Sprite {
  123. boolean touched;
  124. //color before;
  125. color after;
  126. PVector ps[];//points for triangle
  127.  
  128. Flag(float x, float y) {
  129. position = new PVector(x, y);
  130. velocity = new PVector(0, 0);
  131. touched = false;
  132. w = 30;
  133. h = 30;
  134. d = w;
  135. r = d/2;
  136. after = color(120, 40, 100);
  137.  
  138. ps = new PVector[3];
  139. ps[0] = new PVector(x(), y());
  140. ps[1] = new PVector(x()+w()*0.5, y()+h2()/2);
  141. ps[2] = new PVector(x(), y()+h2());
  142. }
  143. void drawRect(){
  144. rect(x(), y(), w(), h());
  145. }
  146. void drawFlag(){
  147. triangle(ps[0].x, ps[0].y, ps[1].x, ps[1].y, ps[2].x, ps[2].y);
  148. line(x(), y()+h2(), x(), yh());
  149. }
  150. void draw() {
  151. if (touched) {
  152. fill(after);
  153. stroke(0, 0, 0);
  154. //drawRect();//check, ok
  155. drawFlag();
  156. } else {
  157. noFill();
  158. stroke(0, 0, 0);
  159. //drawRect();//check, ok
  160. drawFlag();
  161. }
  162. }
  163. void update() {
  164. }
  165. boolean isEnd() {
  166. return false;
  167. }
  168. void touch() {
  169. touched = true;
  170. }
  171. void mousePressed() {
  172. }
  173. boolean isTouched() {
  174. return touched;
  175. }
  176. }
  177. import java.lang.Iterable;
  178. import java.util.Iterator;
  179. class Group<T extends Task> implements Task, Iterable<T> {
  180. ArrayList<T> self;
  181.  
  182. Group() {
  183. this.self = new ArrayList<T>();
  184. }
  185. void draw() {
  186. for (T t : self) {
  187. t.draw();
  188. }
  189. }
  190. void update() {
  191. for (int i=self.size()-1; i>=0; i--) {
  192. self.get(i).update();
  193.  
  194. if (self.get(i).isEnd()) {
  195. self.remove(i);
  196. }
  197. }
  198. }
  199. boolean isEnd() {
  200. return false;
  201. }
  202. void add(T t) {
  203. self.add(t);
  204. }
  205. boolean isEmpty() {
  206. return self.isEmpty();
  207. }
  208. @Override
  209. public Iterator<T> iterator() {
  210. return self.iterator();
  211. }
  212. int size() {
  213. return self.size();
  214. }
  215. T get(int i) {
  216. return self.get(i);
  217. }
  218. void mousePressed() {
  219. for (T t : self) {
  220. t.mousePressed();
  221. }
  222. }
  223. }
  224. enum MaruState {
  225. Normal, Muteki,
  226. }
  227. class Maru extends Sprite {
  228. float e = 0.095;
  229.  
  230. int life;
  231. int lifeMax;
  232.  
  233. float ts = 36;
  234. float tx;
  235. float ty;
  236. float margin = 10;
  237.  
  238. MaruState state;
  239. int count;
  240. final int max = 80;
  241.  
  242. Maru(float x, float y) {
  243. position = new PVector(x, y);
  244. velocity = new PVector(0, 0);
  245. v = 2;
  246. d = 20;
  247. r = d/2;
  248. w = d;
  249. h = d;
  250. lifeMax = 12;
  251. life = lifeMax;
  252. state = MaruState.Normal;
  253. ty = height - ts - margin;
  254. }
  255. void drawLife(){
  256. String s = life+"/"+lifeMax;
  257. float tw = textWidth(s);
  258. tx = width - tw - margin;
  259. textSize(ts);
  260. text(s, tx, ty);
  261. }
  262. void draw() {
  263. switch(state) {
  264. case Normal:
  265. drawLife();
  266.  
  267. noFill();
  268. stroke(0);
  269. ellipse(x(), y(), d, d);
  270. break;
  271.  
  272. case Muteki:
  273. drawLife();
  274.  
  275. if (count%10 < 5) {
  276. noFill();
  277. stroke(0);
  278. ellipse(x(), y(), d, d);
  279. } else {
  280. //
  281. }
  282. break;
  283. }
  284. }
  285. void move_old() {
  286. if (x() < mouseX) {
  287. position.x += v;
  288. } else {
  289. position.x -= v;
  290. }
  291. if (y() < mouseY) {
  292. position.y += v;
  293. } else {
  294. position.y -= v;
  295. }
  296. }
  297. void move_easing() {
  298. position.x += (mouseX-x())*e;
  299. position.y += (mouseY-y())*e;
  300. }
  301. void update() {
  302. switch(state) {
  303. case Normal:
  304. move_easing();
  305. break;
  306.  
  307. case Muteki:
  308. move_easing();
  309. count++;
  310. if (count > max) {
  311. state = MaruState.Normal;
  312. }
  313. break;
  314. }
  315. }
  316. void damaged() {
  317. life--;
  318. state = MaruState.Muteki;
  319. count = 0;
  320. }
  321. boolean isMuteki(){
  322. return state == MaruState.Muteki;
  323. }
  324. boolean isEnd() {
  325. return life <= 0;
  326. }
  327. void mousePressed() {
  328. }
  329. }
  330. interface Scene extends Task {
  331. }
  332.  
  333. interface Changer {
  334. void change(Scene next);
  335. }
  336.  
  337. class SceneManager implements Task, Changer {
  338. Scene current;
  339. Scene next;
  340.  
  341. SceneManager() {
  342. current = new Title(this);
  343. }
  344. void draw() {
  345. current.draw();
  346. }
  347. void update() {
  348. if (next != null) {
  349. current = next;
  350. next = null;
  351. }
  352. current.update();
  353. }
  354. boolean isEnd() {
  355. return false;
  356. }
  357. void change(Scene next) {
  358. this.next = next;
  359. }
  360. void mousePressed() {
  361. current.mousePressed();
  362. }
  363. }
  364.  
  365. class Title implements Scene {
  366. Changer changer;
  367.  
  368. //String s = "avoiding game";
  369. String s = "flag eater";
  370. float ts = 42;
  371. float x, y;
  372.  
  373. Title(Changer changer) {
  374. this.changer = changer;
  375.  
  376. textSize(ts);
  377. float w = textWidth(s);
  378. this.x = width/2 - w/2;
  379. this.y = height/2 - ts;
  380. }
  381. void draw() {
  382. background(0, 0, 100);
  383. fill(140, 100, 100);
  384. textSize(ts);
  385. text(s, x, y);
  386. }
  387. void update() {
  388. }
  389. boolean isEnd() {
  390. return false;
  391. }
  392. void mousePressed() {
  393. changer.change(new Stage(changer));
  394. }
  395. }
  396.  
  397. class Failure implements Scene {
  398. Changer changer;
  399.  
  400. String s = "failure";
  401. float ts = 42;
  402. float x, y;
  403.  
  404. Failure(Changer changer) {
  405. this.changer = changer;
  406.  
  407. textSize(ts);
  408. float w = textWidth(s);
  409. this.x = width/2 - w/2;
  410. this.y = height/2 - ts;
  411. }
  412. void draw() {
  413. background(0, 0, 100);
  414. fill(140, 100, 100);
  415. textSize(ts);
  416. text(s, x, y);
  417. }
  418. void update() {
  419. }
  420. boolean isEnd() {
  421. return false;
  422. }
  423. void mousePressed() {
  424. changer.change(new Title(changer));
  425. }
  426. }
  427.  
  428. class Clear implements Scene {
  429. Changer changer;
  430.  
  431. String s = "clear";
  432. float ts = 42;
  433. float x, y;
  434.  
  435. Clear(Changer changer) {
  436. this.changer = changer;
  437.  
  438. textSize(ts);
  439. float w = textWidth(s);
  440. this.x = width/2 - w/2;
  441. this.y = height/2 - ts;
  442. }
  443. void draw() {
  444. background(0, 0, 100);
  445. fill(140, 100, 100);
  446. textSize(ts);
  447. text(s, x, y);
  448. }
  449. void update() {
  450. }
  451. boolean isEnd() {
  452. return false;
  453. }
  454. void mousePressed() {
  455. changer.change(new Title(changer));
  456. }
  457. }
  458. interface Task {
  459. void draw();
  460. void update();
  461. boolean isEnd();
  462. void mousePressed();
  463. }
  464.  
  465. abstract class Sprite implements Task {
  466. PVector position;
  467. PVector velocity;
  468. float v;
  469. float w, h;
  470. float d, r;
  471.  
  472. float x() {
  473. return position.x;
  474. }
  475. float y() {
  476. return position.y;
  477. }
  478. void x(float value) {
  479. position.x = value;
  480. }
  481. void y(float value) {
  482. position.y = value;
  483. }
  484. float w() {
  485. return w;
  486. }
  487. float h() {
  488. return h;
  489. }
  490. float d() {
  491. return d;
  492. }
  493. float r() {
  494. return r;
  495. }
  496. float w2() {
  497. return w()/2;
  498. }
  499. float h2() {
  500. return h()/2;
  501. }
  502. float cx(){
  503. return x()+w2();
  504. }
  505. float cy(){
  506. return y()+h2();
  507. }
  508. float xw() {
  509. return x()+w();
  510. }
  511. float yh() {
  512. return y()+h();
  513. }
  514. }
  515. class Stage implements Scene {
  516. Changer changer;
  517.  
  518. Maru maru;
  519. Field field;
  520.  
  521. Group<Teki> tekis;
  522. final int tekiMax = 100;
  523.  
  524. Group<Flag> flags;
  525. final int flagX = 8;
  526. final int flagY = 5;
  527. final int flagsAll = flagX * flagY;
  528.  
  529. Beta beta;
  530. Group<Task> tasks;
  531.  
  532. Stage(Changer changer) {
  533. this.changer = changer;
  534.  
  535. field = new Field(0, 0, width, height);
  536.  
  537. final float d = 20;
  538. maru = new Maru(width/2-d/2, height/2-d/2);
  539.  
  540. tekis = new Group<Teki>();
  541. init_tekis();
  542.  
  543. flags = new Group<Flag>();
  544. init_flags();
  545.  
  546. beta = new Beta(maru, field, tekis, flags);
  547.  
  548. tasks = new Group<Task>();
  549. tasks.add(field);
  550. tasks.add(flags);
  551. tasks.add(tekis);
  552. tasks.add(maru);
  553. tasks.add(beta);
  554. }
  555. void init_tekis() {
  556. for (int i=0; i<tekiMax; i++) {
  557. final float x = random(width);
  558. final float y = random(height);
  559. final float v = 1;
  560. final float theta = random(TWO_PI);
  561. tekis.add(new Teki(x, y, v, theta));
  562. }
  563. }
  564. void init_flags() {
  565. final float dx = width/(flagX+2);
  566. final float dy = height/(flagY+2);
  567. for (int j=0; j<flagY; j++) {
  568. final float y = (j+1) * dy;
  569. for (int i=0; i<flagX; i++) {
  570. final float x = (i+1) * dx;
  571. flags.add(new Flag(x, y));
  572. }
  573. }
  574. }
  575. void draw() {
  576. tasks.draw();
  577. }
  578. void update() {
  579. tasks.update();
  580.  
  581. if(maru.isEnd()){
  582. changer.change(new Failure(changer));
  583. }else if(flags_all_touched()){
  584. changer.change(new Clear(changer));
  585. }
  586. }
  587. boolean flags_all_touched(){
  588. for(Flag flag: flags){
  589. if(!flag.isTouched())return false;
  590. }
  591. return true;
  592. }
  593. boolean isEnd() {
  594. //return tasks.isEnd();
  595. return false;
  596. //return maru.isEnd();
  597. }
  598. void mousePressed() {
  599. tasks.mousePressed();
  600. }
  601. }
  602. class Teki extends Sprite {
  603. int life;
  604.  
  605. Teki(float x, float y, float v, float theta) {
  606. position = new PVector(x, y);
  607. velocity = new PVector(v, 0);
  608. velocity.rotate(theta);
  609. v = 1;
  610. d = 20;
  611. r = d/2;
  612. w = d;
  613. h = d;
  614. life = 1;
  615. }
  616. void draw() {
  617. noFill();
  618. stroke(0, 100, 100);
  619. ellipse(x(), y(), d, d);
  620. }
  621. void update() {
  622. position.add(velocity);
  623. }
  624. void damaged() {
  625. life--;
  626. }
  627. boolean isEnd() {
  628. return life <= 0;
  629. }
  630. void mousePressed() {
  631. }
  632. }
  633.  
  634.  
  635.  
  636. SceneManager sceneManager;
  637.  
  638. void setup() {
  639. size(800, 500);
  640. colorMode(HSB, 360, 100, 100);
  641. sceneManager = new SceneManager();
  642. }
  643. void draw() {
  644. sceneManager.draw();
  645. sceneManager.update();
  646. }
  647. void mousePressed() {
  648. sceneManager.mousePressed();
  649. }
  650.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:177: error: class, interface, or enum expected
import java.lang.Iterable;
^
Main.java:178: error: class, interface, or enum expected
import java.util.Iterator;
^
Main.java:636: error: class, interface, or enum expected
SceneManager sceneManager;
^
Main.java:638: error: class, interface, or enum expected
void setup() {
^
Main.java:640: error: class, interface, or enum expected
  colorMode(HSB, 360, 100, 100);
  ^
Main.java:641: error: class, interface, or enum expected
  sceneManager = new SceneManager();
  ^
Main.java:642: error: class, interface, or enum expected
}
^
Main.java:645: error: class, interface, or enum expected
  sceneManager.update();
  ^
Main.java:646: error: class, interface, or enum expected
}
^
Main.java:649: error: class, interface, or enum expected
}
^
10 errors
stdout
Standard output is empty