• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yashetv · Aug 16, 2020 at 08:08 PM · unity 2darchitecturedependencies

How to make good code architecture and dependencies

Hey, I'm making a tilebased game, and have a little problem with my code architecture. Everything works fine, but it's a mess in one point. So..


I have PlayerMovement and BoxMovement scripts which inherits from Move script. At this point everything seems to be ok. In Player Movement i call the "TryToMove" method from Move script.


Move script check if ahead of player is wall / box or air. If it's an air, obviously player can move If it's a wall, obciously player can't move but if its a box... i have to call method again for box. And check what is ahead of the box...

How I can do it without making a spaghetti code.. I know how stupid it looks, but i can't figure out better solution.

 CLASS MOVE
 
 public void TryToMove(Tilemap baseMap, Vector3Int tileAhead, Vector3Int playerDirection)
     {
         switch (TileCollision.CheckTile(baseMap, tileAhead))
         {
             case TileCollision.TileType.undefined:
                 Debug.LogError($"Not recognized object");
                 break;
             case TileCollision.TileType.empty:
                 MoveTo(tileAhead);
                 break;
             case TileCollision.TileType.box:
                 TileCollision.Move(baseMap, tileAhead, playerDirection);
                 MoveTo(tileAhead);
                 break;
             case TileCollision.TileType.wall:
                 return;
         }
     }



 public static class TileCollision 
 {
     public enum TileType
     {
         undefined = 0,
         empty = 1,
         box = 2,
         wall = 3
     }
 
     static TileBase tile;
 
     public static TileType CheckTile(Tilemap baseMap, Vector3Int tileToCheck)
     {
         TileBase tile = baseMap.GetTile(tileToCheck);
 
         if (tile == null)
         {
             return TileType.empty;
         }
         else if (tile.name == "BoxTile")
         {
             return TileType.box;
         }
         else if (tile.name == "Wall")
         {
             return TileType.wall;
         }
         else
         {
             return TileType.undefined;
         }
     }
 
     public static void Move(Tilemap baseMap, Vector3Int tileAhead, Vector3Int playerDirection)
     {
         Vector3Int tileAheadBox = tileAhead + playerDirection;
 
         GameObject boxTile = baseMap.GetInstantiatedObject(tileAhead);
         BoxMovement boxMovement = boxTile.GetComponent<BoxMovement>();
 
         boxMovement.tileAheadPlayer = tileAhead;
         boxMovement.tileAheadBox = tileAheadBox;       
 
         boxMovement.TryToMove(baseMap, tileAheadBox, playerDirection);
     }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to implement 2D Ai Wandering with 8 - directional movement using Perspective Images in unity 4.3 ??? 1 Answer

Reflective laser 2D, unity. 1 Answer

How to change the orthographic size of the camera through commands 0 Answers

Change Integer to All Gameobjects With Script 1 Answer

I have two collider in one game object how to distinguish between them when colliding with another? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges