HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Wall-runner type game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
typegamerunnerwall

Problem

I'm working on a Wall-runner reaction based game. Whilst I know my way around a lot of the features Unity provides, I'm still in the beginner phase when it comes to working with several classes and functions simultaneously.

I have currently got the two following classes that work together:

Obstacle class

```
public class ObstacleSpawn : MonoBehaviour {

//Reaction times: 1.0(4F), 0.9(3.6F), 0.8(3.2F), 0.7(2.8F), 0.6(2.4F), 0.5(2F), 0.4(1.6F), 0.35(1.4F), 0.3(1.2F), 0.25(1F);

public PlayerScript pScript;

public GameObject player;
public GameObject obstacle;
public GameObject endGamePanel;
public GameObject infoText;
public Text Average;
public Text BestReaction;

public GameObject instantiatedObstacle;

public float randomSpawnMin;
public float randomSpawnMax;
public float endingSpeed;
public float speed;
public float canvasMoveTowardsSpeed;
public float spawnTime;
public float maxReactionTime;
public float reactionTime;

float obstacleDimensionY;
float spawnTimeDistance;
float reactionClick;
float amountOfTries;
float panelAlpha = 0F;
float gamePanelAlpha = 1F;
private float[] reactionTimeArray = new float[10];

public int maxClicks;

public bool canSpawn = true;

bool reactionCap;
bool pointOneChange;

int fade = 0;

// Use this for initialization
void Start () {
spawnTimeDistance = 4F;
maxReactionTime = 1F;

InvokeRepeating ("Spawn", Random.Range (randomSpawnMin, randomSpawnMax), Random.Range (randomSpawnMin, randomSpawnMax));

Vector2 sprite_size = obstacle.GetComponent ().sprite.rect.size;
Vector2 spriteScale = obstacle.transform.localScale;
float sizeAndScaleY = sprite_size.y * spriteScale.y;
float obstacle_local_sprite_sizeY = (sizeAndScaleY / obstacle.GetComponent ().sprite.pixelsPerUnit) * 0.5F;
obstacleDimensionY = obstacle_local_sprite_sizeY;
}

// Update is

Solution

The thing that sticks out to me the most is there is a lot of similar or duplicate code. Let's look at the code for assigning the reactionTime:

if (reactionTimeArray [0] == 0) {
    reactionTimeArray [0] = reactionTime;
} else if (reactionTimeArray [1] == 0) {
    reactionTimeArray [1] = reactionTime;
// etc up to 9


The only thing that changes for each if statement is the index is incremented, so we can replace all of the if statements with a for loop:

for (int i = 0; i < 10; i++)
{
    if(reactionTimeArray[i] == 0)
    {
        reactionTimeArray[i] = reactionTime;
        break;
    }
}


Calculating averageReaction and bestReaction can also be done using loops. There are LINQ methods to make this even easier. This requires using System.Linq;.

averageReaction = reactionTimeArray.Sum() / 10;
float bestReaction = reactionTimeArray.Min();


Jumping up there's this chunk of code:

if (maxReactionTime >= 0.4F) {
    pointOneChange = true 
} else if (maxReactionTime < 0.4F) {
    pointOneChange = false;
}


If maxReactionTime is not >= 0.4F, then it has to be < 0.4F, so you can turn the else if into an else. You can then refactor it to one line:

pointOneChange = maxReactionTime >= 0.4F;


The following:

if (maxReactionTime == 1F) {
    reactionCap = true;
} else {
    reactionCap = false;
}


Can also be reduced to one line:

reactionCap = maxReactionTime == 1F;


In your Player class, the following:

if (isRight == true) {
    isRight = false;
} else if (isRight == false) {
    isRight = true;
}


can be reduced to:

isRight = !isRight;


You probably don't want to call GetComponent() in Update(). Instead you can declare a class level variable and assign to it using GetComponent() in Start().

It's your choice if you want to refactor the code into more methods. The main reason I declare more methods is if I can use a method multiple times, reducing duplication. It can also help with readability.

Code Snippets

if (reactionTimeArray [0] == 0) {
    reactionTimeArray [0] = reactionTime;
} else if (reactionTimeArray [1] == 0) {
    reactionTimeArray [1] = reactionTime;
// etc up to 9
for (int i = 0; i < 10; i++)
{
    if(reactionTimeArray[i] == 0)
    {
        reactionTimeArray[i] = reactionTime;
        break;
    }
}
averageReaction = reactionTimeArray.Sum() / 10;
float bestReaction = reactionTimeArray.Min();
if (maxReactionTime >= 0.4F) {
    pointOneChange = true 
} else if (maxReactionTime < 0.4F) {
    pointOneChange = false;
}
pointOneChange = maxReactionTime >= 0.4F;

Context

StackExchange Code Review Q#127942, answer score: 4

Revisions (0)

No revisions yet.