Design checks How was the signup process? Were they helpful?
Would you rather have more TA hours instead?
Slide 5
You can run demos! cs195n_demo tac2 zdavis cs195n_demo tic
zdavis
Slide 6
Tic first impressions Lots of carrying around separate x, y
This leads to copy-pasting xs code to write ys code Which leads to
forgetting to replace an x with a y Use Vec2i and Vec2f instead!
Lots of mouse usage, better than TAs :-D
Slide 7
BGD plug The Brown Game Developers group is meeting on Friday,
4:30p-5:30p in 345 Can be a useful source of advice, within the
bounds of collab policy
Slide 8
QUESTIONS? Announcements
Slide 9
L ECTURE 1 Viewports
Slide 10
MOTIVATION Viewports
Slide 11
Sometimes screen space is hard Theoretically everything can be
done in screen space But some things can be very hard Most notably,
when the entire game isnt visible at one time Video time!
Slide 12
0,01,02,0 0,11,12,1 0,21,22,2 0,01,02,0 0,11,12,1 0,21,22,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 The
UI in screen space has a view into the game world which is just
like any other UI element Screen UI
Slide 13
0,01,02,0 2,1 0,21,22,2 The math Scale = screen size / game
size (in pixels per game coordinate unit) Game point to screen:
1.Minus game upper left 2.Multiply by scale 3.Add screen 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.0 1.1.0, 1.2 2.100,
120 3.220, 140 220, 140
Slide 14
IMPLEMENTATION Viewports
Slide 15
Implementing viewports 1.(Optional) Set the clip ( g.clipRect()
) a.You will draw out of bounds otherwise 2.Set the transform
3.Draw the game-space in its own coordinates 4.Restore the
transform 5.Restore the clip (if you set it)
Slide 16
The transform For many of you, this is as simple as: When
drawing a viewport, make a note inside your Graphics2D wrapper that
you need to transform game->screen coordinates whenever a shape
is drawn Unmark when finished drawing the viewport For cs123
students who want to be fancy, use AffineTransform and the
setTransform() and getTransform() of Graphics2D AffineTransform
used like OpenGL
Slide 17
QUESTIONS? Viewports
Slide 18
L ECTURE 1 Pathfinding
Slide 19
MOTIVATION Pathfinding
Slide 20
Why is pathfinding important? NPCs need to navigate an
environment that has obstructions Represented as a graph Goal: find
minimum cost path from A to B Cost includes factors such as
distance, terrain, position of enemies.
Slide 21
DIJKSTRAS ALGORITHM Pathfinding
Slide 22
Dijkstras Basic idea: Process nodes in order of shortest
distance from start To process a node, update cost to each neighbor
and add to PriorityQueue, then never process this node again Each
node keeps track of shortest distance and pointer to previous node
When its time to process the end node, youre done
Slide 23
Why Dijkstras can be gross
Slide 24
A* Pathfinding
Slide 25
General idea Dijkstras assumes its impossible to predict cost
This is overly pessimistic In pathfinding, we at least know the
general direction we want to go A* is a graph traversal algorithm
that takes advantage of this
Slide 26
How does it work? Uses a heuristic to guess the cost from any
given node to the destination node Heuristic passed by the caller
In addition to tracking distance from start, track heuristic value
for each node Prioritize in PriorityQueue based on
distance+heuristic This can be as simple as the Euclidean distance
between the given and destination node, but can also try to take
other factors Just be sure to NEVER OVERESTIMATE (suboptimal
results)
Slide 27
QUESTIONS? Pathfinding
Slide 28
L ECTURE 1 Content Management I
Slide 29
WHAT IS CONTENT? Content Management I
Slide 30
Content Types of content Sprites, images, textures Music, sound
effects Level/map files Scripts Dialogue A single logical piece of
content is called an asset
Slide 31
WHY NOT HARDCODE ASSETS? Content Management I
Slide 32
Hardcoding Extreme executable bloat Large games cannot fit in
memory Have to recompile entire program every time an asset changes
Still okay sometimes (e.g. the program icon in Windows) Your RAM:
8GB Dragon Age: Origins: 20GB
Slide 33
Solution: break into files Engine/game can load and unload
assets as necessary or desired Its like JIT for your game content!
Non-programmers dont need to compile Level designers only need to
touch map files Artists only need to touch image files Programmers
compile builds for everyone else More maintainable in general
Slide 34
QUESTIONS? Content Management I
Slide 35
L ECTURE 1 Tips for Tac I
Slide 36
FILE PARSING Tips for Tac I
Slide 37
File parsing! Good news: game-side Bad news: So many things can
go wrong! Map file cant be opened Map file is empty Map file is a
directory Map file is a JPEG Is a map file, but has inconsistent
data
Slide 38
Parse safely Read in a line, then parse it, repeat At least you
can report the line count where an error happened Recommended
classes: BufferedReader (for reading lines) Scanner + StringReader
(for parsing each line) Catch, wrap, and rethrow exceptions Level
parsing should only throw a LevelParseException that is caused by
other exceptions
Slide 39
If possible, report errors nicely Good errors: Line 1: , is not
a valid map size Line 7: Row 3 was 24 tiles wide, map is 25 tiles
wide Line 2: Expected boolean, got FLSE instead Bad errors:
java.util.InputMismatchException (null) The map could not be
parsed. Goodbye. (program crashes with giant stack trace) If this
happens you will get an incomplete
Slide 40
UNIT MOVEMENT Tips for Tac I
Slide 41
Unit movement Tiles best stored in an array (discrete indices)
But game space is continuous! Define tile x,y to take up space
[x,x+1) and [y,y+1) Then: Vec2i.fromFloored() Move unit centers
with Vec2f.lerpTo() 0,01,02,0 0,11,12,1 0,21,22,2 3.0, 0.0 0.7, 0.6
1.5, 2.1
Slide 42
Unit exclusion Unit sitting on tile is obvious But what about
while moving? When is it no longer on the first tile? When does it
officially reach the second tile? Can it briefly monopolize two
tiles at once? ???
Slide 43
MISCELLANEOUS TIPS Tips for Tac I
Slide 44
Miscellaneous Tips Zooming is multiplicative, not additive
Rolling mouse wheel should * or / the scale factor Watch out for
weird behavior when changing a units path while its moving E.g.
units moving diagonally
Slide 45
JAVA TIP OF THE WEEK Tips for Tac I
Slide 46
Generics are cool! Youve used generics before but have you ever
written them? Its as easy as: public class SimpleContainer {
private T object; public void setObject(T ob) { object = ob; }
public T getObject() { return object; } }
Slide 47
Generics are cool! Can use extends and super to bound the type
public class AnimalHouse { private A animal; public void
houseAnimal(A a) { animal = a; } public void feedAnimal() {
animal.eat(); } } AnimalHouse kennel; // okay AnimalHouse mountain;
// compile error
Slide 48
Bounds can be confusing Ever used enums in Java? (if not, you
will soon!) This is the declaration of the Enum superclass: public
class Enum > Starts to make sense once you realize it doesnt
infinitely expand You will have to do something similar if you want
to have a generified GraphNode object
Slide 49
Want to know more? Effective Java by Joshua Bloch has an
excellent chapter on generics Gives examples of where advanced
generics are useful Can be found in the back of the Sun Lab