Book Image

TypeScript Blueprints

By : Ivo Gabe de Wolff
Book Image

TypeScript Blueprints

By: Ivo Gabe de Wolff

Overview of this book

TypeScript is the future of JavaScript. Having been designed for the development of large applications, it is now being widely incorporated in cutting-edge projects such as Angular 2. Adopting TypeScript results in more robust software - software that is more scalable and performant. It's scale and performance that lies at the heart of every project that features in this book. The lessons learned throughout this book will arm you with everything you need to build some truly amazing projects. You'll build a complete single page app with Angular 2, create a neat mobile app using NativeScript, and even build a Pac Man game with TypeScript. As if fun wasn't enough, you'll also find out how to migrate your legacy codebase from JavaScript to TypeScript. This book isn't just for developers who want to learn - it's for developers who want to develop. So dive in and get started on these TypeScript projects.
Table of Contents (16 chapters)
TypeScript Blueprints
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Handling events


We will create an event handler in lib/game/event.ts. The event handler must set the correct movement direction in the state. The time handler will then use this to update the direction of the player. The step can only do that when the player is aligned to the grid. If the player is between two fields on the grid, we will not change the direction of the player, since he will then probably head into a wall.

Working with key codes

An event provides the key code of the pressed or released key. We can get this code of a certain character with "x".charCodeAt(0) (where x is the character). The key codes of left, top, right, and bottom are 37, 38, 39, and 40.

First, we must create a helper function that transforms a key code to the Movement enum that we defined earlier. We store the different keys that we use in a new enum:

import { KeyEvent, KeyEventKind } from "../framework/event"; 
import { State, Movement } from "./model"; 
import { update } from "./utils"; 
 
...