lecture 1 - brown universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · lecture 1 tips...

66
LECTURE 1 LECTURE 1 Announcements Announcements

Upload: others

Post on 10-Mar-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

LECTURE 1LECTURE 1

AnnouncementsAnnouncements

Page 2: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Tic is over!

Page 3: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Tic Feedback• Don’t fall behind!

you

the rest of the course

us too

tic

Page 4: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Global Reqs• They exist! • Cover broad standards for every project

– runs 20+ FPS, engine and game code separated, doesn't crash, etc.

• Will be publishing them to the website later today

Page 5: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Rubrics• /course/cs1971/rubrics/<project>• Copy over grade.txt for each checkpoint• Makes grading easier for us

Page 6: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

• We won't be having playtesting today

• Issues with our playtesting script

• Confusion over global requirements

• But no playtesting means you get to make up playtesting reqs!

Playtesting

Page 7: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Screen vs. UIElement• Screens are NOT UIElements• Similar methods, fundamentally different

bits of software• UIElements facilitate player interaction

with the game• Screens store everything about the game,

not just things users can interact with

Page 8: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Handing In• Please include project files and run the

project once on a department machine• If your eclipse project looks like this...• ~/course– cs1971

• tic– <login>

» src» bin

Handin from here!

Page 9: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Reminder• Have a generic Application.java in your

engine• Never import from your game package to

your engine package!

Page 10: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

This Week: Alc• 2 week project• Puzzle Game• First checkpoint

due next week

Page 11: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

LECTURE 1LECTURE 1

Game WorldGame World

Page 12: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

MOTIVATIONGame World

Page 13: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Games are busy…• All games involve a number

of game objects– May be many different types

of objects– May be many instances of

the same type of object– Maybe both of the above

• They exist in their own universe– If our entire universe is a

game, you’re just an object• We need to take the burden

of representing and organizing these objects off the game code

Page 14: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

High Level RepresentationThe GameObjects• Small collections of

functionality• Hold their own logic

and state• Don’t know much

about the rest of the game

The GameWorld• The overarching

collection of GameObjects

• Responsible for global logic and facilitating entity logic

• Represents the boundary between program and game

Page 15: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Without a Game World

GameScreen

Application

FrontEnd

Tile[][]UnitUnitUnitUnitUnitUnit UnitUnitUnitUnitUnitProjectiles

UnitUnitUnitUnitUnitAIUnit

Viewport

Page 16: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

With a Game World

GameScreen

Application

FrontEnd

Tile[][] UnitUnitUnitUnitUnitUnitUnitUnitUnitUnitUnitBullet

UnitUnitUnitUnitUnitAIUnitWorld

Viewport

Page 17: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

RESPONSIBILITIESGame World

Page 18: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

World

What does a world do?• Represents all

GameObjects in a single space

• Centralizes object management– Maintains list of

GameObjects– Passes ticks and draws

to GameObjects

GameObjects

World logic

World logic

UnitUnitUnitUnitUnitBullet

Player

UnitUnitUnitUnitUnitEnemy

Boss

Background

Page 19: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Multi-pass logic• Ticking and drawing

GameObjects in the wrong order leads to undesirable behavior– Drawing background over

everything else• World can selectively update

state in order– E.g. tick all GameObjects so

they update position, *then* check for collisions

– Should have a way for specifying draw order

Page 20: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

EventHandling• Passing events can get cumbersome• Combined mouse-key events can get

complicated• Solution- store input state in

GameWorld• Array of 256 booleans/enums instead of

key events• Keep track of mouse position/state

Page 21: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

QUESTIONS?Game Worlds

Page 22: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

LECTURE 1LECTURE 1Engine FrameworkEngine Framework

Page 23: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

GAME OBJECTSEngine Framework

Page 24: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

What is a game object?• Everything your

GameWorld holds• Your background• Your walls• Your character• Your enemies

Page 25: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

They aren’t everything• Your screens aren’t• Your UI probably

isn’t• Saving/loading isn't

Page 26: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

• Consider a simple game with:– Trees– Skeletons

GameObject• Has a sprite

Skeleton• Can move

Tree• Can grow

Hierarchical design

Page 27: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Hierarchical design• Now we reach a

problem• Where does the

Whomping Willow fit in?

GameObject• Has a sprite

Skeleton• Can move

Tree• Can grow

Page 28: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Hierarchical design• Can make a separate

parent class and re-implement moving

• Can add below skeleton and add ability to grow

• Both not ideal• Only get worse as the game

gets bigger

GameObject• Has a sprite

Skeleton• Can move

Tree• Can grow

Page 29: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Solution• Component-based

design• Everything is a game

object… and only a game object

• Leave game objects dumb

• Let their *components* do all the heavy lifting

GameObject

Page 30: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Solution• GameObjects are just lists of behaviors• The behaviors implement all relevant

functionality• Composition over inheritanceGameObject

• List of behaviors

HealthBehavior

• Health bar

MoveBehavior• Updates

position

KeyControlBehavior• Responds to key

presses

AIBehavior• Makes

decisions

DrawBehavior• Has a Sprite

Page 31: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Component-based design• The appearance and logic of each object

is defined by its components• Making new objects is as easy as adding

new components

Page 32: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

GameObject Contract• An object needs to:– Add a component – Remove a component – Access a component– Update each of its component– Tick ticable components– Draw drawable components

Page 33: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

GameObject Contractprivate ArrayList<Component> _components;public void addComponent(Component c);public void removeComponent(Component c);public Component getComponent(String tag);

public void tick(long t);public void draw(GraphicsContext g);

Page 34: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Component Contract• Needs to respond to ticks and draw

events (can be empty)

public void tick(long nanosSinceLastTick);public void draw(GraphicsContext g);

Page 35: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

SYSTEMSEngine Framework

Page 36: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Systems• Organize shared behavior

– E.g., not all GameObjects will be drawn– GraphicsSystem should only hold GameObjects

that have drawable behaviors• Each System stores a list of relevant GameObjects

and calls the relevant method on each of them• E.g.

– TimerSystem calls tick(long b) on GameObjects

Page 37: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Systems• Your choice of when to register

GameObjects with Systems– On instantiation of the relevant behavior– On addition to the GameWorld

• Your choice of how to register GameObjects with Systems– Manually, e.g. soundSystem.addObject(obj);– Automatically, e.g. have system ask each

GameObject if it’s interested

Page 38: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Enforcing Draw Order• Can set drawing layers– Array of arraylists– Add objects to relevant list based on draw

order– onDraw, go through the array, drawing

everything in the list at [0], then [1], etc.– Guarantees consistent draw order

Page 39: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Enforcing Draw Order• Alternatively: use a TreeSet– log time lookup/insertion/removal– Predictable order (can set priority for

drawing)– Adding the same GameObject twice won’t

leave a duplicate– Requires custom comparator

Page 40: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

QUESTIONS?Game Worlds

Page 41: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

LECTURE 1LECTURE 1

ViewportsViewports

Page 42: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

MOTIVATIONViewports

Page 43: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Sometimes screen space is hard

• Theoretically everything can be done in screen space– E.g. specifying size of objects

in pixels, position in pixel offset• But some things can be very

hard– E.g. panning– Imagine four-player split screen

• Games shouldn’t have to worry about how the screen draws them

Page 44: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

0,0 1,0 2,0

0,1 1,1 2,1

0,2 1,2 2,2

0,0 1,0 2,0

0,1 1,1 2,1

0,2 1,2 2,2

Game-space

Game space vs. Screen space• In nearly all games, it

makes sense to think of the game as existing in its own “space”

• Add a UI element that has “view” into the GameWorld

• This is a viewport

Screen

UI

UIUI

UI

Page 45: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

0,0 1,0 2,0

2,1

0,2 1,2 2,2

Space Conversions• Needs:

– Upper left corner in screen space

– Upper left corner in game space

– Scale/zoom/pixels per game coordinate

• Game point to screen:1. Minus upper left in game

space2. Multiply by scale3. Add viewport upper left

• Screen point to game:– Do the OPPOSITE of the

steps IN REVERSE

1.5, 2.0

0.5, 0.8

120, 20

Scale: 100 px/unit

1.5, 2.01. 1.0, 1.22. 100,

1203. 220,

140

220, 140

Page 46: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

IMPLEMENTATIONViewports

Page 47: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Implementing viewports1. Store the clip and transform (g.save())2. Set the clip

a. You will draw outside the bounds of the viewport otherwise

3. Set the transform4. Draw the game-space in its own

coordinates5. Restore the clip and transform

(g.restore())

Page 48: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Setting the Clip1.Set a path2.Call g.clip()

• Easy way to set a path:– g.rect(x, y, w h)

Page 49: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Setting the Clip• Fancier Paths:

– g.beginPath()– g.moveTo(x, y)– g.lineTo(x, y)– g.arcTo(x1, y1, x2, y2, r)– g.closePath()

Page 50: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

More on Paths• After setting up a path, you can also:

– g.fill()– g.stroke()

Page 51: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Affine Transforms• JavaFX’s Affine keeps track of geometric transforms for

drawing• Can create an Affine that converts from game space to

screen space– Check out appendTranslation(), appendScale(), and others

• How to use the Affine once we have it– A GraphicsContext instance maintains an internal transform– The transform is applied to objects before they are drawn– Use getTransform() to get the current transformation to modify– Use setTransform(...) to set the modified Affine

Page 52: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

0,0 1,0 2,0

2,1

0,2 1,2 2,2

Space Conversions• Affine will handle these

conversions!• Game point to screen:

1. Minus upper left in game space

2. Multiply by scale3. Add viewport upper left

• Clip guarantees you won't draw outside of the green box

1.5, 2.0

0.5, 0.8

120, 20

Scale: 100 px/unit

1.5, 2.01. 1.0, 1.22. 100,

1203. 220,

140

220, 140

Page 53: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Affine Transforms• If you ever want to do rotations in

your game, you should use Affine– appendRotation()

• Never do any rotation calculations yourself

Page 54: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

QUESTIONS?Viewports

Page 55: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Warnings!• Viewports are essential to the rest of the class –

every assignment from here on will depend on using your viewport!– Before you can playtest your game, you must have

a functioning viewport– Design well– Test thoroughly– Don’t put off bugs until later weeks

• The TA staff requires the use of Affine

Page 56: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

LECTURE 1LECTURE 1Tips for Alc ITips for Alc I

Page 57: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Zooming• Need some zoom factor• Zooming is multiplicative, not

additive– Rolling mouse wheel should * or / the

scale factor

• Need to do more– Otherwise, zooming will focus on the

top-left corner

Page 58: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Zooming on Viewport Center

• When converting game space to screen space:– Translate to viewport center in game space – Translate by (-viewportWidth / 2, -

viewportHeight / 2)– Scale by zoom value– Translate by (viewportWidth / 2,

viewportHeight / 2)

Page 59: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Zooming on Mouse Center• Make a new Affine• Calculate new scale based on zoom• Recalculate upper left corner in game

coordinates– newX = gameMouseX - ((screenMouseX -

oldX)/scale)– newY = gameMouseY - ((screenMouseY -

oldY)/scale)• Apply transformations to Affine like when you

first created it

Page 60: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

JAVA TIP OF THE WEEKTips for Tac I

Page 61: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Generics are cool!• You’ve used generics before… but

have you ever written them?• It’s as easy as:

public class SimpleContainer<T> {private T object;public void setObject(T ob) { object = ob; }public T getObject() { return object; }

}

Page 62: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Generics are cool!• Can use extends to bound the type

public class AnimalHouse<A extends Animal> {private A animal;public void houseAnimal(A a) { animal = a; }public void feedAnimal() { animal.eat(); }

}

AnimalHouse<Dog> kennel; // okayAnimalHouse<Rock> mountain; // compile error

Page 63: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Want to know more?• Effective Java by Jeff

Hao has an excellent chapter on generics

• Gives examples of where advanced generics are useful

• Can be found in the back of the SunLab

Page 64: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

Factories• Static functions that initialize objects• Useful for creating standard templates for game

objects without subclassing

public static GameObject makeElementFire() {

GameObject o = new GameObject();

o.addComponent(new ElementComponent(“fire”));

...

return o;

}

Page 65: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

QUESTIONS?Tips for Alc I

Page 66: LECTURE 1 - Brown Universitycs.brown.edu/courses/cs1971/lectures/lecture01.pdf · LECTURE 1 Tips for Alc I. Zooming • Need some zoom factor • Zooming is multiplicative, not additive

TIC PLAYTESTING TIC PLAYTESTING (next week)

Yay!(next week)

Yay!