Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactoring #7

Open
coderman64 opened this issue Jan 11, 2022 · 4 comments
Open

Code refactoring #7

coderman64 opened this issue Jan 11, 2022 · 4 comments
Labels
bug Something isn't working help wanted Extra attention is needed meta an Issue that enapsulates a wider subset of problems

Comments

@coderman64
Copy link
Owner

The codebase is currently very messy. Here are some examples of things to watch out for:

  • Dead code
  • "Magic numbers"
  • bad/non-descriptive variable names (I think goingLeft is true only when the character is going right?)
  • bad commenting
    This is stuff that is likely to get fixed slowly over time, but if you find anything egregious, talk about it in this thread.
@coderman64 coderman64 added bug Something isn't working help wanted Extra attention is needed meta an Issue that enapsulates a wider subset of problems labels Jan 11, 2022
@pink-bot
Copy link
Contributor

In my version I changed the main collision/wall state section in index.js from a nested if/then/else statement to a switch statement. Its posted in my branch but I was trying to implement sonic 3 slope physics in a really messy way so I'm not sure how much help it will be.

I didn't mess with goingleft in my version because it looked like it was being used for homing attack code in some way, ended up creating a separate redundant variable called "mirrored" so that sonic could face/run/slide backwards.

`else if (!(keysDown[86] && devMode) && keysDown[leftKey] && (char.golock <= 0 || char.Gv < 0) && char.state != -1) { //normal state with left key down
char.goingLeft = false;
if (char.Gv > 0) {
char.Gv -= char.DEC;
char.currentAnim = anim.skid;
}
else if (char.Gv > -char.TOP) {
char.Gv -= char.ACC;
if (char.Gv < -char.TOP) {
char.Gv = -char.TOP;
}
}

		if (char.Gv < 0) {
			char.mirrored=true
			if (Math.abs(char.Gv) >= char.TOP) {
				char.currentAnim = anim.run;
				char.animSpeed = Math.abs(char.Gv) / 40 + 0.1;
			}
			else {
				char.currentAnim = anim.jog;
				char.animSpeed = Math.abs(char.Gv) / 40 + 0.1;
			}
		}
	}`

@pink-bot
Copy link
Contributor

pink-bot commented Feb 4, 2022

What exactly are magic numbers? Ive never heard the term before but there are some mathematical equations in the code that I had to google to figure out.

@coderman64
Copy link
Owner Author

Magic numbers in coding refer to hardcoded numbers that should really be stored in well-named constants. So, for example, in the code you posted earlier, I'd consider 40 and 0.1 to be "magic numbers." They're called that because they are usually hard to understand in context, so they just seem like they "magically" do what you want.

@pink-bot
Copy link
Contributor

pink-bot commented Feb 5, 2022

I probably should have asked earlier but would you rather replace magic numbers with variables or constants? Also, I think it might be helpful to replace certain complex math equations with functions, do you agree with this?

edit: any ideas for cleaning up the code that checks for keyboard inputs? It's pretty messy hard to read without adding comments but Im not sure how else you could have done it
edit: this is in my latest pull request but I found code that actually sets goingleft to the opposite direction of whatever key you press. but when the variable is used for charging the spindash, "goingleft = true" leads to a positive ground speed, so it's actually double backwards and the spindash still charges normally.

got some stuff i want to fix/change myself but waiting for the comment branch to get merged first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed meta an Issue that enapsulates a wider subset of problems
Projects
None yet
Development

No branches or pull requests

2 participants