Robo Defense | TweetTweetJam 8
Transfer power from the charging station to your battery and collect as much power as you can. But be careful, little plugs are out to steal your charge!
This was created for the TweetTweetJam 8 Game Jam. The challenge is to use 500 or less characters in your game code.
This was a very fun and difficult challenge to compress everything down to only 500 characters, especially if made with Unity. There are some built in functions and overrides that are very helpful when it comes to Unity but a lot of them have long names like void OnTriggerEnter2D(Collider2D c) that take up a lot of characters and were basically unavoidable.
In the end I managed to squeeze in most of what I originally sought out to make, but a few features are missing that I just couldn't fit in the code at all. I only had 2 characters left by the end so it was very close. I had to use some outside the box thinking to get some of the features working. Here is the full (condensed) code of the only script I used in the game and, if you're curious, there is an explanation on the techniques after the code:
using UnityEngine; class A:MonoBehaviour { public Transform[] s; public UnityEngine.UI.Text a,b; float c,p,k=5,x=.1f; int i,j; void FixedUpdate() { s[0].position+=new Vector3(0,Input.GetAxis("V"))*x; a.text=c.ToString("0.0"); if((k-=x)<0){k=6;i++;s[i%9+9].position=s[i%9+1].position;} for(j=9;j<18;j++)s[j].position+=(s[0].position-s[j].position).normalized*x/4; } void OnTriggerStay2D(Collider2D o) { if(o.tag=="e"&&--c>0)o.transform.position*=99; if(o.tag=="b"&&c-x>=0){p+=x;b.text=p.ToString("0");c-=x;} if(o.tag=="c")c+=x; } }
And this is the more readable version with the spaces back and the variables are also renamed for clarity:
using UnityEngine; class A : MonoBehaviour { public Transform[] transforms; public UnityEngine.UI.Text chargeText, totalPowerText; float charge, totalPower, spawnCooldown = 5, multiplier = .1f; int i, j; // void FixedUpdate() { allTransforms[0].position += new Vector3(0, Input.GetAxis("V")) * multiplier; chargeText.text = charge.ToString("0"); if ((spawnCooldown -= multiplier) < 0) { spawnCooldown = 6; i++; transforms[i % 9 + 9].position = transforms[i % 9 + 1].position; } for (j = 9; j < 18; j++) transforms[j].position += (transforms[0].position-transforms[j].position).normalized * multiplier/4; } // void OnTriggerStay2D(Collider2D o) { if (o.tag == "e" && --charge > 0) o.transform.position *= 99; if (o.tag == "p" && charge - multiplier >= 0) { totalPower += multiplier; totalPowerText.text = totalPower.ToString("0"); charge -= multiplier; } if (o.tag == "c") charge += multiplier; } }
A big difficulty I had was trying to do player movement (without the built-in input system which would avoid needing to use code) and make the enemies follow the player at the same time. The original way I did it used up all the characters so I couldn't even add in the power part or check for collisions at all. I ended up with a clever solution to save a ton of characters, using an array of Transforms. Whenever I wanted to get or edit the position of either the player or an enemy I would need something like 'player.transform.position' or 'enemy.transform.position' and that just used too many characters. So what I did instead is have an array of all the transforms in the game, 0 is the player, 1-8 are the enemy spawnpoints, and 9-17 are the enemies. This way I was able to assign enemies to spawnpoints when they are supposed to spawn (using object pooling instead of creating/destroying) and moving them far off screen when they die. This made it so I didn't have to have a separate script for controlling enemies nor did I have to search the scene to find all active enemies and update their positions.
The other challenge I faced was the power problem. I wanted to make it so that I could store power in the player when they collide with a charging station, and then transfer that to a battery when colliding with a battery. But making each of those collisions work required a lot of characters for things like updating text, subtracting and adding power, making sure the player is colliding with the correct object, and so on. So what I ended up doing was making as condensed if statements as I could and storing a set multiplier to transfer between player/charging station and player/battery. Then I ended up using one character tags to identify the specific objects, 'e' for enemy, 'b' for battery, and 'c' for charging station.
All in all I had a ton of fun working on this project and although there isn't an actual way to win or lose, the art is just super basic shapes and text, and the control is very limited, its still kinda fun to play and I think it turned out all right for how restricted I was.
If you made it this far, thanks for reading and hope you enjoy my little game!
Comments
Log in with itch.io to leave a comment.
Cool submission! It's been fun seeing some Unity entries this year, and it's pretty amazing that you're able to make something in Unity with so few characters. I also made a power management game back in the day, so I know that balancing is a real challenge in this genre!