510 likes | 603 Views
Lecture 1. Announcements. Tic is over!. Collaboration policies!. Design checks. How was the signup process? Were they helpful ? Would you rather have more TA hours instead?. You can run demos!. cs195n_demo tac2 zdavis. cs195n_demo tic zdavis. Tic first impressions.
E N D
Lecture 1 Announcements
Design checks • How was the signup process? • Were they helpful? • Would you rather have more TA hours instead?
You can run demos! cs195n_demo tac2 zdavis cs195n_demo tic zdavis
Tic first impressions • Lots of carrying around separate x, y • This leads to copy-pasting x’s code to write y’s 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
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
Announcements Questions?
Lecture 1 Viewports
Viewports Motivation
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 isn’t visible at one time Video time!
Game space vs. Screen space Game-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 0,0 0,0 1,0 1,0 2,0 2,0 0,1 0,1 1,1 1,1 2,1 2,1 Screen 0,2 0,2 1,2 1,2 2,2 2,2 UI UI UI UI
The math • Scale = screen size / game size • (in pixels per game coordinate unit) • Game point to screen: • Minus game upper left • Multiply by scale • Add screen upper left • Screen point to game: • Do the OPPOSITE of the steps IN REVERSE 0,0 1,0 0.5, 0.8 2,0 Scale: 100 px/unit 1.5, 2.0 2,1 120, 20 0,2 1,2 2,2 1.5, 2.0 1.0, 1.2 100, 120 220, 140 220, 140
Viewports Implementation
Implementing viewports • (Optional) Set the clip (g.clipRect()) • You will draw out of bounds otherwise • “Set” the “transform” • Draw the game-space in its own coordinates • “Restore” the “transform” • Restore the clip (if you set it)
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
Viewports Questions?
Lecture 1 Pathfinding
Pathfinding Motivation
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.
Pathfinding Dijkstra’s Algorithm
Dijkstra’s • 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 it’s time to process the end node, you’re done
Pathfinding A*
General idea • Dijkstra’s assumes it’s 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
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)
Pathfinding Questions?
Lecture 1 Content Management I
Content Management I What is content?
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”
Content Management I Why not hardcode ASSETS?
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
Solution: break into files • Engine/game can load and unload assets as necessary or desired • It’s like JIT for your game content! • Non-programmers don’t 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
Content Management I Questions?
Lecture 1 Tips for Tac I
Tips for Tac I File Parsing
File parsing! • Good news: game-side • Bad news: So many things can go wrong! • Map file can’t be opened • Map file is empty • Map file is a directory • Map file is a JPEG • Is a map file, but has inconsistent data
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
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
Tips for Tac I Unit Movement
Unit movement 3.0, 0.0 • 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,0 1,0 2,0 0.7, 0.6 0,1 1,1 2,1 1.5, 2.1 0,2 1,2 2,2
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?
Tips for Tac I Miscellaneous Tips
Miscellaneous Tips • Zooming is multiplicative, not additive • Rolling mouse wheel should * or / the scale factor • Watch out for weird behavior when changing a unit’s path while it’s moving • E.g. units moving diagonally
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 classSimpleContainer<T> {privateT object;public voidsetObject(Tob) { object = ob; }publicTgetObject() { return object; }}
Generics are cool! • Can use extends and super to bound the type public classAnimalHouse<AextendsAnimal> {privateA animal;public voidhouseAnimal(A a) { animal = a; }publicvoidfeedAnimal() { animal.eat(); }} AnimalHouse<Dog> kennel; // okay AnimalHouse<Rock> mountain; // compile error
Bounds can be confusing • Ever used enums in Java? (if not, you will soon!) • This is the declaration of the Enum superclass: public classEnum<EextendsEnum<E>> • Starts to make sense once you realize it doesn’t infinitely expand • You will have to do something similar if you want to have a generifiedGraphNode object
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
Tips for Tac I Questions?