Skip to content

Movements

runx1ng edited this page Oct 18, 2021 · 7 revisions

At the beginning of the round, the player starts from the spawn point. Players will have two basic options to control the character by using the up (W) and down (S) buttons on the player's keyboard. There are another two extra keys Q and E to help the player jump to the very top lane or the very bottom lane.

The reason to implement these two extra movements is that since the game is a complete 2D game so we cannot implement a jump method for the player since it will look so weird if the player jumps. Moreover, having these two extra movement methods allows the player to survive longer there are obstacles on the player’s current lane or the lane beside the player.

To let the player move on the map, the method we use is keyDown() method and this could detect what the player pressed on the keyboard and do the correct movement.

In this method, we used the setPosition() method to set the player to the correct location where location is the y-coordinate for each lane. Also, to get the y-coordinate for each lane we first need to get the bridge that we’re using for the game, and then we need to store the values for each lane into a list. However, the y-coordinates returned from the getLane() method is quite weird and what we can do is to add some calculations to make it correct.

...
public boolean keyDown(int keycode) {
    Bridge bridge = RagnorakRacer.rainbowBridge;
    List<Lane> lanes = bridge.getLanes();

    float newY = 0;
    if (keycode == Keys.W){
      if(entity.getPosition().y <= lanes.get(3).getMid() - 9){
        System.out.println(lanes.get(3).getMid() - 7.5);
        entity.setPosition(entity.getPosition().x, lanes.get(i).getMid() - (1.7f * (i + 1) + j));
        newY = lanes.get(i).getMid() - (1.6f * (i + 1));
        if (i <= 2){
          i += 1;
        }
        if (j <= 0.14f){
          j += 0.03f;
        }
        System.out.println("current lane number:" + i);
        System.out.println(newY);
      }
    } else if (keycode == Keys.S){
      if (entity.getPosition().y >= lanes.get(0).getMid() - 4){
          System.out.println(lanes.get(0).getMid());
          if (i > 0){
            entity.setPosition(entity.getPosition().x, lanes.get(i - 1).getMid() - (1.7f * (i + 1)));
            i -= 1;
            if (j >= 0.3f){
              j -= 0.03f;
            }
          }

          System.out.println("current lane number:" + i);
      }
    }else if (keycode == Keys.E){

      entity.setPosition(entity.getPosition().x, lanes.get(0).getMid() - (1.6f * (1 + 1)));
      i = 0;
    }else if (keycode == Keys.Q){
      entity.setPosition(entity.getPosition().x, lanes.get(3).getMid() - (1.6f * (4 + 1)));
      i = 3;
    }else if (keycode == Keys.SPACE){
      entity.getEvents().trigger("attack");
    }
    return true;
  }
...

We also used some static variables like i to keep track of which lane is the player currently on and j for the y-coordinate calculations.

...
public static int i = 0;
public static float j = 0.5f;
...

Table of Contents

Home

Game Design

Player Health System, Scoring System & Game Over
  • Enemies

Enemies
Enemies testing and testing plan
Player Interaction Sprint1 & Sprint2
Player Interaction Sprint3
Player Interaction Sprint4
Map contents Sprint1-3
Map contents Sprint4

Game Features

Game Engine

Getting Started

Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally