C# and Cellular Automata

Spent some time this week learning Cellular Automata the first part of Unity live training course on procedural generation.

 

 

using UnityEngine;
using System.Collections;
using System;

public class MapGenerator : MonoBehaviour {

    public int width;
    public int height;

    public string seed;
    public bool useRandomSeed;

    [Range(0,100)]
    public int randomFillPercent;

    int[,] map;

    void Start() {
        GenerateMap();
    }

    void Update() {
        if (Input.GetMouseButtonDown(0)) {
            GenerateMap();
        }
    }

    void GenerateMap() {
        map = new int[width,height];
        RandomFillMap();

        for (int i = 0; i < 5; i ++) {
            SmoothMap();
        }
    }


    void RandomFillMap() {
        if (useRandomSeed) {
            seed = Time.time.ToString();
        }

        System.Random pseudoRandom = new System.Random(seed.GetHashCode());

        for (int x = 0; x < width; x ++) {
            for (int y = 0; y < height; y ++) {
                if (x == 0 || x == width-1 || y == 0 || y == height -1) {
                    map[x,y] = 1;
                }
                else {
                    map[x,y] = (pseudoRandom.Next(0,100) < randomFillPercent)? 1: 0;
                }
            }
        }
    }

    void SmoothMap() {
        for (int x = 0; x < width; x ++) {
            for (int y = 0; y < height; y ++) { int neighbourWallTiles = GetSurroundingWallCount(x,y); if (neighbourWallTiles > 4)
                    map[x,y] = 1;
                else if (neighbourWallTiles < 4)
                    map[x,y] = 0;

            }
        }
    }

    int GetSurroundingWallCount(int gridX, int gridY) {
        int wallCount = 0;
        for (int neighbourX = gridX - 1; neighbourX <= gridX + 1; neighbourX ++) {
            for (int neighbourY = gridY - 1; neighbourY <= gridY + 1; neighbourY ++) { if (neighbourX >= 0 && neighbourX < width && neighbourY >= 0 && neighbourY < height) {
                    if (neighbourX != gridX || neighbourY != gridY) {
                        wallCount += map[neighbourX,neighbourY];
                    }
                }
                else {
                    wallCount ++;
                }
            }
        }

        return wallCount;
    }


    void OnDrawGizmos() {
        if (map != null) {
            for (int x = 0; x < width; x ++) {
                for (int y = 0; y < height; y ++) {
                    Gizmos.color = (map[x,y] == 1)?Color.black:Color.white;
                    Vector3 pos = new Vector3(-width/2 + x + .5f,0, -height/2 + y+.5f);
                    Gizmos.DrawCube(pos,Vector3.one);
                }
            }
        }
    }

}

Learning C# in Unity3d and a bit of java (wink-wink)

Before I dive into the real meat of the blog this week. A quick side shuffle if you don’t mind. I’ve spent  the past few months learning Javascript, mostly because I wanted to learn node.js and get a clearer understanding of the MEAN Stack.687474703a2f2f7777772e646d7574682e6f72672f66696c65732f6e6f64656a732d6461726b2e706e67

MEAN is short for: MongoDB, Express.js, AngularJS, and Node.js. A set of instructions, frameworks, database’, and libraries for running server side javascript apps. If you’ve ever visited a single page website before.. something that behaves more like a app than a dusty old html site, then you’ve likely been browsing a site with Node.js or similar framework, under the hood. According to google i/o this year there’s going to be an additional billion worldwide smartphone users going online over the next 15 years. And they’ve realized that the limits of pushing things even as small as a single Android app is going to crush their business model (when you look at things at their scale). So what’s the answer? Modular web apps. For instance: you’re booking a flight, you want to do it while you’re with a group of friends because, you’re all going together. You pull open your phone, download some travel app, enter personal info, add credit card data, create searches pull database requests again and again. Well the Modular method of the future won’t be so data heavy. Instead of building apps that have to move large chunks of data back and forth, they and most of the development world want to take the APP off the phones and instead deliver Smart Server Packages ie; only the modules and components you need at the moment. You don’t need to rent a car, fine it won’t load ANY car data. All you need is flight records of such and such place on such and such day/time. The Server pulls and compiles at server run-time only the packages for your request and delivers that data on your phone as if it were being done locally.

Woah okay, big distraction. But, its just a cool concept and I can’t wait to see the kinds of shit we’re gonna be able to do when APPs and databases are coded by intelligent machines that have been scripted by Node.js and DOM hackers.  Strange things are afoot at the Circle K.

UNITY3d and C#: This week it’s all about data serialization and instantiated objects. The book I’m currently plowing through is titled:

download (1)

Unity in Action! It’s a pretty handy book as far as unity scripting books go. This one isn’t for the starter looking to learn from scratch. It requires you to have a bit of pre-existing experience writing C# and managing gameObjects. The scripting dialog in the book itself asks the reader to inject alot of reference code between inter connected objects and scripts with out too much hand holding often simply referring only to the methods that have to be added at the time they are needed. Not much of the example code can simply be copy and pasted. Thank fully this book is written with Unity 5x in mind so the GameObject<ObjectZ> references and such are up to date.

New to me is: Serialization or [SerializeField]. Its use calls for the compiler to convert an object or bit of code into a block of raw data. In this form it’s locked it from the editor so can then be used in a myriad of ways. Then when its done it can either be referenced to null (destroyed) or converted back to editable c# code. This isn’t anything new but I did learn that the editor inside unity serializes the data in side itself when it’s displayed to you. Which I suspect is the culprit behind the editor issue of altering script values in the inspector locks the value to whatever has been serialized by the inspector, and if you alter the scripts declared float for instance, the inspector permanently ignores the value in favor of the GUI one. Just a thought.

Where would you use this? Imagine you have a gun shooting bullets, each bullet you shoot has its own properties like ray projection, physical properties like speed and scale. We want the object to be instantiated and go about its business un-editable by the game controller SO we can create another one. Or 500 of them and and not have to scan each one each frame and bring the system to a snails pace.

All interesting shit right? more next week.

GlobalGameJam 2016

With only three days (BABYSTEPS) I was able to do a lot from scratch. This prototype is essentially a TowerDefence economy game in a cave but now, you set fake bounties to lure in hapless adventurers and slaughter them with traps placed in a Construction mode then set off in game by tapping them at precise times to kill heroes.

The story behind it was a D&D game some friends and I played many years ago; at the end of one game a Kobold archer very near death, escaped our clutches and was inevitably going to inform his army of our location(or so we convinced ourselves). After the game our DM informed us that he was just a helpless conscript who ran for his life with 1hp(maybe 1/2hp) to spare. We felt terrible about our plans to hunt down and slaughter that poor kid.  Later I gave that kid a name ‘Gary the Kobold’. And this game is his story of revenge: Having narrowly survived the clutches of a band of blood lusting adventurers Gary goes full Rolling Thunder, he replaces is missing hand with a hook, covers his missing eye with a black leather eye patch. Buys a book on Advanced trap design and then posts a fake “Adventurers Wanted” poster on a wall outside the local tavern.

screenshot1You can plainly see I wasn’t able to get all of the assets made in time but, I got the bounty system working and I was able to program:

 

  • Players have Gold inventory, bounty can be raised or lowered and get removed from total Gold.
  • Remaining gold Value carries over into next scene.
  • Construction mode where you spend remaining Gold to place traps.
  • Hero AI to move around the room semi-randomly until they find the Treasure Chest.
  • Hero’s collide with the Treasure and ‘steal’ from you until its empty aka Players Remaining GOLD, then escape.
  • Hero escaping pass near to traps, the Player TAPS activating a trap and the Hero dies.

screenshot3 screenshot2