Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[FREE] Color Based Level Generator Release

Discussion in 'Made With Unity' started by mathiassoeholm, Dec 1, 2010.

  1. mathiassoeholm

    mathiassoeholm

    Joined:
    Jul 20, 2010
    Posts:
    104
    Here's the first version of a free level generator, which reads every pixel color from an image and generate Gameobjects based on each color.
    This could be used for a lot of games such as:

    - Pacman and other maze games
    - Breakout
    - Top down shooters
    - Puzzle games
    ...

    You make an image in Photoshop or anything similar, it have to be 8x8, 16x16, 32x32 and so on ...
    You can assign each color to instantiate different gameobjects, as an example you could have colors representing enemies, the player and walls.
    Level design is suddenley a lot easier.

    Also thanks to melmonkey for making a JS version, and giving excellent feedback

    Note: When using it, make sure to use the same texture import settings as the example ones.

    Please feel free to come with suggestions for the next version.

    Download

    Here's a Pacman example:

    Image used:


    To generate:
     
    Last edited: Jun 9, 2012
    REV-FX likes this.
  2. melmonkey

    melmonkey

    Joined:
    Mar 31, 2009
    Posts:
    373
    Hey everyone!

    When I saw Mathias's first post on this, I was intrigued. I talked with him, gave feedback, and converted his C# work, to JS, for all the JS people out there (like me).

    While this is a very cool tool, in it's current state, we would love to expand on it, and make it an amazing tool. With that in mind, please take a look, and try it out.

    Post any and all feedback, as well as feature requests. We have a few in mind, but they are decidedly leaning to what I want to use the tool for.

    Let us know what you want to see, and how you want to use it.

    Thanks for trying it out.
     
  3. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,821
    Something fun I was considering was using a variant on this for some quick-n-dirty AI.

    For instance, it could be what's telling the AI where it should move, where an emergency hiding place is, a tactically important position, all while hiding it from the player, and using a deceptively simple script interface. Theoretically this isn't so dissimilar from Waypoint technology...but this "Tactic Mapped" AI is theoretically every bit as powerful.

    Hmmm. :D

    One small thing: It needs modifications to work in Unity 2.6. Everyone is now warned.
     
    Last edited: Dec 2, 2010
  4. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,821
    Well, I'm stuck on mine. Since this is, at heart, your tech, I'll share it.

    It's a two piece system so far - the "Map" program, which lives on your actual terrain, and the AI program which executes the AI. These were perhaps too simple tests; in hindsight, this might not be so great for pathfinding, but more of managing high-level objectives like defending a particular spot. Anyways, here they are:

    TacticMappedAI.js:
    Code (csharp):
    1. class NodeAction
    2. {
    3.   var actionString : String;
    4.   var nodeColor : Color;
    5. }
    6.  
    7. var speed : float;
    8. var aiActions : NodeAction[];
    9.  
    10. private var tacticMap : TacticMap;
    11. private var mapWidth : int;
    12. private var mapHeight : int;
    13. private var moveDirection : Vector3;
    14. private var mapLocation : Vector2;
    15. private var oldIndex : int;
    16. private var mapIndex : int;
    17. private var aiCommand = new Array();
    18.  
    19. function Start()
    20. {
    21.   // Start by finding a tactic map.  Wring some
    22.   // good info out of it.
    23.   tacticMap = FindObjectOfType( TacticMap );
    24.   mapWidth = tacticMap.map.width;
    25.   mapHeight = tacticMap.map.height;
    26.  
    27.   // "Unfurl" the tactical map by converting the map's
    28.   // pixel colors into an array of AI command strings.
    29.   var mapColors : Color[] = tacticMap.map.GetPixels( 0 );
    30.  
    31.   for( var point : Color in mapColors )
    32.   {
    33.     // Read the color, then match it to an index
    34.     // in the aiActions array.  If it matches,
    35.     // assign the command to the AI array.
    36.     for( var action : NodeAction in aiActions )
    37.     {
    38.       if( point == action.nodeColor )
    39.       {
    40.         aiCommand.Add( action.actionString );
    41.         break;
    42.       }
    43.     }
    44.   }
    45.  
    46.   // Pre-set the move direction, initialize the transition
    47.   // recognition.
    48.   moveDirection = Vector3.forward;
    49.   CalculatePosition();
    50.   mapIndex = ConvertPointToLinear( mapLocation, mapWidth );
    51.   oldIndex = mapIndex;
    52.  
    53.   // That's all for preparation.  See you in FixedUpdate!
    54. }
    55.  
    56. function FixedUpdate()
    57. {
    58.   CalculatePosition();
    59.   mapIndex = ConvertPointToLinear( mapLocation, mapWidth );
    60.  
    61.   if( mapIndex != oldIndex )
    62.   {
    63.     InterpretAction( aiCommand[mapIndex] );
    64.     oldIndex = mapIndex;
    65.   }
    66.  
    67.   transform.Translate( moveDirection * speed * Time.deltaTime );
    68. }
    69.  
    70. function InterpretAction( actionString : String ) : void
    71. // There's nothing for it.  This function has to be
    72. // hard-coded by the end-user or their programmer.
    73. //
    74. // In this example, InterpretAction() only handles
    75. // the alteration to moveDirection.  In your game,
    76. // you may want this to have links to firing weapons,
    77. // changing pursuit targets, or other actions.
    78. {
    79.   //Debug.Log( "Acting on instruction: " + actionString );
    80.   if( actionString == "Go Straight" )
    81.   {
    82.     transform.Rotate( 0,0,0 );
    83.   }
    84.   if( actionString == "Go Backward" )
    85.   {
    86.     transform.Rotate( 0,180,0 );
    87.   }
    88.   if( actionString == "Go Left" )
    89.   {
    90.     transform.Rotate( 0,90, 0 );
    91.   }
    92.   if( actionString == "Go Right" )
    93.   {
    94.     transform.Rotate( 0,-90, 0 );
    95.   }
    96. }
    97.  
    98. function CalculatePosition() : void
    99. // We want to find (0,0), so we take the local dimensions,
    100. // and divide by the tactical map dimensions to derive a
    101. // coordinate system.
    102. {
    103.   var mapDims : Vector3 = tacticMap.transform.localScale;
    104.   var mapCenter : Vector2 = tacticMap.CenterCoordinate();
    105.   var mapX : int;
    106.   var mapY : int;
    107.  
    108.   // Calculate the map position...
    109.   mapX = transform.position.x - tacticMap.transform.position.x;
    110.   mapX /= mapDims.x;
    111.   mapX /= tacticMap.trueDimensions.x;
    112.   mapX += mapCenter.x;
    113.  
    114.   mapY = transform.position.z - tacticMap.transform.position.z;
    115.   mapY /= mapDims.y;
    116.   mapX /= tacticMap.trueDimensions.y;
    117.   mapY += mapCenter.y;
    118.  
    119.   mapLocation = Vector2( mapX,mapY );
    120.  
    121.   Debug.Log( "I am at (" + mapLocation.x + ", " + mapLocation.y + ")" );
    122. }
    123.  
    124. function ConvertPointToLinear( coords : Vector2, width : int ) : int
    125. {
    126.   return coords.x + ( coords.y * width );
    127. }
    TacticMap.js:
    Code (csharp):
    1. var map : Texture2D;
    2. var trueDimensions : Vector2;
    3. var centerOrigin : boolean = true;
    4. var debugMode : boolean = false;
    5.  
    6. private var mat : Material;
    7. private var originCoord : Vector2;
    8.  
    9. function Start()
    10. {
    11.   // This just shows the tactical map on the mesh.
    12.   if( debugMode )
    13.   {
    14.     mat = renderer.material;
    15.     mat.mainTexture = map;
    16.   }
    17.  
    18.   // Derive the real origin offset.  We do this by
    19.   // looking at the width and height of the map
    20.   // image.
    21.   if( centerOrigin )
    22.   {
    23.     originCoord = Vector2( ( map.width / 2 ), ( map.height / 2 ) );
    24.   }
    25.   else
    26.   {
    27.     originCoord = Vector2.zero;
    28.   }
    29. }
    30.  
    31. function CenterCoordinate() : Vector2
    32. {
    33.   return originCoord;
    34. }
     
    REV-FX likes this.
  5. mathiassoeholm

    mathiassoeholm

    Joined:
    Jul 20, 2010
    Posts:
    104
    Hey that's awesome, very nice use of it there.
    I actually didn't consider to use this for an AI of any sort.

    Thanks for posting your code, I might try this out myself :)
     
  6. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,821
    Well, my code has one serious issue: I have a hard time pinpointing a player's exact position on a mesh. I'm sure my method in that code is not correct. If you can figure that part out, you have a Tactic Map AI solution, too.

    If you get that working, give it back to me, and I'll get started on an advanced version of this. Imagine multiple tactic maps running together, like one that tells AIs where they can move, another that gives higher-level directives (again, defend this, wait for allies, etc.), etc. Subsumptive Tactic Maps, as it were.
     
  7. CoatlGames

    CoatlGames

    Joined:
    Apr 25, 2008
    Posts:
    773
    absolutely brilliant
     
  8. LamentConfig

    LamentConfig

    Joined:
    Sep 28, 2010
    Posts:
    292
    This is a really really sweet idea :) Nice toy to play with until I can think of something useful to do with it :) Cheers!
     
  9. mathiassoeholm

    mathiassoeholm

    Joined:
    Jul 20, 2010
    Posts:
    104
    Thanks for the replies :)
     
  10. wimeck

    wimeck

    Joined:
    May 26, 2008
    Posts:
    50
    Hi Mathias,

    This looks really interesting, great work!!!
     
  11. fallingbrickwork

    fallingbrickwork

    Joined:
    Mar 16, 2009
    Posts:
    1,072
    Yes, this does look really interesting. Great work so far.

    Matt.
     
  12. brendonvdm

    brendonvdm

    Joined:
    Sep 28, 2010
    Posts:
    28
    hey i would like to use this maze generator hope you still active , your link is down
     
  13. mathiassoeholm

    mathiassoeholm

    Joined:
    Jul 20, 2010
    Posts:
    104
    I just updated the dead link upon request, and made some small improvements to the code :)
     
  14. Essal

    Essal

    Joined:
    Feb 16, 2010
    Posts:
    107
    Sorry for ressurecting an old thread like this - But I really wanted to thank you for releasing this! It's helped me tremendously in my pursuit of finding a way of creating my level from an image. :)
    Played around with it a little, added some variables and randomrolls, and actually achieved what i wanted to do :)
    Heres a little image from my playing around with it:)
    $Screenshot.jpg

    Everything is placed by color in a lvlimage:)
     
  15. mathiassoeholm

    mathiassoeholm

    Joined:
    Jul 20, 2010
    Posts:
    104
    Hey I didn't see your post before, but that is so cool.. I'm glad you found it useful :D
     
  16. chloroplastgames

    chloroplastgames

    Joined:
    Sep 10, 2014
    Posts:
    6
    This plugin is awesome. Anyone can reupload the plugin?
     
    Last edited: Dec 30, 2014