Tower Defence - Day 2

Published May 06, 2018
Advertisement

Well suffice to say that finally getting some sunshine and nice weather here in the UK is anathema to making any kind of progress in game development, as I spent the day motobiking in Wales. In fact, my mum suggests that part of the reason the UK does well in intellectual pursuits is because we have such godawful weather, and you have to find something to do while you are trapped indoors for 360 days of the year.

But today I managed a second day of wrestling with Unity and Monodevelop. I'm developing a passionate hatred of both, particularly Monodevelop as the defaults seem to try to screw with every bit of text formatting, and there seems to no obvious way of turning all the auto stuff OFF. But it does seem to give some intellisense, something that isn't properly working in Geany. And given that debugging these C# scripts has so far been a nightmare, I'll be thankful for having Intellisense.

pointing1.jpg.39f325869cf8abf7f84c763a06371f71.jpg

Apparently you can do stepping through scripts somehow, but I haven't worked it out yet, and am having to rely on Debug logs (which seem overly verbose), and running the thing, changing a line, running again etc. It's like being back in the 80s.

Anyway despite this I'm gradually making progress. I still haven't a clue how C# really works, how it handles references to objects etc, so it's a miracle anything is working really.

I made a very dodgy placeholder gun turret in blender and got it imported, and put in a framework for the towers, an array to hold them, and fixed update and frame updates to do their logic and interpolation for rendering.

Took far too long debugging to get Atan2 working for orientations to point the guns at the enemies, but finally it seems to be working. The guns smoothly move to point at their target enemy.

pointing2.thumb.jpg.288c9d4a8b02eed28773a67c322f82a6.jpg

For the enemy I've just imported one of my native models, he has a run animation but I haven't got it playing yet, I'll worry about that later.

The next stage I guess it to start making some paths and get the enemies to move along it towards the player 'base', then get the towers firing some projectiles. I could use spears, or monkeys really.

Previous Entry Tower Defence Challenge
Next Entry Tower Defence Day 3
2 likes 2 comments

Comments

lawnjelly

Got the enemies following waypoints now and the towers tracking them. That's enough for tonight! :)

May 06, 2018 07:52 PM
Rutin

Very cool! :) I don't use C# much anymore as I program in C++ 99% of the time, but when you're comparing C++ and C# there will be differences.

For example, if I make a class called enemyOrc: 


orcEnemy myOrc1 = new orcEnemy();

This class is a reference type, so that means if I pass it directly through a function and make changes, it will retain it.

The two confusions that happen is people miss-understand what are reference types, and value types when passing.

I made an example of a class (reference type) and a int (value type) for you.

Also keep in mind, if I'm taking a int health but passing myOrc2.health this is a value type and will not retain the change. I have three examples here:

Passing by reference type

Passing by value type

Passing by value type (from a class variable "myOrc2.health")


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class orcEnemy
    {
        public int health;

        public orcEnemy()
        {
            health = 100;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            orcEnemy myOrc1 = new orcEnemy();
            attackOrc(myOrc1, 25);
            Console.WriteLine("(Outside of function) Orc has " + myOrc1.health + " health.");

            //////////////////

            Console.WriteLine("======================");

            //////////////////

            int myOrc1Health = 100;
            attackOrc(myOrc1Health, 25);
            Console.WriteLine("(Outside of function) Orc has " + myOrc1Health + " health.");

            //////////////////

            Console.WriteLine("======================");

            //////////////////

            orcEnemy myOrc2 = new orcEnemy();
            attackOrc(myOrc2.health, 25);
            Console.WriteLine("(Outside of function) Orc has " + myOrc2.health + " health.");


            //////////////////

            Console.Read();
        }

        static void attackOrc(orcEnemy enemy, int damage)
        {
            Console.WriteLine("Orc has " + enemy.health + " health.");

            enemy.health -= damage;

            Console.WriteLine("Orc took " + damage + " damage! Orc has " + enemy.health + " health left!");
            
        }

        static void attackOrc(int health, int damage)
        {
            Console.WriteLine("Orc has " + health + " health.");

            health -= damage;

            Console.WriteLine("Orc took " + damage + " damage! Orc has " + health + " health left!");

        }
    }
}

 

Console Output:


Orc has 100 health.
Orc took 25 damage! Orc has 75 health left!
(Outside of function) Orc has 75 health.
======================
Orc has 100 health.
Orc took 25 damage! Orc has 75 health left!
(Outside of function) Orc has 100 health.
======================
Orc has 100 health.
Orc took 25 damage! Orc has 75 health left!
(Outside of function) Orc has 100 health.

I hope this helps you a bit, and I hope my example is clear considering I don't use C# very often. :D 

May 06, 2018 08:47 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement