127
CLASSES CHAPTER 25 1

CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

CLASSES

CHAPTER 25

1

Page 2: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Topics

2

Page 3: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Topics§ Understanding Classes

2

Page 4: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Topics§ Understanding Classes

– The Anatomy of a Class

2

Page 5: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Topics§ Understanding Classes

– The Anatomy of a Class

§ Class Inheritance

2

Page 6: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Topics§ Understanding Classes

– The Anatomy of a Class

§ Class Inheritance– Superclasses and Subclasses

2

Page 7: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Topics§ Understanding Classes

– The Anatomy of a Class

§ Class Inheritance– Superclasses and Subclasses– Virtual and Override

2

Page 8: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes

3

Page 9: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Classes are the key concept in Object-Oriented

Programming

3

Page 10: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Classes are the key concept in Object-Oriented

Programming§ A class is a definition of a type of object

3

Page 11: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Classes are the key concept in Object-Oriented

Programming§ A class is a definition of a type of object§ There can be many instances of a single class

3

Page 12: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Classes are the key concept in Object-Oriented

Programming§ A class is a definition of a type of object§ There can be many instances of a single class

– Each person in this classroom could be thought of as an instance of the Human class

3

Page 13: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Classes are the key concept in Object-Oriented

Programming§ A class is a definition of a type of object§ There can be many instances of a single class

– Each person in this classroom could be thought of as an instance of the Human class

§ C# classes combine data and functionality

3

Page 14: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Classes are the key concept in Object-Oriented

Programming§ A class is a definition of a type of object§ There can be many instances of a single class

– Each person in this classroom could be thought of as an instance of the Human class

§ C# classes combine data and functionality– Classes have variables, which are called fields

3

Page 15: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Classes are the key concept in Object-Oriented

Programming§ A class is a definition of a type of object§ There can be many instances of a single class

– Each person in this classroom could be thought of as an instance of the Human class

§ C# classes combine data and functionality– Classes have variables, which are called fields– Classes have functions, which are called methods

3

Page 16: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Classes are the key concept in Object-Oriented

Programming§ A class is a definition of a type of object§ There can be many instances of a single class

– Each person in this classroom could be thought of as an instance of the Human class

§ C# classes combine data and functionality– Classes have variables, which are called fields– Classes have functions, which are called methods

§ You're already using classes!

3

Page 17: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Classes are the key concept in Object-Oriented

Programming§ A class is a definition of a type of object§ There can be many instances of a single class

– Each person in this classroom could be thought of as an instance of the Human class

§ C# classes combine data and functionality– Classes have variables, which are called fields– Classes have functions, which are called methods

§ You're already using classes!– Each C# script you've written is a class

3

Page 18: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Classes are the key concept in Object-Oriented

Programming§ A class is a definition of a type of object§ There can be many instances of a single class

– Each person in this classroom could be thought of as an instance of the Human class

§ C# classes combine data and functionality– Classes have variables, which are called fields– Classes have functions, which are called methods

§ You're already using classes!– Each C# script you've written is a class

§ Classes represent objects in your game

3

Page 19: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes

4

Page 20: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Example: A character in a standard RPG

4

Page 21: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Example: A character in a standard RPG

– Fields you would want for each character

4

Page 22: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Example: A character in a standard RPG

– Fields you would want for each character! string! ! name;!! ! // The character's name

4

Page 23: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Example: A character in a standard RPG

– Fields you would want for each character! string! ! name;!! ! // The character's name ! float!! ! health;! ! // The amount of health she has

4

Page 24: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Example: A character in a standard RPG

– Fields you would want for each character! string! ! name;!! ! // The character's name ! float!! ! health;! ! // The amount of health she has ! float!! ! healthMax;! // Her maximum amount of health

4

Page 25: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Example: A character in a standard RPG

– Fields you would want for each character! string! ! name;!! ! // The character's name ! float!! ! health;! ! // The amount of health she has ! float!! ! healthMax;! // Her maximum amount of health ! List<Item>! inventory;! // List of Items in her inventory

4

Page 26: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Example: A character in a standard RPG

– Fields you would want for each character! string! ! name;!! ! // The character's name ! float!! ! health;! ! // The amount of health she has ! float!! ! healthMax;! // Her maximum amount of health ! List<Item>! inventory;! // List of Items in her inventory ! List<Item>! equipped;! // A List of Items she has equipped

4

Page 27: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Example: A character in a standard RPG

– Fields you would want for each character! string! ! name;!! ! // The character's name ! float!! ! health;! ! // The amount of health she has ! float!! ! healthMax;! // Her maximum amount of health ! List<Item>! inventory;! // List of Items in her inventory ! List<Item>! equipped;! // A List of Items she has equipped

– Methods you would want

4

Page 28: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Example: A character in a standard RPG

– Fields you would want for each character! string! ! name;!! ! // The character's name ! float!! ! health;! ! // The amount of health she has ! float!! ! healthMax;! // Her maximum amount of health ! List<Item>! inventory;! // List of Items in her inventory ! List<Item>! equipped;! // A List of Items she has equipped

– Methods you would want! void Move(Vector3 newLoc) {…}!! ! // Moves her to newLoc

4

Page 29: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Example: A character in a standard RPG

– Fields you would want for each character! string! ! name;!! ! // The character's name ! float!! ! health;! ! // The amount of health she has ! float!! ! healthMax;! // Her maximum amount of health ! List<Item>! inventory;! // List of Items in her inventory ! List<Item>! equipped;! // A List of Items she has equipped

– Methods you would want! void Move(Vector3 newLoc) {…}!! ! // Moves her to newLoc ! void Attack(Character target) {…}! // Attacks target with the ! ! ! ! ! ! ! ! ! ! ! ! ! current weapon or spell

4

Page 30: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Example: A character in a standard RPG

– Fields you would want for each character! string! ! name;!! ! // The character's name ! float!! ! health;! ! // The amount of health she has ! float!! ! healthMax;! // Her maximum amount of health ! List<Item>! inventory;! // List of Items in her inventory ! List<Item>! equipped;! // A List of Items she has equipped

– Methods you would want! void Move(Vector3 newLoc) {…}!! ! // Moves her to newLoc ! void Attack(Character target) {…}! // Attacks target with the ! ! ! ! ! ! ! ! ! ! ! ! ! current weapon or spell! void TakeDamage(float dmgAmt) {…}! // Reduces health

4

Page 31: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Understanding Classes§ Example: A character in a standard RPG

– Fields you would want for each character! string! ! name;!! ! // The character's name ! float!! ! health;! ! // The amount of health she has ! float!! ! healthMax;! // Her maximum amount of health ! List<Item>! inventory;! // List of Items in her inventory ! List<Item>! equipped;! // A List of Items she has equipped

– Methods you would want! void Move(Vector3 newLoc) {…}!! ! // Moves her to newLoc ! void Attack(Character target) {…}! // Attacks target with the ! ! ! ! ! ! ! ! ! ! ! ! ! current weapon or spell! void TakeDamage(float dmgAmt) {…}! // Reduces health ! void Equip(Item newItem) {…}! ! ! // Adds an Item to the ! ! ! ! ! ! ! ! ! ! ! ! ! equipped List

4

Page 32: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class

5

Page 33: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class

5

Includes

Page 34: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class

5

IncludesThe Class Declaration

Page 35: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class

5

IncludesThe Class DeclarationFields

Page 36: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class

5

IncludesThe Class DeclarationFields

Methods

Page 37: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class

5

IncludesThe Class DeclarationFields

Methods

Properties

Page 38: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class

5

IncludesThe Class DeclarationFields

Methods

Properties

p.s. Line numbers are handled automatically by MonoDevelop

Page 39: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class

6

Page 40: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ We'll explore each part of a class named Enemy

6

Page 41: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ We'll explore each part of a class named Enemy

– The Enemy class is for a simple top-down space shooter game

6

Page 42: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ We'll explore each part of a class named Enemy

– The Enemy class is for a simple top-down space shooter game– An Enemy instance moves down the screen at a speed of 10

6

Page 43: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ We'll explore each part of a class named Enemy

– The Enemy class is for a simple top-down space shooter game– An Enemy instance moves down the screen at a speed of 10

§ Includes

6

Page 44: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ We'll explore each part of a class named Enemy

– The Enemy class is for a simple top-down space shooter game– An Enemy instance moves down the screen at a speed of 10

§ Includes– Include code libraries in your project

6

Page 45: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ We'll explore each part of a class named Enemy

– The Enemy class is for a simple top-down space shooter game– An Enemy instance moves down the screen at a speed of 10

§ Includes– Include code libraries in your project– Enables standard Unity libraries and objects

6

Page 46: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ We'll explore each part of a class named Enemy

– The Enemy class is for a simple top-down space shooter game– An Enemy instance moves down the screen at a speed of 10

§ Includes– Include code libraries in your project– Enables standard Unity libraries and objects

• e.g., GameObject, MonoBehaviour, Transform, Renderer, etc.

6

Page 47: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ We'll explore each part of a class named Enemy

– The Enemy class is for a simple top-down space shooter game– An Enemy instance moves down the screen at a speed of 10

§ Includes– Include code libraries in your project– Enables standard Unity libraries and objects

• e.g., GameObject, MonoBehaviour, Transform, Renderer, etc. 1 using UnityEngine;!! ! ! ! ! ! // Required for Unity 2 using System.Collections;! ! ! ! // Included by Unity's default 3 using System.Collections.Generic;!! // Required to use a List

6

Page 48: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ We'll explore each part of a class named Enemy

– The Enemy class is for a simple top-down space shooter game– An Enemy instance moves down the screen at a speed of 10

§ Includes– Include code libraries in your project– Enables standard Unity libraries and objects

• e.g., GameObject, MonoBehaviour, Transform, Renderer, etc. 1 using UnityEngine;!! ! ! ! ! ! // Required for Unity 2 using System.Collections;! ! ! ! // Included by Unity's default 3 using System.Collections.Generic;!! // Required to use a List

§ The Class Declaration

6

Page 49: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ We'll explore each part of a class named Enemy

– The Enemy class is for a simple top-down space shooter game– An Enemy instance moves down the screen at a speed of 10

§ Includes– Include code libraries in your project– Enables standard Unity libraries and objects

• e.g., GameObject, MonoBehaviour, Transform, Renderer, etc. 1 using UnityEngine;!! ! ! ! ! ! // Required for Unity 2 using System.Collections;! ! ! ! // Included by Unity's default 3 using System.Collections.Generic;!! // Required to use a List

§ The Class Declaration– Declares the name of the class and its superclass

6

Page 50: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ We'll explore each part of a class named Enemy

– The Enemy class is for a simple top-down space shooter game– An Enemy instance moves down the screen at a speed of 10

§ Includes– Include code libraries in your project– Enables standard Unity libraries and objects

• e.g., GameObject, MonoBehaviour, Transform, Renderer, etc. 1 using UnityEngine;!! ! ! ! ! ! // Required for Unity 2 using System.Collections;! ! ! ! // Included by Unity's default 3 using System.Collections.Generic;!! // Required to use a List

§ The Class Declaration– Declares the name of the class and its superclass– Enemy is a class that extends its superclass MonoBehaviour

6

Page 51: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ We'll explore each part of a class named Enemy

– The Enemy class is for a simple top-down space shooter game– An Enemy instance moves down the screen at a speed of 10

§ Includes– Include code libraries in your project– Enables standard Unity libraries and objects

• e.g., GameObject, MonoBehaviour, Transform, Renderer, etc. 1 using UnityEngine;!! ! ! ! ! ! // Required for Unity 2 using System.Collections;! ! ! ! // Included by Unity's default 3 using System.Collections.Generic;!! // Required to use a List

§ The Class Declaration– Declares the name of the class and its superclass– Enemy is a class that extends its superclass MonoBehaviour

5 public class Enemy : MonoBehaviour {

6

Page 52: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class

7

Page 53: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Fields

7

Page 54: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Fields

– Fields are variables that are part of the class

7

Page 55: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Fields

– Fields are variables that are part of the class– Fields marked public are able to be seen by other classes and

by other instances of this class

7

Page 56: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Fields

– Fields are variables that are part of the class– Fields marked public are able to be seen by other classes and

by other instances of this class– Fields marked private are only able to be seen by this one

instance of a class

7

Page 57: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Fields

– Fields are variables that are part of the class– Fields marked public are able to be seen by other classes and

by other instances of this class– Fields marked private are only able to be seen by this one

instance of a class• Private fields are secrets

7

Page 58: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Fields

– Fields are variables that are part of the class– Fields marked public are able to be seen by other classes and

by other instances of this class– Fields marked private are only able to be seen by this one

instance of a class• Private fields are secrets• They are also a safer way to program than always using public fields

7

Page 59: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Fields

– Fields are variables that are part of the class– Fields marked public are able to be seen by other classes and

by other instances of this class– Fields marked private are only able to be seen by this one

instance of a class• Private fields are secrets• They are also a safer way to program than always using public fields• Public fields are used throughout the book so that the field values

appear and are editable in the Unity Inspector

7

Page 60: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Fields

– Fields are variables that are part of the class– Fields marked public are able to be seen by other classes and

by other instances of this class– Fields marked private are only able to be seen by this one

instance of a class• Private fields are secrets• They are also a safer way to program than always using public fields• Public fields are used throughout the book so that the field values

appear and are editable in the Unity Inspector 7! ! public float! speed = 10f;! ! // The speed in m/s

7

Page 61: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Fields

– Fields are variables that are part of the class– Fields marked public are able to be seen by other classes and

by other instances of this class– Fields marked private are only able to be seen by this one

instance of a class• Private fields are secrets• They are also a safer way to program than always using public fields• Public fields are used throughout the book so that the field values

appear and are editable in the Unity Inspector 7! ! public float! speed = 10f;! ! // The speed in m/s 8! ! public float! fireRate = 0.3f;! // Shots per second (Unused)

7

Page 62: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Fields

– Fields are variables that are part of the class– Fields marked public are able to be seen by other classes and

by other instances of this class– Fields marked private are only able to be seen by this one

instance of a class• Private fields are secrets• They are also a safer way to program than always using public fields• Public fields are used throughout the book so that the field values

appear and are editable in the Unity Inspector 7! ! public float! speed = 10f;! ! // The speed in m/s 8! ! public float! fireRate = 0.3f;! // Shots per second (Unused)

• Declares two public fields for all instances of the Enemy class

7

Page 63: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Fields

– Fields are variables that are part of the class– Fields marked public are able to be seen by other classes and

by other instances of this class– Fields marked private are only able to be seen by this one

instance of a class• Private fields are secrets• They are also a safer way to program than always using public fields• Public fields are used throughout the book so that the field values

appear and are editable in the Unity Inspector 7! ! public float! speed = 10f;! ! // The speed in m/s 8! ! public float! fireRate = 0.3f;! // Shots per second (Unused)

• Declares two public fields for all instances of the Enemy class• Each instance has its own value for speed and fireRate

7

Page 64: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class

8

Page 65: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Methods

8

Page 66: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Methods

– Functions that are part of the class

8

Page 67: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Methods

– Functions that are part of the class– Can also be marked public or private

8

Page 68: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Methods

– Functions that are part of the class– Can also be marked public or private

11! ! void Update() { 12! ! ! Move(); 13! ! }

8

Page 69: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Methods

– Functions that are part of the class– Can also be marked public or private

11! ! void Update() { 12! ! ! Move(); 13! ! }14 15! ! public virtual void Move() {! // Move down the screen at speed 16! ! ! Vector3 tempPos = pos; 17! ! ! tempPos.y -= speed * Time.deltaTime; // Makes it Time-Based! 18! ! ! pos = tempPos; 19! ! }

8

Page 70: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Methods

– Functions that are part of the class– Can also be marked public or private

11! ! void Update() { 12! ! ! Move(); 13! ! }14 15! ! public virtual void Move() {! // Move down the screen at speed 16! ! ! Vector3 tempPos = pos; 17! ! ! tempPos.y -= speed * Time.deltaTime; // Makes it Time-Based! 18! ! ! pos = tempPos; 19! ! }

– Note that Move is a virtual function

8

Page 71: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Methods

– Functions that are part of the class– Can also be marked public or private

11! ! void Update() { 12! ! ! Move(); 13! ! }14 15! ! public virtual void Move() {! // Move down the screen at speed 16! ! ! Vector3 tempPos = pos; 17! ! ! tempPos.y -= speed * Time.deltaTime; // Makes it Time-Based! 18! ! ! pos = tempPos; 19! ! }

– Note that Move is a virtual function• Virtual functions can be overridden by functions of the same name in a

subclass (we'll cover this shortly)

8

Page 72: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class

9

Page 73: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Properties

9

Page 74: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Properties

– Properties are methods masquerading as fields

9

Page 75: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Properties

– Properties are methods masquerading as fields– Properties can only exist within classes

9

Page 76: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Properties

– Properties are methods masquerading as fields– Properties can only exist within classes

35! ! public Vector3 pos {

9

Page 77: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Properties

– Properties are methods masquerading as fields– Properties can only exist within classes

35! ! public Vector3 pos { 36! ! ! get {

9

Page 78: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Properties

– Properties are methods masquerading as fields– Properties can only exist within classes

35! ! public Vector3 pos { 36! ! ! get { 37! ! ! ! return( this.transform.position );

9

Page 79: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Properties

– Properties are methods masquerading as fields– Properties can only exist within classes

35! ! public Vector3 pos { 36! ! ! get { 37! ! ! ! return( this.transform.position ); 38! ! ! }

9

Page 80: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Properties

– Properties are methods masquerading as fields– Properties can only exist within classes

35! ! public Vector3 pos { 36! ! ! get { 37! ! ! ! return( this.transform.position ); 38! ! ! } 39! ! ! set {

9

Page 81: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Properties

– Properties are methods masquerading as fields– Properties can only exist within classes

35! ! public Vector3 pos { 36! ! ! get { 37! ! ! ! return( this.transform.position ); 38! ! ! } 39! ! ! set { 40! ! ! ! this.transform.position = value;

9

Page 82: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Properties

– Properties are methods masquerading as fields– Properties can only exist within classes

35! ! public Vector3 pos { 36! ! ! get { 37! ! ! ! return( this.transform.position ); 38! ! ! } 39! ! ! set { 40! ! ! ! this.transform.position = value; 41! ! ! }

9

Page 83: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Properties

– Properties are methods masquerading as fields– Properties can only exist within classes

35! ! public Vector3 pos { 36! ! ! get { 37! ! ! ! return( this.transform.position ); 38! ! ! } 39! ! ! set { 40! ! ! ! this.transform.position = value; 41! ! ! } 42! ! }

9

Page 84: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

The Anatomy of a Class§ Properties

– Properties are methods masquerading as fields– Properties can only exist within classes

35! ! public Vector3 pos { 36! ! ! get { 37! ! ! ! return( this.transform.position ); 38! ! ! } 39! ! ! set { 40! ! ! ! this.transform.position = value; 41! ! ! } 42! ! }

– This property simplifies setting the transform.position of this Enemy

9

Page 85: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Instances as Components

10

Page 86: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Instances as Components§ In Unity, all class instances are treated as GameObject

Components

10

Page 87: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Instances as Components§ In Unity, all class instances are treated as GameObject

Components– The class instance can be accessed using GetComponent<>()

10

Page 88: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Instances as Components§ In Unity, all class instances are treated as GameObject

Components– The class instance can be accessed using GetComponent<>()

! ! Enemy thisEnemy = this.gameObject.GetComponent<Enemy>();

10

Page 89: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Instances as Components§ In Unity, all class instances are treated as GameObject

Components– The class instance can be accessed using GetComponent<>()

! ! Enemy thisEnemy = this.gameObject.GetComponent<Enemy>();

– From there, any public variable can be accessed

10

Page 90: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Instances as Components§ In Unity, all class instances are treated as GameObject

Components– The class instance can be accessed using GetComponent<>()

! ! Enemy thisEnemy = this.gameObject.GetComponent<Enemy>();

– From there, any public variable can be accessed! ! thisEnemy.speed = 20f;! // Increase speed of this Enemy to 20

10

Page 91: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Instances as Components§ In Unity, all class instances are treated as GameObject

Components– The class instance can be accessed using GetComponent<>()

! ! Enemy thisEnemy = this.gameObject.GetComponent<Enemy>();

– From there, any public variable can be accessed! ! thisEnemy.speed = 20f;! // Increase speed of this Enemy to 20

– Many C# scripts can be attached to a single GameObject

10

Page 92: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance

11

Page 93: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ Most classes inherit from another class

11

Page 94: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ Most classes inherit from another class 5 public class Enemy : MonoBehaviour {…}

11

Page 95: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ Most classes inherit from another class 5 public class Enemy : MonoBehaviour {…}

§ Enemy inherits from MonoBehaviour

11

Page 96: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ Most classes inherit from another class 5 public class Enemy : MonoBehaviour {…}

§ Enemy inherits from MonoBehaviour– Enemy is the subclass of MonoBehavior

11

Page 97: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ Most classes inherit from another class 5 public class Enemy : MonoBehaviour {…}

§ Enemy inherits from MonoBehaviour– Enemy is the subclass of MonoBehavior– MonoBehavior is called the superclass, base class, or parent

class of Enemy

11

Page 98: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ Most classes inherit from another class 5 public class Enemy : MonoBehaviour {…}

§ Enemy inherits from MonoBehaviour– Enemy is the subclass of MonoBehavior– MonoBehavior is called the superclass, base class, or parent

class of Enemy– This means that Enemy inherits all of MonoBehaviour's fields

and methods

11

Page 99: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ Most classes inherit from another class 5 public class Enemy : MonoBehaviour {…}

§ Enemy inherits from MonoBehaviour– Enemy is the subclass of MonoBehavior– MonoBehavior is called the superclass, base class, or parent

class of Enemy– This means that Enemy inherits all of MonoBehaviour's fields

and methods• Example inherited fields:

11

Page 100: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ Most classes inherit from another class 5 public class Enemy : MonoBehaviour {…}

§ Enemy inherits from MonoBehaviour– Enemy is the subclass of MonoBehavior– MonoBehavior is called the superclass, base class, or parent

class of Enemy– This means that Enemy inherits all of MonoBehaviour's fields

and methods• Example inherited fields:

– gameObject, transform, renderer, etc.

11

Page 101: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ Most classes inherit from another class 5 public class Enemy : MonoBehaviour {…}

§ Enemy inherits from MonoBehaviour– Enemy is the subclass of MonoBehavior– MonoBehavior is called the superclass, base class, or parent

class of Enemy– This means that Enemy inherits all of MonoBehaviour's fields

and methods• Example inherited fields:

– gameObject, transform, renderer, etc.• Example inherited methods:

11

Page 102: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ Most classes inherit from another class 5 public class Enemy : MonoBehaviour {…}

§ Enemy inherits from MonoBehaviour– Enemy is the subclass of MonoBehavior– MonoBehavior is called the superclass, base class, or parent

class of Enemy– This means that Enemy inherits all of MonoBehaviour's fields

and methods• Example inherited fields:

– gameObject, transform, renderer, etc.• Example inherited methods:

– GetComponent<>(), Invoke(), StartCoroutine(), etc.

11

Page 103: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ Most classes inherit from another class 5 public class Enemy : MonoBehaviour {…}

§ Enemy inherits from MonoBehaviour– Enemy is the subclass of MonoBehavior– MonoBehavior is called the superclass, base class, or parent

class of Enemy– This means that Enemy inherits all of MonoBehaviour's fields

and methods• Example inherited fields:

– gameObject, transform, renderer, etc.• Example inherited methods:

– GetComponent<>(), Invoke(), StartCoroutine(), etc.• Inheriting from MonoBehaviour is what makes Enemy able to act like a

GameObject component

11

Page 104: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance

12

Page 105: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ We can create a class that inherits from Enemy!

12

Page 106: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ We can create a class that inherits from Enemy! 1! using UnityEngine; 2! using System.Collections; 3 4! public class EnemyZig : Enemy { 5 !! // EnemyZig inherits ALL its behavior from Enemy 6! }

12

Page 107: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ We can create a class that inherits from Enemy! 1! using UnityEngine; 2! using System.Collections; 3 4! public class EnemyZig : Enemy { 5 !! // EnemyZig inherits ALL its behavior from Enemy 6! }

§ If this class is attached to a different GameObject, that GameObject will act exactly like an Enemy

12

Page 108: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ We can create a class that inherits from Enemy! 1! using UnityEngine; 2! using System.Collections; 3 4! public class EnemyZig : Enemy { 5 !! // EnemyZig inherits ALL its behavior from Enemy 6! }

§ If this class is attached to a different GameObject, that GameObject will act exactly like an Enemy

– It will also move down the screen at a rate of 10m/second

12

Page 109: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ We can create a class that inherits from Enemy! 1! using UnityEngine; 2! using System.Collections; 3 4! public class EnemyZig : Enemy { 5 !! // EnemyZig inherits ALL its behavior from Enemy 6! }

§ If this class is attached to a different GameObject, that GameObject will act exactly like an Enemy

– It will also move down the screen at a rate of 10m/second

§ Move() can be overridden because it is a virtual function

12

Page 110: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ We can create a class that inherits from Enemy! 1! using UnityEngine; 2! using System.Collections; 3 4! public class EnemyZig : Enemy { 5 !! // EnemyZig inherits ALL its behavior from Enemy 6! }

§ If this class is attached to a different GameObject, that GameObject will act exactly like an Enemy

– It will also move down the screen at a rate of 10m/second

§ Move() can be overridden because it is a virtual function – This means that EnemyZig can have its own version of Move()!

12

Page 111: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance

13

Page 112: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ EnemyZig.Move() overrides Enemy.Move()

13

Page 113: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ EnemyZig.Move() overrides Enemy.Move() 4! public class EnemyZig : Enemy { 5 !! public override void Move () { 6 !! ! Vector3 tempPos = pos; 7 !! ! tempPos.x = Mathf.Sin(Time.time * Mathf.PI*2) * 4; 8 !! ! pos = tempPos; // Uses the pos property of the superclass 9 !! ! base.Move(); // Calls Move() on the superclass 10 !! } 11! }

13

Page 114: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ EnemyZig.Move() overrides Enemy.Move() 4! public class EnemyZig : Enemy { 5 !! public override void Move () { 6 !! ! Vector3 tempPos = pos; 7 !! ! tempPos.x = Mathf.Sin(Time.time * Mathf.PI*2) * 4; 8 !! ! pos = tempPos; // Uses the pos property of the superclass 9 !! ! base.Move(); // Calls Move() on the superclass 10 !! } 11! }

– Now, when the Update() method in Enemy calls Move(), EnemyZig instances will use EnemyZig.Move() instead

13

Page 115: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ EnemyZig.Move() overrides Enemy.Move() 4! public class EnemyZig : Enemy { 5 !! public override void Move () { 6 !! ! Vector3 tempPos = pos; 7 !! ! tempPos.x = Mathf.Sin(Time.time * Mathf.PI*2) * 4; 8 !! ! pos = tempPos; // Uses the pos property of the superclass 9 !! ! base.Move(); // Calls Move() on the superclass 10 !! } 11! }

– Now, when the Update() method in Enemy calls Move(), EnemyZig instances will use EnemyZig.Move() instead

• This moves the EnemyZig instance back and forth horizontally

13

Page 116: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ EnemyZig.Move() overrides Enemy.Move() 4! public class EnemyZig : Enemy { 5 !! public override void Move () { 6 !! ! Vector3 tempPos = pos; 7 !! ! tempPos.x = Mathf.Sin(Time.time * Mathf.PI*2) * 4; 8 !! ! pos = tempPos; // Uses the pos property of the superclass 9 !! ! base.Move(); // Calls Move() on the superclass 10 !! } 11! }

– Now, when the Update() method in Enemy calls Move(), EnemyZig instances will use EnemyZig.Move() instead

• This moves the EnemyZig instance back and forth horizontally– On line 9, base.Move() calls the Move() function on EnemyZig's

base class, Enemy

13

Page 117: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Class Inheritance§ EnemyZig.Move() overrides Enemy.Move() 4! public class EnemyZig : Enemy { 5 !! public override void Move () { 6 !! ! Vector3 tempPos = pos; 7 !! ! tempPos.x = Mathf.Sin(Time.time * Mathf.PI*2) * 4; 8 !! ! pos = tempPos; // Uses the pos property of the superclass 9 !! ! base.Move(); // Calls Move() on the superclass 10 !! } 11! }

– Now, when the Update() method in Enemy calls Move(), EnemyZig instances will use EnemyZig.Move() instead

• This moves the EnemyZig instance back and forth horizontally– On line 9, base.Move() calls the Move() function on EnemyZig's

base class, Enemy• This causes EnemyZig instances to continue to move downward as well

13

Page 118: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Chapter 25 – Summary

14

Page 119: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Chapter 25 – Summary

14

§ Classes combine data (fields) and functionality (methods)

Page 120: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Chapter 25 – Summary

14

§ Classes combine data (fields) and functionality (methods)

§ Classes can inherit from each other

Page 121: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Chapter 25 – Summary

14

§ Classes combine data (fields) and functionality (methods)

§ Classes can inherit from each other§ Classes are used in Unity as GameObject Components

Page 122: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Chapter 25 – Summary

14

§ Classes combine data (fields) and functionality (methods)

§ Classes can inherit from each other§ Classes are used in Unity as GameObject Components§ Understanding classes is the key to object-oriented

programming (OOP)

Page 123: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Chapter 25 – Summary

14

§ Classes combine data (fields) and functionality (methods)

§ Classes can inherit from each other§ Classes are used in Unity as GameObject Components§ Understanding classes is the key to object-oriented

programming (OOP)– Before OOP, games were often a single, very large function

Page 124: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Chapter 25 – Summary

14

§ Classes combine data (fields) and functionality (methods)

§ Classes can inherit from each other§ Classes are used in Unity as GameObject Components§ Understanding classes is the key to object-oriented

programming (OOP)– Before OOP, games were often a single, very large function– With OOP, each object in the game is a class, and each class

can think for itself

Page 125: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Chapter 25 – Summary

14

§ Classes combine data (fields) and functionality (methods)

§ Classes can inherit from each other§ Classes are used in Unity as GameObject Components§ Understanding classes is the key to object-oriented

programming (OOP)– Before OOP, games were often a single, very large function– With OOP, each object in the game is a class, and each class

can think for itself

§ Next Chapter: Object-Oriented Thinking

Page 126: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Chapter 25 – Summary

14

§ Classes combine data (fields) and functionality (methods)

§ Classes can inherit from each other§ Classes are used in Unity as GameObject Components§ Understanding classes is the key to object-oriented

programming (OOP)– Before OOP, games were often a single, very large function– With OOP, each object in the game is a class, and each class

can think for itself

§ Next Chapter: Object-Oriented Thinking– The next chapter talks more about the OOP mentality

Page 127: CLASSES - Introduction to Game Design, Prototyping, and ...book.prototools.net/wp-content/uploads/2014/06... · – The Enemy class is for a simple top-down space shooter game –

Chapter 25 – Summary

14

§ Classes combine data (fields) and functionality (methods)

§ Classes can inherit from each other§ Classes are used in Unity as GameObject Components§ Understanding classes is the key to object-oriented

programming (OOP)– Before OOP, games were often a single, very large function– With OOP, each object in the game is a class, and each class

can think for itself

§ Next Chapter: Object-Oriented Thinking– The next chapter talks more about the OOP mentality– Also has a section where you make procedural art!