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

Holding down a movement key makes player move too fast. #5

Open
zurdmont opened this issue Jul 4, 2018 · 2 comments
Open

Holding down a movement key makes player move too fast. #5

zurdmont opened this issue Jul 4, 2018 · 2 comments

Comments

@zurdmont
Copy link

zurdmont commented Jul 4, 2018

If I have an enemy (who has the same movement speed/frequency as the player) right next to me and move one square at a time, the enemy will keep pace and I'll never gain any space between us. This is working as intended.

If I hold the movement key down however, I start gaining space and eventually completely lose the monster. I'm not sure if this is because of my slow laptop; I tried seeing if it happens to you on stream, it doesn't look like it but I can't tell. I'm only on day 20 but I tested on your final build and it still happens.

@JimmyJames707
Copy link

JimmyJames707 commented Sep 4, 2018

I just checked again and see you are right. Weird. Perhaps it is occurring in dark.c.

@JimmyJames707
Copy link

JimmyJames707 commented Sep 4, 2018

Okay I found the problem. When you press and hold it goes though the SDL_PollEvent loop multiple times for the same key press and this will change player's position multiple times before the other creatures are told to move. This fragment fixes it for Day19 inside dark.c:

bool done = false;
bool recalculateFOV = false;
bool playerMoved = false;
Position newPos;

while (!done) {

	playerMoved = false;

	SDL_Event event;
	u32 timePerFrame = 1000 / FPS_LIMIT;
	u32 frameStart = 0;
	while (SDL_PollEvent(&event) != 0) {
		frameStart = SDL_GetTicks();

		if (event.type == SDL_QUIT) {
			done = true; 
			break;
		}

		if (event.type == SDL_KEYDOWN) {
			SDL_Keycode key = event.key.keysym.sym;

			Position *playerPos = (Position *)game_object_get_component(player, COMP_POSITION);

			switch (key) {

				// DEBUG
				case SDLK_m:
					level_init(player);
					break;
				// END DEBUG

				case SDLK_ESCAPE:
					done = true;
					break;

				case SDLK_UP: {
					newPos = (Position){playerPos->objectId, playerPos->x, playerPos->y - 1, LAYER_TOP};
					if (can_move(newPos)) {
						//game_object_add_component(player, COMP_POSITION, &newPos);
						recalculateFOV = true;					
						playerMoved = true;		
					}
				}
				break;

				case SDLK_DOWN: {
					newPos = (Position){playerPos->objectId, playerPos->x, playerPos->y + 1, LAYER_TOP};
					if (can_move(newPos)) {
						//game_object_add_component(player, COMP_POSITION, &newPos);							
						recalculateFOV = true;							
						playerMoved = true;		
					}
				}
				break;

				case SDLK_LEFT: {
					newPos = (Position){playerPos->objectId, playerPos->x - 1, playerPos->y, LAYER_TOP};
					if (can_move(newPos)) {
						//game_object_add_component(player, COMP_POSITION, &newPos);							
						recalculateFOV = true;							
						playerMoved = true;		
					}
				}
				break;

				case SDLK_RIGHT: {
					newPos = (Position){playerPos->objectId, playerPos->x + 1, playerPos->y, LAYER_TOP};
					if (can_move(newPos)) {
						//game_object_add_component(player, COMP_POSITION, &newPos);							
						recalculateFOV = true;							
						playerMoved = true;		
					}
				}
				break;

				default:
					break;
			}
		}
	}

	// Have things move themselves around the dungeon if the player moved
	if (playerMoved) {
		if (targetMap != NULL) {
			free(targetMap);
		}

		// Position *playerPos = (Position *)game_object_get_component(player, COMP_POSITION);
		// generate_target_map(playerPos->x, playerPos->y);
		game_object_add_component(player, COMP_POSITION, &newPos);
		generate_target_map(newPos.x, newPos.y);

		movement_update();			
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants