-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
114 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ node_modules/ | |
# Ruby | ||
/.bundle | ||
.DS_Store | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { World, Action } from "../common/domain.js"; | ||
import { sendAction, requestLogin } from "../common/server.js" | ||
import * as io from "socket.io-client"; | ||
|
||
// hack -- to connect to the game, use this: | ||
var socket = io("http://xp-game.codeselfstudy.com"); | ||
//var socket = io("http://localhost:5000"); | ||
|
||
interface AiConfig { | ||
name: string, | ||
respawnTime: number | ||
} | ||
|
||
export interface AiContext { | ||
act(action: Action): void; | ||
world: World; | ||
} | ||
|
||
/** | ||
* Connect to the socket server, and spawn an ai entity. The ai entity is | ||
* controlled via the `update` function, which is called each tick with the | ||
* most recent game state, `World`. | ||
*/ | ||
export function initialize(cfg: AiConfig, | ||
update: (ctx: AiContext) => void) { | ||
function respawn() { | ||
requestLogin(socket, cfg.name); | ||
} | ||
function act(action: Action){ | ||
sendAction(socket, action); | ||
} | ||
|
||
socket.on('connect', () => respawn()); | ||
socket.on('despawn', () => { | ||
setTimeout(respawn, cfg.respawnTime); | ||
}); | ||
socket.on('world', (world: World) => { | ||
update({act, world}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Direction } from "../common/vectors.js"; | ||
import { ActionKind } from "../common/domain.js"; | ||
import { initialize, AiContext } from "./base.js"; | ||
|
||
/** | ||
* This is a super simple ai implementation that moves randomly | ||
* every so often. | ||
*/ | ||
|
||
function direction(): Direction{ | ||
let index = Math.floor(Math.random() * 4); | ||
let direction: Direction[] = ["North", "East", "West", "South"]; | ||
return direction[index]; | ||
} | ||
|
||
function update(context: AiContext){ | ||
var rand = Math.random(); | ||
var kind: ActionKind = Math.random() >= 0.5 ? "Move" : "Attack"; | ||
if(rand > 0.90){ | ||
context.act({kind: kind, direction: direction()}); | ||
} | ||
} | ||
|
||
initialize({name: "Dopey the Dwarf", respawnTime: 5000}, update); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"dependencies": { | ||
"socket.io-client": "^2.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^12.7.3" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"sourceMap": true, | ||
"target": "es6", | ||
"module": "commonjs", | ||
"outDir": "./build/" | ||
}, | ||
"include": [ | ||
"./client/**/*" | ||
], | ||
"exclude":[ | ||
"node_modules" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters