Lecture 1 Announcements
Tic is over!
Tic Feedback Don’t fall behind! you tic
Tic Feedback Don’t fall behind! you the rest of the course us too
Escape Key Should not be defined engine-side Engine doesn’t know what the game wants to do on escape (deselect, save before exiting, etc.)
Handing In Please include project files ~/course cs1971 tic <login> src bin Hand these in!
Course Scripts They work now! Use /course/cs1971/bin/cs1971_demo <project> You should be added to the cs-1971student group this week
Hours Change Hours on Saturday 6-8 moved to Sunday 2-4 Design check hours shuffled around
This Week: Tac 4 week project 2D dungeon crawler First checkpoint due next week
Lecture 1 Game World
Game World Motivation
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
High Level Representation The GameObjects The GameWorld Small collections of functionality Hold their own logic and state Don’t know much about the rest of the game The overarching collection of GameObjects Responsible for global logic and facilitating entity logic Represents the boundary between program and game
Tac Without a Game World Tile[][] Viewport Unit Unit Projectiles GameScreen Application Unit AIUnit FrontEnd
Tac With a Game World World Viewport Tile[][] Unit Bullet GameScreen AIUnit GameScreen Application World FrontEnd
Game World Responsibilities
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 World GameObjects Unit Enemy Unit Bullet Background Boss Player World logic
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 Could have a way for specifying draw order
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
General Contract public void tick(long nanosSinceLastTic); public void draw(GraphicsContext2D g); public void onDDDEEE(DDDEvent e);
Game Worlds Questions?
Lecture 1 Engine Framework
Engine Framework Game objects
What is a game object? Everything your GameWorld holds Your background Your walls Your character Your enemies
They aren’t everything Your screens aren’t Your UI probably isn’t Map generation isn’t
Hierarchical design Consider a simple game with: Player Enemies Destructible terrain Invincible enemies GameObject Player Can move Has health bar
Hierarchical design Start with player and enemies They both move and have health bars Abstract out common code! GameObject Can move Has health bar Player Key controls Enemy AI
Hierarchical design Let’s add in destructible terrain It has a health bar but doesn’t move, time to abstract out some more GameObject Has health bar Units Can move Terrain Pretty sprite Player Key controls Enemy AI
Hierarchical design Now we reach a problem Where do our invincible enemies fit in? They move but don’t have health bars GameObject Has health bar Units Can move Terrain Pretty sprite Player Key controls Enemy AI
Hierarchical design Can make a separate class and re-implement moving Can add below enemies and hide the health bar Both not ideal Only get worse as the game gets bigger GameObject Has health bar Units Can move Terrain Pretty sprite Player Key controls Enemy AI
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 ?
Solution GameObjects are just lists of behaviors The behaviors implement all relevant functionality Composition over inheritance GameObject List of behaviors HealthBehavior Health bar MoveBehavior Updates position KeyControlBehavior Responds to key presses DrawBehavior Has a Sprite Can create game objects with any combination of these behaviors. Can do what we couldn’t do before AIBehavior Makes decisions
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
General Contract An object needs to do the following: Add a component Remove a component Access a component Pass a message to each component
General Contract private ArrayList<Behavior> _behaviors; public void addBehavior(Behavior b); public void removeBehavior(Behavior b); public Behavior getBehavior(String tag);
Expanded Contract An object should also do the following: Tick each tickable component Draw each drawable component
Expanded Contract public void tick(long b); public void draw(GraphicsContext2D b);
Behavior Contract Needs to respond to ticks and draw events (can be empty) public void tick(long nanosSinceLastTick); public void draw(GraphicsContext2D g);
Engine Framework Systems
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 interested GameObjects and calls the relevant method on each of them E.g. TimerSystem calls tick(long b) on GameObjects
Systems Your choice of when to register GameObjects with Systems On instantiation 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
Enforcing Draw Order Use a TreeSet Alternatively, set drawing layers 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 Alternatively, set drawing layers
Game Worlds Questions?
Lecture 1 Viewports
Viewports Motivation
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
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 0,0 1,0 2,0 0,1 1,1 2,1 0,2 1,2 2,2 Game-space Screen UI 0,0 1,0 2,0 0,1 1,1 2,1 0,2 1,2 2,2
Space Conversions Needs: Game point to screen: Screen point to game: Upper left corner in screen space Upper left corner in game space Scale/zoom/pixels per game coordinate Game point to screen: Minus upper left in game space Multiply by scale Add viewport upper left Screen point to game: Do the OPPOSITE of the steps IN REVERSE Conversion is called a transform 0.5, 0.8 Scale: 100 px/unit 1.5, 2.0 0,0 1,0 2,0 2,1 0,2 1,2 2,2 120, 20 1.5, 2.0 1.0, 1.2 100, 120 220, 140 220, 140 Viewport should know how to convert objects in world space to objects in screen space. Accomplish this by converting world space positions to screen space positions.
Viewports Implementation
Implementing viewports Set the clip (g.clipRect() ) You will draw outside the bounds of the viewport otherwise Set the transform Draw the game-space in its own coordinates Restore the transform Restore the clip (g.setClip(null) )
The Transform This could be implemented as follows: Don’t do this Create a wrapper for Graphics2D with a method like setViewport() When drawing a viewport, toggle a flag inside your Graphics2D wrapper that you need to transform game to screen coordinates whenever a shape is drawn Do separate game object transforms in each object’s own onDraw() call Unmark your wrapper when finished drawing the viewport Don’t do this Summary Before we draw each shape, convert it’s vertices from world space to screen space But we already have a way to do this…
Affine Transforms Java’s AffineTransform keeps track of geometric transformations for drawing Uses concepts from linear algebra Haven’t taken it? No problem! Can create an AffineTransform that converts from world space to screen space Check out translate(), scale(), and others How to use the AffineTransform once we have it A Graphics2D instance maintains an internal transform The transform is applied to objects before they are drawn Use setTransform() and getTransform() to modify the current AffineTransform Similar to other 2D and 3D graphics pipelines In this case, we can create an AffineTransform object that converts world space positions to screen space “Maintains an internal transform” which it applies to objects before they are drawn
Viewports Questions?
Warnings! Viewports are essential to the rest of the class – every assignment from here on will depend on using your viewport! Hard to test your game if you can’t display it correctly Design well Test thoroughly Don’t put off bugs until later weeks The TA staff requires the use of AffineTransform If you do not feel comfortable with using AffineTransforms, come to hours!
Lecture 1 Tips for Tac I
Viewports Zooming is multiplicative, not additive Rolling mouse wheel should * or / the scale factor Some zoom levels don’t make sense Optional: set zoom in/out limits! Most maps don’t go on forever Optional: set pan limits!
Tips for Tac I Java Tip Of The Week
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; } }
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; // okay AnimalHouse<Rock> mountain; // compile error
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
Tips for Tac I Questions?
Game Design 1 Introduction
Video game creation is an art, a science, and a feat of engineering Video game creation is an art, a science, and a feat of engineering. -Jeffries van Dam
Supplement to 2D Games More “high-level” concepts, less technical Visualize game creation from a more artistic perspective Inspire ideas and concepts for the final project Perhaps influence coding style and engine design
Help create better Final Projects Ultimate Goal Help create better Final Projects
This Week The Juice Video www.youtube.com/watch?v=Fy0aCDmgnxg
Game Design: Genres Questions?
Tic Playtesting Yay!