{"action":"created","issue":{"url":"https://api.github.com/repos/lolzvid/undertale_techdemo/issues/5","labels_url":"https://api.github.com/repos/lolzvid/undertale_techdemo/issues/5/labels{/name}","comments_url":"https://api.github.com/repos/lolzvid/undertale_techdemo/issues/5/comments","events_url":"https://api.github.com/repos/lolzvid/undertale_techdemo/issues/5/events","html_url":"https://github.com/lolzvid/undertale_techdemo/pull/5","id":120649691,"number":5,"title":"Refactor player logic","user":{"login":"hlixed","id":12309977,"avatar_url":"https://avatars.githubusercontent.com/u/12309977?v=3","gravatar_id":"","url":"https://api.github.com/users/hlixed","html_url":"https://github.com/hlixed","followers_url":"https://api.github.com/users/hlixed/followers","following_url":"https://api.github.com/users/hlixed/following{/other_user}","gists_url":"https://api.github.com/users/hlixed/gists{/gist_id}","starred_url":"https://api.github.com/users/hlixed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hlixed/subscriptions","organizations_url":"https://api.github.com/users/hlixed/orgs","repos_url":"https://api.github.com/users/hlixed/repos","events_url":"https://api.github.com/users/hlixed/events{/privacy}","received_events_url":"https://api.github.com/users/hlixed/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-12-06T18:18:37Z","updated_at":"2015-12-08T21:59:35Z","closed_at":"2015-12-06T18:42:12Z","pull_request":{"url":"https://api.github.com/repos/lolzvid/undertale_techdemo/pulls/5","html_url":"https://github.com/lolzvid/undertale_techdemo/pull/5","diff_url":"https://github.com/lolzvid/undertale_techdemo/pull/5.diff","patch_url":"https://github.com/lolzvid/undertale_techdemo/pull/5.patch"},"body":"Hahaha... It's just like you to make several if-statements in a row that all do the same thing. BUT NOT TODAY! Messy. Messy! MESSY! That code has too many if-statements for player direction, each doing nearly THE SAME THING, for over 16 CALS! Not good. NOT GOOD! It's time for a refactoring!\r\n\r\nHaha! See this? \r\n\r\nsf2d_texture* tex_arr_friskWalk [4][4];\r\n\r\nIt's a multidimensional array! This code... Creates a 4-by-4 array of sf2d_texture*-s! MORE THAN JUST ONE! It makes me so MAD to see this code repitition! Us spectres can fit several ghosts into one little variable name, and we can then access them like so:\r\n\r\ncurr_tex = tex_arr_friskWalk[0][2];\r\n\r\nNow, listen up. Listen up! LISTEN UP! I made the first array index represent the direction, and the 2nd index represent the frame, but you can make it WHATEVER YOU WANT! HAHAHA! tex_arr_friskWalk [0][2] is the 3rd frame (Remember! Arrays start from 0, human!) of the right-walking animation. Efficient, huh? Now you can plug in sprTimer into that last index like so:\r\n\r\ncurr_tex = tex_arr_friskWalk [0][(int)sprTimer];\r\n\r\nand now all you have to do is update sprTimer! Efficiency. Efficiency. EFFICIENCY! And that's NOT ALL! For convenience, I also created a few VARIABLES like FRISK_RIGHT=0, so you can PLUG THEM INTO THE FIRST BRACKET! Hahaha! No need for remembering what each number represents, because that might lead to ERRORS:\r\n\r\ncurr_tex = tex_arr_friskWalk [FRISK_RIGHT][(int)sprTimer];\r\n\r\nBeautiful. Beautiful! BEAUTIFUL! Look at how nice-looking that is! And all we did is OFFLOAD THOSE TEXTURES to the MULTIDIMENSIONAL ARRAY!\r\n\r\nBut. But! BUT! But when you start the program,YOU NEED TO CALL sfil_load_PNG_buffer()! Could this mean writing out 16 different sfil_load_PNG_buffer() calls? Never. Never! NEVER! Instead, we can use FOR LOOPS to access EVERY ELEMENT IN ORDER! HAHAHA!\r\n\r\n\tint i;\r\n\tint j;\r\n\r\n\tfor (i=0;i<4;i++){ \r\n\t\tfor (j=0;j<4;j++){\r\n\t\t\ttex_arr_friskWalk[i][j] = sfil_load_PNG_buffer(friskFilenames[i][j], SF2D_PLACE_RAM);\r\n\t\t}\r\n\t}\r\n\r\nNow, EVERYTHING is loaded! And we can use the same technique for sf2d_free_texture().\r\n\r\nNow. Now! NOW! We have EFFICIENCY! It makes me SO GLAD! Now, WE HALVED THE SIZE OF THE CODE! HAHAHA!\r\n\r\nif(playerDir == 0){\r\n  \t\tif (hsp == 0) {\r\n\t\t\tcurr_tex = tex_arr_friskWalk[FRISK_RIGHT][0];\r\n\t\t}\r\n\t\telse {\r\n\t\t\tcurr_tex = tex_arr_friskWalk[FRISK_RIGHT][(int)floor(sprTimer)];\r\n\t\t}\r\n}\r\nif(playerDir == 1){\r\n....\r\n\r\nComputers are dummies. That's why you've gotta TELL THEM what to DO! This works as long as sprTimer NEVER goes above 4. Ever. Ever! EVER!\r\n\r\n![Mad Dummy](http://vignette4.wikia.nocookie.net/undertale/images/8/84/Maddummy.gif/revision/latest?cb=20151122074929)\r\n\r\n**HEY, YOU!**\r\n\r\nWho, me?\r\n\r\n**YEAH, YOU!!! YOU'RE TRYIN' TO STEAL MY STYLE, HUH? Dummy. Dummy! DUMMY! YOU CAN GO FURTHER THAN THAT! Lemme show you. YOU'RE FIRED!**\r\n\r\n**Foolish. Foolish! FOOLISH! We can do EVEN BETTER! You've got two \"curr_tex =\" calls per playerDir. Inefficient. Inefficient! INEFFICIENT! Those calls are doing the SAME THING WITH MINOR DIFFERENCES! YOU CAN REFACTOR!!**\r\n\r\n**Dummy! Instead of just pasting in FRISK_RIGHT and FRISK_LEFT and all that, why not just plug in playerDir? Hahaha. Hahaha! HAHAHA! I DON'T NEED IF-STATEMENTS. I'VE GOT VARIABLES!!!**\r\n\r\n\t if (hsp == 0) {\r\n\t\tcurr_tex = tex_arr_friskWalk[playerDir][0];\r\n\t}\r\n\telse {\r\n\t\tcurr_tex = tex_arr_friskWalk[playerDir][(int)floor(sprTimer)];\r\n\t}\r\n\r\n**Now, you've only got TWO LINES of code! You're changing playerDir already, right? Reuse. Reuse! REUSE! Multidimensional arrays are for storing similar-typed data all together. The efficiency is MAD!**\r\n\r\n**Alright, you're good to go. I'M OUTTA HERE!**\r\n\r\nOh, and you might eventually want to move the room data to something similar; look up structs if you want. Enjoy!"},"comment":{"url":"https://api.github.com/repos/lolzvid/undertale_techdemo/issues/comments/163031498","html_url":"https://github.com/lolzvid/undertale_techdemo/pull/5#issuecomment-163031498","issue_url":"https://api.github.com/repos/lolzvid/undertale_techdemo/issues/5","id":163031498,"user":{"login":"Xavyrr","id":10913345,"avatar_url":"https://avatars.githubusercontent.com/u/10913345?v=3","gravatar_id":"","url":"https://api.github.com/users/Xavyrr","html_url":"https://github.com/Xavyrr","followers_url":"https://api.github.com/users/Xavyrr/followers","following_url":"https://api.github.com/users/Xavyrr/following{/other_user}","gists_url":"https://api.github.com/users/Xavyrr/gists{/gist_id}","starred_url":"https://api.github.com/users/Xavyrr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Xavyrr/subscriptions","organizations_url":"https://api.github.com/users/Xavyrr/orgs","repos_url":"https://api.github.com/users/Xavyrr/repos","events_url":"https://api.github.com/users/Xavyrr/events{/privacy}","received_events_url":"https://api.github.com/users/Xavyrr/received_events","type":"User","site_admin":false},"created_at":"2015-12-08T21:59:35Z","updated_at":"2015-12-08T21:59:35Z","body":"It should actually be \r\nif (hsp == 0) {\r\n   if (vsp == 0)\r\n      //dostuff\r\n   }\r\n}\r\nWhile this takes up more code, it's actually faster since it only does the first check, and then checks the second if the first is true. When they're combined it does both checks regardless even if the first check is false. ;) "}}