Esempio n. 1
0
void Controller::computeTorques(int _currentFrame) {
  mCurrentFrame = _currentFrame;
  mTorques.setZero();
  if (mState == "STAND") {
    stand();
  } else if (mState == "CROUCH") {
    crouch();
  } else if (mState == "JUMP") {
    jump();
  } else if (mState == "REACH") {
    reach();
  } else if (mState == "GRAB") {
    grab();
  } else if (mState == "RELEASE") {
    release();
  } else if (mState == "SWING") {
    swing();
  } else if (mState == "BENDABDOMEN") {
    bendAbdomen();
  } else {
    std::cout << "Illegal state: " << mState << std::endl;
  }

  // Just to make sure no illegal torque is used. Do not remove this.
  for (int i = 0; i < 6; i++) {
    mTorques[i] = 0.0;
  }
}
Esempio n. 2
0
void Babar::update_state()
{
    if (locked() )
        m_lock--;
    if (m_lock == 0)
    {
        unlock();
    }
    if (!Keyboard::GetInstance()->time_pressed (k_jump) )
        m_jump = false;
    if (m_jump)
    {
        if ( Keyboard::GetInstance()->time_pressed (k_jump) > 1 )
        {
            m_ready_double_jump = true;
            if ( Keyboard::GetInstance()->time_pressed (k_jump) > JUMP_TIME)
            {
                m_jump = false;
                Keyboard::GetInstance()->disable_key (k_jump);
            }
        }
    }

    update_direction();

    if ( CollisionsManager::is_down_coll (gCollision->get_matrix()->down_collision_type (position() ) ) )
    {
        interrupt_jump();
    }

    if (can_fire() )
    {
        m_fire = true;
        gProj->add_proj (fire_old(), PLAYER1);
        m_fire_phase = 0;
    }
    else
    {
        m_fire = false;
        m_fire_phase++;
    }

    if (can_crouch() )
    {
        crouch();
    }
    else
    {
        /* si on se releve */
        if ( m_crouch_time )
        {
            interrupt_crouch();
        }
    }

    if (can_jump() )
        jump();

    if (can_double_jump() )
        double_jump();

    if (can_go_down() )
        go_down();

    if (m_invincible > 0)
    {
        m_invincible --;
        if (!locked() )
        {
            if ( m_invincible % 2)
            {
                m_spriteGrid->no_pic();
            }
            else
            {
                m_spriteGrid->set_pic();
            }
        }
    }

    m_weapons_armory.update();

    if (Keyboard::GetInstance()->time_pressed (k_prev_weapon) == 1)
        m_weapons_armory.previous_weapon();

    if (Keyboard::GetInstance()->time_pressed (k_next_weapon) == 1)
        m_weapons_armory.next_weapon();

    switch (get_state() )
    {
    case STATIC:
        m_spriteGrid->setPictures (0);
        break;
    case WALK:
        m_spriteGrid->setPictures (1, 0, 2, 0);
        break;
    case JUMP:
        m_spriteGrid->setPictures (1);
        break;
    case CROUCH:
        m_spriteGrid->setPictures (12);
        break;
    case CROUCH_WALKING:
        m_spriteGrid->setPictures (13, 12, 14, 12);
        break;
    default:
        m_spriteGrid->setPictures (0);
        break;
    }

    //m_spriteGrid->change_anim(get_state(), m_dir, Keyboard::GetInstance()->key_down(k_fire));
    m_spriteGrid->set_pos (position() );
    set_h (m_spriteGrid->h() );
    set_w (m_spriteGrid->w() );
}
Esempio n. 3
0
void PlayerController::update()
{
	// NOTE(juha): Get game controller button states.
	if (gamepad_ready) {
		AButton =	SDL_GameControllerGetButton(ControllerHandle, SDL_CONTROLLER_BUTTON_A);
		XButton =	SDL_GameControllerGetButton(ControllerHandle, SDL_CONTROLLER_BUTTON_X);
		Up =		SDL_GameControllerGetButton(ControllerHandle, SDL_CONTROLLER_BUTTON_DPAD_UP);
		Down =		SDL_GameControllerGetButton(ControllerHandle, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
		Left =		SDL_GameControllerGetButton(ControllerHandle, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
		Right =		SDL_GameControllerGetButton(ControllerHandle, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
	
		// NOTE(juha): Get button up/down states.
		if (!AButton)	AButton_up =	true;
		if (!XButton)	XButton_up =	true;
		if (!Up)		Up_up =			true;
		if (!Down)		Down_up =		true;
		if (!Left)		Left_up =		true;
		if (!Right)		Right_up =		true;
	}

	if (!in_menu) {
		// NOTE(juha): Gravitational stuff
		velocity_y += GRAVITY * (16.f / 1000);
		desired.y += (int)velocity_y;
		crouching = false;
	
		if (velocity_y >= 1) {
			in_air = true;
			knight->falling = true;
			knight->is_landed = false;
		}
	
		if (velocity_y >= 7) {
			velocity_y = 7;
		}
	
		if (!in_air) {
			jumping = false;
		}
	}
	
	// NOTE(juha): If there are only two players in the game and the current
	// player controller is for the fourth player, then lock the controller.
	if (in_menu) {
		if (player == 3 && *players == 2) {
			controller_locked = true;
		} else {
			controller_locked = false;
		}
	} else {
		controller_locked = false;
	}
	
	// NOTE(juha): Reading the inputs for single player and multiplayer.
	
	// NOTE(juha): If no input is given, tmp_imput is 9999.
	int tmp_input = 9999;
	
	// TODO(juha): NEEDS REWRITE!
	// Too many conditions, all parts too similar to eachother,
	// input reading from both controller and keyboard too convoluted.

	// MOVE LEFT
	if (playerInput.keyState(key_left) || (Left && !controller_locked)) {
		if ((playerInput.isKeyPressed(key_left) && playerInput.isKeyDown(key_left)) || 
			(Left_up && !controller_locked && gamepad_ready)) {
			if (!in_menu) {
				if (facing_direction == FACING_LEFT) {
					tmp_input = knight->FORWARD;
				} else {
					tmp_input = knight->BACKWARD;
				}
			}
			if (in_menu) {
				menu_x -= 1;
				sfx_select2.play(1);
			}
		}
		left();
		Left_up = false;
	}
	
	// MOVE RIGHT
	if (playerInput.keyState(key_right) || (Right && !controller_locked)) {
		if ((playerInput.isKeyPressed(key_right) && playerInput.isKeyDown(key_right)) || 
			(Right_up && !controller_locked && gamepad_ready)) {
			if (!in_menu) {
				if (facing_direction == FACING_RIGHT) {
					tmp_input = knight->FORWARD;
				} else {
					tmp_input = knight->BACKWARD;
				}
			}
			menu_x += 1;
			if (in_menu) {
				sfx_select2.play(1);
			}
		}
		right();
		Right_up = false;
	}
		
	// DOWN BUTTON
	if (playerInput.keyState(key_down) || (Down && !controller_locked)) {
		if ((playerInput.isKeyPressed(key_down) && playerInput.isKeyDown(key_down)) || 
			(Down_up && !controller_locked && gamepad_ready)) {
			if (!in_menu) {
				tmp_input = knight->DOWN;
			}
			menu_y += 1;
			if (in_menu) {
				sfx_select2.play(1);
			}
		}
		crouch();
		Down_up = false;
	}
		
	// UP BUTTON
	if (playerInput.keyState(key_up) || (Up && !controller_locked)) {
		if ((playerInput.isKeyPressed(key_up) && playerInput.isKeyDown(key_up)) || 
			(Up_up && !controller_locked && gamepad_ready)) {
			if (!in_menu) {
				tmp_input = knight->UP;
			}
			menu_y -= 1;
			if (in_menu) {
				sfx_select2.play(1);
			}
		}
		up();
		Up_up = false;
	}
	
	// JUMP BUTTON
	if (playerInput.keyState(key_jump) || (AButton && !controller_locked)) {
		if ((playerInput.isKeyPressed(key_jump) && playerInput.isKeyDown(key_jump) && !in_menu) || 
			(AButton_up && !controller_locked && gamepad_ready)) {
			tmp_input = knight->JUMP;
		}
		if (!has_jumped) {
			jump();
			AButton_up = false;
			has_jumped = true;
		}
	}
	// NOTE(juha): Prevent players from holding the jump key down.
	if (playerInput.isKeyUp(key_jump)) {
		has_jumped = false;
	}
		
	// ACTION BUTTON
	if (playerInput.keyState(key_action) || (XButton && !controller_locked)) {
		if ((playerInput.isKeyPressed(key_action) && playerInput.isKeyDown(key_action)) || 
			(XButton_up && !controller_locked && gamepad_ready)) {
			if (!in_menu) {
				tmp_input = knight->ACTION;
			} else {
				if (in_game == false) {
					in_game = true;
					*players += 1;
				}
			}
		}
		if (!has_attacked) {
			basicAttack();
			XButton_up = false;
			has_attacked = true;
		}
	}
	
	// NOTE(juha): Prevent players from holding the attack key down.
	if (playerInput.isKeyUp(key_action)) {
		has_attacked = false;
	}

	// MENU BUTTON
	if (playerInput.keyState(key_menu)) {
	}
	
	if (!in_menu) {
		// NOTE(juha): respawn timer
		if (knight->alive == false) {
			if (deathTimer.isStarted() == false) {
				deathTimer.start();
			}
			if (deathTimer.getTicks() >= RESPAWN_TIME) {
				knight->respawn();
				deathTimer.stop();
			}
		}

		// NOTE(juha): Goes through all the special combos.
		for (int i = 0; i < (*moves).size(); ++i) {
			(*moves)[i].tmp_input = tmp_input;
			
			bool continue_execution = false;
			if ((*moves)[i].keys.size() > 0) {
				
				if ((*moves)[i].keys.size() > 1) {
					// NOTE(juha): If the player just turned, then the first
					// key can be counted as forward instead of backward.
					if ((*moves)[i].keys[0].keycode == knight->FORWARD &&
						(*moves)[i].tmp_input == knight->BACKWARD &&
						(*moves)[i].state == 0) {
						(*moves)[i].tmp_input = knight->FORWARD;
					}

					// NOTE(juha): tmp_input 9999 means that no keys were pressed.
					// NOTE(juha): If a right key was pressed, move combos that had that key
					// to the next state. If nothing was pressed, do nothing. If the key
					// wasn't a part of the combo, then return it to the beginning state.
					if ((*moves)[i].keys[(*moves)[i].state].keycode == (*moves)[i].tmp_input &&
						(*moves)[i].executing == false) {
						(*moves)[i].state++;
					} else if ((*moves)[i].tmp_input == 9999) {
						// Do nothing.
					} else {
						(*moves)[i].state = 0;
					}
				
					// NOTE(juha): For combos that require that the player
					// holds a key down, this loop checks if the key is
					// pressed down as the combo is executed.
					for (unsigned int j = 0; j < (*moves)[i].keys.size(); j++) {
						if ((*moves)[i].keys[j].pressed == true) {

							if ((*moves)[i].keys[j].keycode == knight->FORWARD &&
								facing_direction == FACING_LEFT) {
								
								if (playerInput.isKeyPressed(key_left)) {
									continue_execution = true;
								}
							} else if ((*moves)[i].keys[j].keycode == knight->BACKWARD &&
								facing_direction == FACING_LEFT) {
								
								if (playerInput.isKeyPressed(key_right)) {
									continue_execution = true;
								}
							}  else if ((*moves)[i].keys[j].keycode == knight->FORWARD &&
								facing_direction == FACING_RIGHT) {
								
								if (playerInput.isKeyPressed(key_right)) {
									continue_execution = true;
								}
							}  else if ((*moves)[i].keys[j].keycode == knight->BACKWARD &&
								facing_direction == FACING_RIGHT) {
								
								if (playerInput.isKeyPressed(key_left)) {
									continue_execution = true;
								}
							} else if ((*moves)[i].keys[j].keycode == knight->JUMP) {
								
								if (playerInput.isKeyPressed(key_jump)) {
									continue_execution = true;
								}
							} else if ((*moves)[i].keys[j].keycode == knight->DOWN) {
								
								if (playerInput.isKeyPressed(key_down)) {
									continue_execution = true;
								}
							} else if ((*moves)[i].keys[j].keycode == knight->UP) {
								
								if (playerInput.isKeyPressed(key_up)) {
									continue_execution = true;
								}
							} else if ((*moves)[i].keys[j].keycode == knight->ACTION) {
								
								if (playerInput.isKeyPressed(key_action)) {
									continue_execution = true;
								}
							}

						} else {
							continue_execution = true;
						}
						
						// NOTE(juha): For combos that require that the knight
						// is in air, checks if the knight is in air.
						if ((*moves)[i].keys[j].in_air && !in_air) {
							continue_execution = false;
						}
					}
					
					// NOTE(juha): For combos that require that the knight
					// is on ground, checks if the knight is on ground.
					if ((*moves)[i].in_ground && in_air) {
						continue_execution = false;
					}
					
					if (continue_execution == false) {
						(*moves)[i].state = 0;
					}
					
					// NOTE(juha): How much power do the combos require for
					// execution.
					int combopower = 5;
					if ((*moves)[i].keys.size() == (*moves)[i].state &&
						continue_execution == true) {
						if ((*moves)[i].disabled == false && 
							knight->getSpecialPower() > combopower) {
							(*moves)[i].executing = true;
							executing_combo = true;
							knight->executeCombo(combopower);
							printf("executing\n");
						}
						(*moves)[i].state = 0;
					}
				}
			}
		}

		if (jumping) {
			jump();
		}
	
		// NOTE(juha): Calculate horizontal movement.
		velocity_x += ((targetVx - velocity_x) * acceleration) * 0.16667f;
	
		if (fabs(velocity_x) > speed) {
			velocity_x = (velocity_x > 0) ? (speed+1) : -speed;
		}

		if (fabs(velocity_x) < stoppedThreshold) {
			velocity_x = 0;
		}

		if (facing_direction == FACING_RIGHT) {
			desired.x += (int)ceilf(fabs(velocity_x));
		} else {
			desired.x -= (int)ceilf(fabs(velocity_x));
		}

		int deduct = 0;
		attack_hb.y = desired.y + 8;
		targetVx = 0;

		// NOTE(juha): Movements are effects that move the character.
		if (movements.size() > 0) {
			for (unsigned int i = 0; i < movements.size(); i++) {
				if (movements.at(i).executing &&
					movements.at(i).distance_travelled < movements.at(i).range) {
				
					double new_x = movements.at(0).speed * cos(movements.at(i).angle * PI / 180);
					double new_y = movements.at(0).speed * sin(movements.at(i).angle * PI / 180);
	
					if (facing_direction == 1) {
						desired.x += (int)new_x;
					} else {
						desired.x -= (int)new_x;
					}

					desired.y -= (int)new_y;
					movements.at(i).distance_travelled += movements.at(i).speed;
				} else {
					movements.erase(movements.begin() + i);
				}
			}
		}
	}
}