Download presentation
Presentation is loading. Please wait.
1
Lecture 1 Announcements
2
Tic is over!
3
Tic Feedback Don’t fall behind! you tic
4
Tic Feedback Don’t fall behind! you the rest of the course us too
5
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.)
6
Handing In Please include project files ~/course cs1971 tic
<login> src bin Hand these in!
7
Course Scripts They work now!
Use /course/cs1971/bin/cs1971_demo <project> You should be added to the cs-1971student group this week
8
Hours Change Hours on Saturday 6-8 moved to Sunday 2-4
Design check hours shuffled around
9
This Week: Tac 4 week project 2D dungeon crawler
First checkpoint due next week
10
Lecture 1 Game World
11
Game World Motivation
12
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
13
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
14
Tac Without a Game World
Tile[][] Viewport Unit Unit Projectiles GameScreen Application Unit AIUnit FrontEnd
15
Tac With a Game World World Viewport Tile[][] Unit Bullet GameScreen
AIUnit GameScreen Application World FrontEnd
16
Game World Responsibilities
17
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
18
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
19
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
20
General Contract public void tick(long nanosSinceLastTic); public void draw(GraphicsContext2D g); public void onDDDEEE(DDDEvent e);
21
Game Worlds Questions?
22
Lecture 1 Engine Framework
23
Engine Framework Game objects
24
What is a game object? Everything your GameWorld holds Your background
Your walls Your character Your enemies
25
They aren’t everything
Your screens aren’t Your UI probably isn’t Map generation isn’t
26
Hierarchical design Consider a simple game with: Player Enemies
Destructible terrain Invincible enemies GameObject Player Can move Has health bar
27
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
28
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
29
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
30
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
31
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 ?
32
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
33
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
34
General Contract An object needs to do the following: Add a component
Remove a component Access a component Pass a message to each component
35
General Contract private ArrayList<Behavior> _behaviors; public void addBehavior(Behavior b); public void removeBehavior(Behavior b); public Behavior getBehavior(String tag);
36
Expanded Contract An object should also do the following:
Tick each tickable component Draw each drawable component
37
Expanded Contract public void tick(long b); public void draw(GraphicsContext2D b);
38
Behavior Contract Needs to respond to ticks and draw events (can be empty) public void tick(long nanosSinceLastTick); public void draw(GraphicsContext2D g);
39
Engine Framework Systems
40
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
41
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
42
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
43
Game Worlds Questions?
44
Lecture 1 Viewports
45
Viewports Motivation
46
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
47
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
48
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.
49
Viewports Implementation
50
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) )
51
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…
52
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
53
Viewports Questions?
54
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!
55
Lecture 1 Tips for Tac I
56
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!
57
Tips for Tac I Java Tip Of The Week
58
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; } }
59
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
60
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
61
Tips for Tac I Questions?
62
Game Design 1 Introduction
63
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
64
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
65
Help create better Final Projects
Ultimate Goal Help create better Final Projects
66
This Week The Juice Video
67
Game Design: Genres Questions?
68
Tic Playtesting Yay!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.