Skip to content

Commit

Permalink
Tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Aug 19, 2019
1 parent f91d103 commit ed70253
Show file tree
Hide file tree
Showing 118 changed files with 7,774 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: swift
osx_image: xcode10.1

script:
- cd Source
- xcodebuild clean build -scheme Rampage -destination 'platform=iOS Simulator,name=iPhone XR,OS=12.1'
- xcodebuild clean test -scheme Rampage -destination 'platform=iOS Simulator,name=iPhone XR,OS=12.1'
53 changes: 53 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## Change Log

Occasionally bugs happen, and given the episodic nature of this tutorial, it is difficult to address these retrospectively without changing the Git commit history.

This file is a record of bugs that have been found and fixed since the tutorial started. The dates next to each bug indicate when the fix was merged. If you completed the relevant tutorial(s) after the date listed for a given bug, you can safely ignore it.


### Wall Collisions (2019/19/08)

The original wall collision detection code described in [Part 2](Tutorial/Part2.md) had a bug that could cause the player to stick when sliding along a wall (thanks to [José Ibañez](https://twitter.com/jose_ibanez/status/1163225777401401344?s=20) for reporting).

The fix for this was to return the largest intersection detected between any wall segment, rather than just the first intersection detected. The necessary code changes are in `Actor.intersection(with map:)`, which should now look like this:

```swift
func intersection(with map: Tilemap) -> Vector? {
let minX = Int(rect.min.x), maxX = Int(rect.max.x)
let minY = Int(rect.min.y), maxY = Int(rect.max.y)
var largestIntersection: Vector?
for y in minY ... maxY {
for x in minX ... maxX where map[x, y].isWall {
let wallRect = Rect(
min: Vector(x: Double(x), y: Double(y)),
max: Vector(x: Double(x + 1), y: Double(y + 1))
)
if let intersection = rect.intersection(with: wallRect),
intersection.length > largestIntersection?.length ?? 0 {
largestIntersection = intersection
}
}
}
return largestIntersection
}
```

### Sprite Rendering (2019/02/08)

In the original version of [Part 5](Tutorial/Part5.md) there were a couple of bugs in the sprite texture coordinate calculation. In your own project, check if the `// Draw sprites` section in `Renderer.swift` contains the following two lines:

```swift
let textureX = Int(spriteX * Double(wallTexture.width))
let spriteTexture = textures[sprite.texture]
```

If so, replace them with:

```swift
let spriteTexture = textures[sprite.texture]
let textureX = min(Int(spriteX * Double(spriteTexture.width)), spriteTexture.width - 1)
```

### Texture Capitalization (2019/07/28)

This isn't exactly a bug, but the capitalization of texture image keys changed from `lowercase` to `camelCase` after [Part 5](Tutorial/Part5.md) was released. If you've been using the tutorial textures in your own project, watch out for this when updating as some of the asset file names may not match up with the keys in `Textures.swift`.
110 changes: 109 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,111 @@
## Retro Rampage

This repository contains a code snapshot for the [Retro Rampage tutorial series](https://github.com/nicklockwood/RetroRampage) by Nick Lockwood.
[![PayPal](https://img.shields.io/badge/paypal-donate-blue.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CR6YX6DLRNJTY&source=url)
[![Travis](https://travis-ci.org/nicklockwood/RetroRampage.svg)](https://travis-ci.org/nicklockwood/RetroRampage)
[![Swift 4.2](https://img.shields.io/badge/swift-4.2-red.svg?style=flat)](https://developer.apple.com/swift)
[![License](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://opensource.org/licenses/MIT)
[![Twitter](https://img.shields.io/badge/[email protected])](http://twitter.com/nicklockwood)

![Screenshot](Tutorial/Images/FunctioningDoor.png)

### About

Retro Rampage is a tutorial series in which you will learn how to build a Wolfenstein-like game from scratch, in Swift. Initially the game will be targeting iPhone and iPad, but the engine should work on any platform that can run Swift code.

Modern shooters have moved on a bit from Wolfenstein's grid-based 2.5D world, but we're going to stick with that template for a few reasons:

* It's feasible to build Wolfenstein's 3D engine from scratch, without a lot of complicated math and without needing to know anything about GPUs or shaders.

* It's simple to create and visualize maps that are constructed on a 2D grid, avoiding the complexities of 3D modeling and animation tools.

* Tile grids are an excellent way to prototype techniques such as procedural map generation, pathfinding and line-of-sight calculations, which can then be applied to more complex worlds.

### Background

Ever since I first played Wolfenstein 3D on a friend's battered old 386 back in 1993, I was hooked on the *First-Person Shooter*.

As an aspiring programmer, I wanted to recreate what I had seen. But armed only with 7th grade math and a rudimentary knowledge of BASIC, recreating the state-of-the-art in modern PC 3D graphics was hopelessly beyond my reach.

More than two decades later, a few things have changed:

We have the iPhone - a mobile computer many hundreds of times more powerful than a DOS-era desktop PC; We have Swift - a simple, powerful programming language with which to write apps and games; Finally - and most importantly - we have the Wolfenstein source code, and the wizardry behind it has been thoroughly demystified.

I guess now is as good a time as any to scratch that quarter-century itch and build an FPS!

### Tutorials

The tutorials below are designed to be completed in order, and each step builds on the code from the previous one. If you decide to skip ahead, project snapshots for each step are available [here](https://github.com/nicklockwood/RetroRampage/releases).

The tutorials are written with the assumption that you are already familiar with Xcode and are comfortable setting up an iOS project and adding new files to it. No knowledge of advanced Swift features is required, so it's fine if you've only used Objective-C or other C-like languages.

[Part 1 - Separation of Concerns](Tutorial/Part1.md)

Unlike most apps, games are typically designed to be independent of any given device or OS. Swift has already been ported to many platforms outside of the Apple ecosystem, including Android, Ubuntu, Windows and even Raspberry Pi. In this first part, we'll set up our project to minimize dependencies with iOS and provide a solid foundation for writing a fully portable game engine.

[Part 2 - Mazes and Motion](Tutorial/Part2.md)

Wolfenstein 3D is really a 2D game projected into the third dimension. The game mechanics work exactly the same as for a top-down 2D shooter, and to prove that we'll begin by building the game from a top-down 2D perspective before we make the shift to first-person 3D.

[Part 3 - Ray Casting](Tutorial/Part3.md)

Long before hardware accelerated 3D graphics, some of the greatest game programmers of our generation were creating incredible 3D worlds armed only with a 16-bit processor. We'll follow in their footsteps and bring our game into the third dimension with an old-school graphics hack called *ray casting*.

[Part 4 - Texture Mapping](Tutorial/Part4.md)

In this chapter we'll spruce up the bare walls and floor with *texture mapping*. Texture mapping is the process of painting or *wall-papering* a 3D object with a 2D image, helping to provide the appearance of intricate detail in an otherwise featureless surface.

[Part 5 - Sprites](Tutorial/Part5.md)

It's time to introduce some monsters to keep our player company. We'll display these using *sprites* - a popular technique used to add engaging content to 3D games in the days before it was possible to render textured polygonal models in real-time with sufficient detail.

[Part 6 - Enemy Action](Tutorial/Part6.md)

Right now the monsters in the maze are little more than gruesome scenery. We'll bring those passive monsters to life with collision detection, animations, and artificial intelligence so they can hunt and attack the player.

[Part 7 - Death and Pixels](Tutorial/Part7.md)

In this part we'll implement player damage, giving the monsters the ability to hurt and eventually kill the game's protagonist. We'll explore a variety of damage effects and techniques, including a cool Wolfenstein transition called *fizzlefade*.

[Part 8 - Target Practice](Tutorial/Part8.md)

We'll now give the player a weapon so they can fight back against the ravenous monsters. This chapter will demonstrate how to extend our drawing logic to handle screen-space sprites, add a bunch of new animations, and figure out how to implement reliable collision detection for fast-moving projectiles.

[Part 9 - Performance Tuning](Tutorial/Part9.md)

The new effects we've added are starting to take a toll on the game's frame rate, especially on older devices. Let's take a break from adding new features and spend some time on improving the rendering speed. In this chapter we'll find out how to diagnose and fix performance bottlenecks, while avoiding the kind of micro-optimizations that will make it harder to add new features later on.

[Part 10 - Sliding Doors](Tutorial/Part10.md)

In this chapter we add another iconic feature from Wolfenstein - the sliding metal doors between rooms. These add some interesting challenges as the first non-static, non-grid-aligned scenery in the game.

### Bugs

I've occasionally made retrospective fixes after a tutorial chapter was published. This will normally be called out in a later tutorial if it directly impacts any new code, but it's a good idea to periodically check the [CHANGELOG](CHANGELOG.md) for fixes.

### Acknowledgments

I'd like to thank [Nat Brown](https://github.com/natbro) and [PJ Cook](https://github.com/pjcook) for their invaluable feedback on the first draft of these tutorials.

Thanks also to [Lode Vandevenne](https://github.com/lvandeve) and [Fabien Sanglard](https://github.com/fabiensanglard/), whom I've never actually spoken to, but whose brilliant explanations of ray casting and the Wolfenstein engine formed both the basis and inspiration for this tutorial series.

### Experiments

If you're up to date with the tutorials, and can't wait for the next chapter, you might like to check out some of the [experimental PRs](https://github.com/nicklockwood/RetroRampage/pulls) on Github.

These experiments demonstrate advanced features that we aren't quite ready to explore in the tutorials yet.

### Further Reading

If you've exhausted the tutorials and experiments and are still eager to learn more, here are some resources you might find useful:

* [Lode's Raycasting Tutorial](https://lodev.org/cgtutor/raycasting.html#Introduction) - A great tutorial on ray casting, implemented in C++.
* [Game Engine Black Book: Wolfenstein 3D](https://www.amazon.co.uk/gp/product/1727646703/ref=as_li_tl?ie=UTF8&camp=1634&creative=6738&creativeASIN=1727646703&linkCode=as2&tag=charcoaldesig-21&linkId=aab5d43499c96f7417b7aa0a7b3e587d) - Fabien Sanglard's excellent book about the Wolfenstein 3D game engine.
* [Swiftenstein](https://github.com/nicklockwood/Swiftenstein) - A more complete but less polished implementation of the ideas covered in this tutorial.
* [Handmade Hero](https://handmadehero.org) - A video series in which games industry veteran [Casey Muratori](https://github.com/cmuratori) builds a game from scratch in C.

### Tip Jar

I started this tutorial series thinking it would take just a few days. Many months later, with no end in sight, I realize I may have been a bit naive. If you've found it interesting, please consider donating to my caffeine fund.

[![Donate via PayPal](https://www.paypalobjects.com/en_GB/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CR6YX6DLRNJTY&source=url)

Binary file added Tutorial/Images/AddUnitTestingBundle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/Architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/AttackAnimation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/BigPixelFizzle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/BitmapOpacityTest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/BlackSpriteBackground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/BlendColorCrash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/BlurryBluePixel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/ClearSpriteBackground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/CollisionLoop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/CollisionResponse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/ColumnOrderTest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/CurvedWalls.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/DeathEffectTimeline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/DisableSafetyChecks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/DoorInCorner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/DoorRays.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/DoorTextures.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/Doorjamb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/DoorjambTextures.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/DraggableJoystick.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/EasingCurves.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/FieldOfView.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/FizzleFade.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/FloatingJoystick.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tutorial/Images/FunctioningDoor.png
Binary file added Tutorial/Images/HurtThroughDoor.png
Binary file added Tutorial/Images/ImprovedTextureTrace.png
Binary file added Tutorial/Images/InlineSetterTest.png
Binary file added Tutorial/Images/Lighting.png
Binary file added Tutorial/Images/LineDrawing.png
Binary file added Tutorial/Images/LineOfSight.png
Binary file added Tutorial/Images/LinearAccessTest.png
Binary file added Tutorial/Images/MemoryHierarchy.png
Binary file added Tutorial/Images/MonsterDeath.png
Binary file added Tutorial/Images/MonsterInWall.png
Binary file added Tutorial/Images/MonsterInWall2.png
Binary file added Tutorial/Images/MonsterMob.png
Binary file added Tutorial/Images/MonsterSprite.png
Binary file added Tutorial/Images/MonsterSurprise.png
Binary file added Tutorial/Images/MonstersAttacking.png
Binary file added Tutorial/Images/MuzzleFlash.png
Binary file added Tutorial/Images/NoInliningSubscript.png
Binary file added Tutorial/Images/OptimizedBlendTrace.png
Binary file added Tutorial/Images/OptimizedOrderTrace.png
Binary file added Tutorial/Images/OverlappingAccess.png
Binary file added Tutorial/Images/PerspectiveView.png
Binary file added Tutorial/Images/PistolFiring.png
Binary file added Tutorial/Images/PistolOverlay.png
Binary file added Tutorial/Images/PistolPosition.png
Binary file added Tutorial/Images/PistolSprite.png
Binary file added Tutorial/Images/PixelOpacityTest.png
Binary file added Tutorial/Images/ProjectStructure.png
Binary file added Tutorial/Images/Pythagoras.png
Binary file added Tutorial/Images/RayFan.png
Binary file added Tutorial/Images/RayLengths.png
Binary file added Tutorial/Images/RayTileIntersection.png
Binary file added Tutorial/Images/RedFlashEffect.png
Binary file added Tutorial/Images/RedSpriteBackground.png
Binary file added Tutorial/Images/ReleaseMode.png
Binary file added Tutorial/Images/ReleaseModeTests.png
Binary file added Tutorial/Images/RemoveMultiplicationTest.png
Binary file added Tutorial/Images/RotatedOutput.png
Binary file added Tutorial/Images/RotatedSprites.png
Binary file added Tutorial/Images/RoundingErrors.png
Binary file added Tutorial/Images/SafetyChecksOffTest.png
Binary file added Tutorial/Images/ScrambledOutput.png
Binary file added Tutorial/Images/SetBaseline.png
Binary file added Tutorial/Images/SharpBluePixel.png
Binary file added Tutorial/Images/SlopeIntercept.png
Binary file added Tutorial/Images/SmoothedDeathEffect.png
Binary file added Tutorial/Images/SolidFloorColor.png
Binary file added Tutorial/Images/SortedSprites.png
Binary file added Tutorial/Images/SpriteBehindPlayer.png
Binary file added Tutorial/Images/SpriteLines.png
Binary file added Tutorial/Images/SpriteOrderBug.png
Binary file added Tutorial/Images/SpriteRadius.png
Binary file added Tutorial/Images/SpriteRayBug.png
Binary file added Tutorial/Images/SpriteRayIntersection.png
Binary file added Tutorial/Images/StagedResponse.png
Binary file added Tutorial/Images/StateMachine.png
Binary file added Tutorial/Images/StoredWidthTest.png
Binary file added Tutorial/Images/StraightWalls.png
Binary file added Tutorial/Images/StretchedWalls.png
Binary file added Tutorial/Images/TextureLookupTrace.png
Binary file added Tutorial/Images/TextureMapping.png
Binary file added Tutorial/Images/TextureSmearing.png
Binary file added Tutorial/Images/TextureVariants.png
Binary file added Tutorial/Images/TexturedCeiling.png
Binary file added Tutorial/Images/TexturedFloor.png
Binary file added Tutorial/Images/TexturesAndLighting.png
Binary file added Tutorial/Images/TileEdgeHit.png
Binary file added Tutorial/Images/TileSteps.png
Binary file added Tutorial/Images/Tilemap.png
Binary file added Tutorial/Images/UnoptimizedTest.png
Binary file added Tutorial/Images/UnoptimizedTrace.png
Binary file added Tutorial/Images/VariedTextures.png
Binary file added Tutorial/Images/ViewFrustum.png
Binary file added Tutorial/Images/ViewPlane.png
Binary file added Tutorial/Images/WalkingFrames.png
Binary file added Tutorial/Images/WallCollisions.png
Binary file added Tutorial/Images/WallDistance.png
Binary file added Tutorial/Images/WallHit.png
Binary file added Tutorial/Images/WallTexture.png
Binary file added Tutorial/Images/WolfensteinSprites.png
Loading

0 comments on commit ed70253

Please sign in to comment.