Let's delve into a more structured and comprehensive implementation of a simple browser-based game inspired by Karateka using TypeScript. We'll focus on essential mechanics like movement, combat, and basic enemy AI. The goal is to give you a solid foundation to build upon.
Project Setup
We'll begin with a basic setup that includes core game components.
Directory Structure:
karateka-clone/
│
├── src/
│ ├── index.ts
│ ├── game.ts
│ ├── character.ts
│ ├── player.ts
│ ├── enemy.ts
│ └── inputHandler.ts
├── index.html
└── tsconfig.json
Configuration:
tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"]
}
HTML Structure:
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Karateka Clone</title>
<style>
body, html { margin: 0; height: 100%; }
canvas { background-color: black; display: block; margin: 0 auto; }
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="400"></canvas>
<script src="dist/index.js"></script>
</body>
</html>
TypeScript Code
index.ts
This is the entry point where we initialize the game and render the canvas.
import Game from './game';
window.onload = () => {
const canvas = document.getElementById('gameCanvas') as HTMLCanvasElement;
if (canvas) {
const context = canvas.getContext('2d');
if (context) {
const game = new Game(context);
game.start();
}
}
};
game.ts
Manage the game loop and coordinate character interactions.
import Player from './player';
import Enemy from './enemy';
import InputHandler from './inputHandler';
class Game {
private context: CanvasRenderingContext2D;
private player: Player;
private enemy: Enemy;
private inputHandler: InputHandler;
private lastUpdateTime = 0;
constructor(context: CanvasRenderingContext2D) {
this.context = context;
this.player = new Player(100, 320, 'white');
this.enemy = new Enemy(600, 320, 'red');
this.inputHandler = new InputHandler();
}
public start() {
requestAnimationFrame(this.gameLoop.bind(this));
}
private gameLoop(timestamp: number) {
const deltaTime = timestamp - this.lastUpdateTime;
this.lastUpdateTime = timestamp;
this.update(deltaTime);
this.render();
requestAnimationFrame(this.gameLoop.bind(this));
}
private update(deltaTime: number) {
this.player.update(deltaTime, this.inputHandler);
this.enemy.update(deltaTime, this.player);
if (this.collides(this.player, this.enemy)) {
console.log('Collision Detected');
}
}
private collides(a: Player, b: Enemy): boolean {
return a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y;
}
private render() {
this.context.clearRect(0, 0, this.context.canvas.width, this.context.canvas.height);
this.player.draw(this.context);
this.enemy.draw(this.context);
}
}
export default Game;
player.ts
Handle player-specific logic.
import Character from './character';
import InputHandler from './inputHandler';
class Player extends Character {
public update(deltaTime: number, inputHandler: InputHandler) {
if (inputHandler.isKeyPressed('ArrowLeft')) {
this.move(-1, deltaTime);
} else if (inputHandler.isKeyPressed('ArrowRight')) {
this.move(1, deltaTime);
}
if (inputHandler.isKeyPressed(' ')) {
this.attack();
}
}
}
export default Player;
enemy.ts
Define basic enemy behavior.
import Character from './character';
class Enemy extends Character {
public update(deltaTime: number, player: Character) {
if (player.x < this.x) {
this.move(-1, deltaTime);
} else if (player.x > this.x) {
this.move(1, deltaTime);
}
}
}
export default Enemy;
character.ts
Base class for characters in the game.
class Character {
public x: number;
public y: number;
public width: number = 40;
public height: number = 40;
private color: string;
private speed: number = 0.1;
constructor(x: number, y: number, color: string) {
this.x = x;
this.y = y;
this.color = color;
}
protected move(direction: number, deltaTime: number) {
this.x += direction * this.speed * deltaTime;
this.x = Math.max(0, Math.min(this.x, 760)); // Keep within canvas bounds
}
public update(_deltaTime: number) {
// Shared update logic if needed
}
public draw(context: CanvasRenderingContext2D) {
context.fillStyle = this.color;
context.fillRect(this.x, this.y, this.width, this.height);
}
public attack() {
console.log('Attack action');
}
}
export default Character;
inputHandler.ts
Handle keyboard input for player controls.
class InputHandler {
private keys: Set<string> = new Set();
constructor() {
window.addEventListener('keydown', (e) => this.keys.add(e.key));
window.addEventListener('keyup', (e) => this.keys.delete(e.key));
}
public isKeyPressed(key: string): boolean {
return this.keys.has(key);
}
}
export default InputHandler;
Considerations and Further Development
- Graphics and Animations: Replace simple rectangles with sprite animations for a more authentic Karateka feel.
- Audio: Implement sound effects and music using the Web Audio API or a library like Howler.js.
- Combat Mechanics: Enhance the attack mechanics to have blocking and health systems.
- AI Improvements: Develop more sophisticated enemy AI to provide challenging gameplay.
This implementation provides a starting framework for a browser-based game that mirrors some key mechanics of Karateka. You can expand upon it with more detailed features as you refine the game design.