Skip to content

CR - #1

Open
dekel31 wants to merge 2 commits into
code-reviewfrom
master
Open

CR#1
dekel31 wants to merge 2 commits into
code-reviewfrom
master

Conversation

@dekel31

@dekel31 dekel31 commented Sep 11, 2021

Copy link
Copy Markdown
Owner

No description provided.

@@ -0,0 +1,11 @@
using UnityEngine;

[CreateAssetMenu(menuName = "Gameplay/Road Params", fileName = "Road Params")]

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if it's a good idea to call file names with spaces


public class GameplayObjectPool : IObjectPool
{
#region Constants

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove regions that are empty, no code, no need for a region and empty lines

using System.Collections.Generic;
using UnityEngine;

public class GameplayObjectPool : IObjectPool

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class seems to have a "manager" quality - it looks like it's generic, but in fact it deals with very specific objects, each in it's own individual name (look at the 2nd comment here as a good explanation why it's bad: https://softwareengineering.stackexchange.com/questions/129537/can-manager-classes-be-a-sign-of-bad-architecture)
If it creates objects, it should be a factory. if it's a factory, it should create only one type of object.

using System.Collections.Generic;
using UnityEngine;

public abstract class ObstacleBaseFactory

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this class seems redundant. you inherit it once, and you don't use the abstraction. you should delete it

using System.Collections.Generic;
using UnityEngine;

public abstract class RoadBaseFactory

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this class seems redundant. you inherit it once, and you don't use the abstraction. you should delete it

Obstacle
}

public interface IPooledObject

@dekel31 dekel31 Sep 11, 2021

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PooledObject means an object that an get the gameObject, in the end. it is too generic, any unity object can have a gameObject

}
else
{
var sinmpleRoad = (SimpleRoad)_simpleRoadFactory.Create();

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

using System.Collections.Generic;
using UnityEngine;

public enum PooledObjectType

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this type describes every object in the game, too generic. follow the main comment at the end of this to remove it completely too

public override IObstacle Create()
{
var asteroid = (GameObject)Object.Instantiate(_obstacleParams.AsteroidPrefab);
return asteroid.GetComponent<AsteroidObstacle>();

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetComponent doesn't necessarily get the object that you just created, see https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html (it might get you a different one that you created earlier)


#region Methods

public void AddObjectToPool(IPooledObject obj)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is actually removing an object from the game, right?

Comment on lines +51 to +56
var newObs = RandomizeNewObstacle();
if (newObs != null)
{
var pos = RandomPositionOnRoad();
GameplayElements.Instance.AddObstacleToRoad(newObs, _lastRoadIndex, pos);
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a duplicate of lines 32-37. simply, you can send the parameters of AddObstacleToRoad to the RandomizeNewObstacle method, if it does create an obstacle it will use them, won't otherwise


public static void Initialize()
{
_awaitService = CreateAwaitService();

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like a lot of these can be NativeSingleton s

@dekel31

dekel31 commented Sep 11, 2021

Copy link
Copy Markdown
Owner Author

I haven't looked at everything, but this is what I found so far:

@dekel31

dekel31 commented Sep 11, 2021

Copy link
Copy Markdown
Owner Author

first I'll begin with the things I don't like, but are not wrong. I'll state my reason for my disliking them.
You have an interface for everything, even ones that you use only once. imho, the less code, the better. shouldn't make a mountain out of a molehill. Some programmers create an interface for each class, in this day and age, tests are way easier to write than before, DI happens automatically, therefore this practice is dated.
Region - god I hate region. It creates so many empty lines and redundant lines. While you have an enormous 1k line class, fine, but for 50 line? you added 8-12 for regions.
That's my dislikes. onto things I think you should change, in the next comment

@dekel31

dekel31 commented Sep 11, 2021

Copy link
Copy Markdown
Owner Author

I think you don't need the IPooledObject interface. correct me if I'm wrong: for example, in AsteroidFactory, when instantiating the object, you use getComponent in order to get the "underlying" object of the GameObject that is the asteroid (BaseObstacle) and return it. but you never use the BaseObstacle itself (it's properties or whatnot), you just end up using GetGameObject (I'm referring to GameplayObjectPool: line 41 and using it in line 63. right after you get it in line 63 you immediately get the game object, so what is it for?). Happens in ObstacleController as well, you don't really use the type you created, so you can just refer to all game objects as themselves, not as the classes you created for them

@dekel31

dekel31 commented Sep 11, 2021

Copy link
Copy Markdown
Owner Author

Lastly, regarding GameplayObjectPool. this class is bound to get out of hand very fast with every new type of game component. As you can see, your switch case options are actually copies of one another:

case PooledObjectType.Road:
                _inActiveRoadsOP.Enqueue(obj.GetGameObject().GetComponent<BaseRoad>());
                break;

            case PooledObjectType.Obstacle:
                _inActiveObstaclesOP.Enqueue(obj.GetGameObject().GetComponent<BaseObstacle>());
                break;

instead, you can create a dictionary that it's key will be the object tag (see next comment) and the value will be the queue, something like this:

private tagsToGameObjects Dictionary<string, Queue<GameObject>> = new Dictionary<string, GameObject>();
tagsToGameObjects.Add("asteroid", new Queue<GameObject>());
tagsToGameObjects.Add("road", new Queue<GameObject>());
...
...
//instead of the switch case you do this:
public void AddObjectToPool(GameObject obj) {
tagsToGameObjects[gameObject.tag].Enqueue(obj));
...

@dekel31

dekel31 commented Sep 11, 2021

Copy link
Copy Markdown
Owner Author

I found something inside gameObject that can help differentiate between different types of them, instead of converting them into your own objects and then get the game object again. it's called tag https://docs.unity3d.com/ScriptReference/GameObject-tag.html

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

Successfully merging this pull request may close these issues.

2 participants