slotmachine.pde 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. Game slotmachine = new Game(500, 400);
  2. void setup() {
  3. size(500, 400);
  4. background(0,0,0);
  5. text("Loading...", 20, 20);
  6. slotmachine.init();
  7. }
  8. void draw() {
  9. background(0,0,0);
  10. slotmachine.update();
  11. slotmachine.draw();
  12. }
  13. void mousePressed() {
  14. slotmachine.mousePressed(mouseX, mouseY, mouseButton);
  15. }
  16. void mouseReleased() {
  17. slotmachine.mouseReleased(mouseX, mouseY);
  18. }
  19. void mouseMoved() {
  20. slotmachine.mouseMoved(mouseX, mouseY);
  21. }
  22. void mouseDragged() {
  23. slotmachine.mouseDragged(mouseX, mouseY);
  24. }
  25. class Button {
  26. private float[] coords;
  27. private Event event;
  28. private float[] bounds;
  29. private Event hoverEvent;
  30. private Event clickEvent;
  31. private int[] fillColor;
  32. private int[] borderColor;
  33. private boolean previousPress = false;
  34. Button(Event event) {
  35. this.coords = new float[2];
  36. this.bounds = new float[2];
  37. this.fillColor = new int[3];
  38. this.borderColor = new int[3];
  39. this.hoverEvent = new Event(new String[]{"hover"});
  40. this.clickEvent = new Event(new String[]{"left", "right"});
  41. this.event = event;
  42. }
  43. void setPosition(float[] coords) {
  44. this.coords = coords;
  45. }
  46. void setBounds(float[] bounds) {
  47. this.bounds = bounds;
  48. }
  49. void setFillColor(int[] fillColor) {
  50. this.fillColor = fillColor;
  51. }
  52. void setBorderColor(int[] borderColor) {
  53. this.borderColor = borderColor;
  54. }
  55. void update() {
  56. if(this.hoverEvent.isRaised("hover")) {
  57. if(this.clickEvent.isRaised("left")) {
  58. this.previousPress = true;
  59. this.fillColor = new int[]{255, 0, 0};
  60. } else {
  61. this.fillColor = new int[]{64, 64, 255};
  62. }
  63. } else {
  64. this.fillColor = new int[]{0, 0, 255};
  65. }
  66. }
  67. void draw() {
  68. fill(this.fillColor[0], this.fillColor[1], this.fillColor[2]);
  69. stroke(this.borderColor[0], this.borderColor[1], this.borderColor[2]);
  70. rect(this.coords[0], this.coords[1], this.bounds[0], this.bounds[1]);
  71. fill(this.borderColor[0], this.borderColor[1], this.borderColor[2]);
  72. text("SPIN", this.coords[0] + (this.bounds[0] / 2) - 15, this.coords[1] + (this.bounds[1] / 2));
  73. }
  74. void mouseMoved(int mouseX, int mouseY) {
  75. if (mouseX > this.coords[0] && mouseX < this.coords[0] + this.bounds[0] &&
  76. mouseY > this.coords[1] && mouseY < this.coords[1] + this.bounds[1]) {
  77. this.hoverEvent.raiseEvent("hover");
  78. } else {
  79. this.hoverEvent.clear();
  80. }
  81. }
  82. void mousePressed(int mouseX, int mouseY, int mouseButton) {
  83. if(this.hoverEvent.isRaised("hover")) {
  84. if(mouseButton == LEFT) {
  85. this.clickEvent.raiseEvent("left");
  86. } else if (mouseButton == RIGHT) {
  87. this.clickEvent.raiseEvent("right");
  88. }
  89. }
  90. }
  91. void mouseReleased(int mouseX, int mouseY) {
  92. this.clickEvent.clear();
  93. if(this.previousPress && this.hoverEvent.isRaised("hover")) {
  94. this.event.raiseEvent("spinButtonClicked");
  95. this.previousPress = false;
  96. }
  97. }
  98. void mouseDragged(int mouseX, int mouseY) {
  99. this.mouseMoved(mouseX, mouseY);
  100. }
  101. }
  102. class Event {
  103. String[] eventKeys;
  104. boolean[] raised;
  105. Event(String[] eventKeys) {
  106. this.eventKeys = eventKeys;
  107. this.raised = new boolean[eventKeys.length];
  108. }
  109. void raiseEvent(String event) {
  110. int eventIndex = this.getIndexOfValue(event);
  111. if(eventIndex >= 0) {
  112. this.raised[eventIndex] = true;
  113. }
  114. }
  115. boolean isRaised(String event) {
  116. int eventIndex = this.getIndexOfValue(event);
  117. if(eventIndex >= 0) {
  118. return this.raised[eventIndex];
  119. }
  120. return false;
  121. }
  122. private int getIndexOfValue(String key) {
  123. for(int i = 0; i < this.eventKeys.length; i++) {
  124. if(key == this.eventKeys[i]) {
  125. return i;
  126. }
  127. }
  128. return -1;
  129. }
  130. void clear() {
  131. this.raised = new boolean[eventKeys.length];
  132. }
  133. void clearEvent(String event) {
  134. println("clearing " + event);
  135. int eventIndex = this.getIndexOfValue(event);
  136. if(eventIndex >= 0) {
  137. this.raised[eventIndex] = false;
  138. }
  139. }
  140. }
  141. class Game {
  142. private float width;
  143. private float height;
  144. private Button spinButton;
  145. private Reel[] reels;
  146. private Event[] gameEvents;
  147. private Event[] reelEvents;
  148. private boolean startedSpin;
  149. private int[] lastSpinResults;
  150. private int lastWinAmount;
  151. Game(float width, float height) {
  152. this.width = width;
  153. this.height = height;
  154. this.reelEvents = new Event[]{
  155. new Event(new String[]{"reelDone"})
  156. , new Event(new String[]{"reelDone"})
  157. , new Event(new String[]{"reelDone"})
  158. , new Event(new String[]{"reelDone"})
  159. , new Event(new String[]{"reelDone"})
  160. };
  161. this.gameEvents = new Event[]{
  162. new Event(new String[]{"spinButtonClicked"})
  163. };
  164. this.spinButton = new Button(this.gameEvents[0]);
  165. this.reels = new Reel[]{
  166. new Reel(null, this.reelEvents[0])
  167. ,new Reel(this.reelEvents[0], this.reelEvents[1])
  168. ,new Reel(this.reelEvents[1], this.reelEvents[2])
  169. ,new Reel(this.reelEvents[2], this.reelEvents[3])
  170. ,new Reel(this.reelEvents[3], this.reelEvents[4])
  171. };
  172. }
  173. void init() {
  174. this.lastWinAmount = 0;
  175. this.startedSpin = false;
  176. this.spinButton.setPosition(new float[]{this.width - 90, this.height - 90});
  177. this.spinButton.setBounds(new float[]{80, 80});
  178. for(int i = 0; i < this.reels.length; i++) {
  179. this.reels[i].init();
  180. this.reels[i].setPosition(new float[]{((i + 1) * 15) + (i * 80), 20});
  181. this.reels[i].setFillColor(new int[]{0, 0, 0});
  182. this.reels[i].setBorderColor(new int[]{255, 0, 0});
  183. }
  184. this.lastSpinResults = new int[this.reels.length];
  185. }
  186. void draw() {
  187. this.spinButton.draw();
  188. for(int i = 0; i < this.reels.length; i++) {
  189. this.reels[i].draw();
  190. }
  191. text("Last win: " + this.lastWinAmount, this.width / 2, this.height - 50);
  192. }
  193. void update() {
  194. this.spinButton.update();
  195. if(this.gameEvents[0].isRaised("spinButtonClicked")) {
  196. this.startedSpin = true;
  197. this.lastSpinResults = new int[this.reels.length];
  198. for(int i = 0; i < this.reels.length; i++) {
  199. this.lastSpinResults[i] = this.reels[i].spin(i);
  200. }
  201. }
  202. if(this.startedSpin && this.reelEvents[this.reelEvents.length - 1].isRaised("reelDone")) {
  203. for(int i = 0; i < this.reelEvents.length; i++) {
  204. this.reelEvents[i].clear();
  205. this.startedSpin = false;
  206. }
  207. this.rewardCalculation(this.lastSpinResults);
  208. }
  209. for(int i = 0; i < this.reels.length; i++) {
  210. this.reels[i].update();
  211. }
  212. for(int i = 0; i < this.gameEvents.length; i++) {
  213. this.gameEvents[i].clear();
  214. }
  215. }
  216. private void rewardCalculation(int[] spinResults) {
  217. int prize = 0;
  218. int dupeCount = 0;
  219. for(int i = 0; i < spinResults.length; i++) {
  220. if (i < spinResults.length - 1) {
  221. if(spinResults[i] == spinResults[i + 1]) {
  222. dupeCount += 1;
  223. prize += spinResults.length;
  224. prize *= dupeCount;
  225. } else {
  226. dupeCount = 0;
  227. }
  228. }
  229. }
  230. this.lastWinAmount = prize;
  231. }
  232. void mousePressed(int mouseX, int mouseY, int mouseButton) {
  233. this.spinButton.mousePressed(mouseX, mouseY, mouseButton);
  234. }
  235. void mouseReleased(int mouseX, int mouseY) {
  236. this.spinButton.mouseReleased(mouseX, mouseY);
  237. }
  238. void mouseMoved(int mouseX, int mouseY) {
  239. this.spinButton.mouseMoved(mouseX, mouseY);
  240. }
  241. void mouseDragged(int mouseX, int mouseY) {
  242. this.spinButton.mouseDragged(mouseX, mouseY);
  243. }
  244. }
  245. class Reel {
  246. private float[] coords;
  247. private int[] fillColor;
  248. private int[] borderColor;
  249. private String[] reelList;
  250. private int currentReelIndex;
  251. private int nextReelIndex;
  252. private Event previousReel;
  253. private Event currentReel;
  254. private boolean isSpinning;
  255. Reel(Event previousReel, Event currentReel) {
  256. this.previousReel = previousReel;
  257. this.currentReel = currentReel;
  258. this.coords = new float[2];
  259. this.reelList = new String[14];
  260. }
  261. void init() {
  262. this.currentReelIndex = this.reelList.length - 1;
  263. this.nextReelIndex = 1;
  264. this.isSpinning = true;
  265. this.reelList[0] = "iron"; //pull
  266. this.reelList[1] = "steel"; //push
  267. this.reelList[2] = "tin"; //senses
  268. this.reelList[3] = "pewter"; //strength
  269. this.reelList[4] = "zinc"; //rioter
  270. this.reelList[5] = "brass"; //soother
  271. this.reelList[6] = "copper"; //hide allomancy
  272. this.reelList[7] = "bronze"; //sense allomancy
  273. //this.reelList[0] = "cadmium"; //slow time bubble
  274. //this.reelList[0] = "bendalloy"; //fast time bubble
  275. this.reelList[8] = "gold"; //see past with different choices
  276. this.reelList[9] = "electrum"; //see future
  277. //this.reelList[0] = "chromium"; //destroy another's metals
  278. //this.reelList[0] = "nicrosil"; //flare another's metals
  279. this.reelList[10] = "aluminium"; //destroy self metals
  280. this.reelList[11] = "duralumin"; //flare self metals
  281. this.reelList[12] = "atium"; //see few seconds into the future
  282. this.reelList[13] = "malatium"; //see someone else's possible lives
  283. //this.reelList[0] = "lerasium"; //make mistborn
  284. }
  285. void setPosition(float[] coords) {
  286. this.coords = coords;
  287. }
  288. void setFillColor(int[] fillColor) {
  289. this.fillColor = fillColor;
  290. }
  291. void setBorderColor(int[] borderColor) {
  292. this.borderColor = borderColor;
  293. }
  294. int spin(int seed) {
  295. this.nextReelIndex = (int)random(0, this.reelList.length);
  296. this.isSpinning = true;
  297. return this.nextReelIndex;
  298. }
  299. void draw() {
  300. fill(this.fillColor[0], this.fillColor[1], this.fillColor[2]);
  301. stroke(this.borderColor[0], this.borderColor[1], this.borderColor[2]);
  302. rect(this.coords[0], this.coords[1], 80, 180);
  303. fill(255, 255, 255);
  304. int previousReelIndex = this.currentReelIndex - 1;
  305. int nextReelIndex = this.currentReelIndex + 1;
  306. if(previousReelIndex < 0) {
  307. previousReelIndex = this.reelList.length - 1;
  308. }
  309. if(nextReelIndex > this.reelList.length - 1) {
  310. nextReelIndex = 0;
  311. }
  312. text(this.reelList[previousReelIndex], this.coords[0] + 5, this.coords[1] + 30);
  313. text(this.reelList[this.currentReelIndex], this.coords[0] + 5, this.coords[1] + 90);
  314. text(this.reelList[nextReelIndex], this.coords[0] + 5, this.coords[1] + 150);
  315. }
  316. void update() {
  317. if(this.currentReelIndex == this.nextReelIndex && (this.previousReel == null || this.previousReel.isRaised("reelDone"))) {
  318. this.isSpinning = false;
  319. this.currentReel.raiseEvent("reelDone");
  320. }
  321. if(isSpinning) {
  322. this.currentReelIndex += 1;
  323. }
  324. if(this.currentReelIndex > this.reelList.length - 1) {
  325. this.currentReelIndex = 0;
  326. }
  327. }
  328. }