Skip to content

Commit

Permalink
converted to typescript, published to npm
Browse files Browse the repository at this point in the history
  • Loading branch information
cdleveille committed Jul 12, 2022
1 parent 986608d commit e8225c8
Show file tree
Hide file tree
Showing 25 changed files with 1,322 additions and 3,110 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = crlf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
indent_style = tab
indent_size = 4

# Matches the exact files either *.json or *.yml
[*.{json,yml}]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
28 changes: 28 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// https://eslint.org/docs/user-guide/configuring/

{
"env": {
"browser": true,
"es2021": true,
"node": true,
"commonjs": true,
"shared-node-browser": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {
"quotes": ["warn", "double"],
"semi": ["warn", "always"],
"no-unused-vars": ["warn", { "args": "none" }],
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-empty-function": "warn"
}
}
46 changes: 46 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
###############################
# Git Line Endings #
###############################

# Set default behaviour to automatically normalize line endings.
* text=auto eol=crlf

# Force batch scripts to always use CRLF line endings so that if a repo is accessed
# in Windows via a file share from Linux, the scripts will work.
*.{cmd,[cC][mM][dD]} text eol=crlf
*.{bat,[bB][aA][tT]} text eol=crlf

# Force bash scripts to always use LF line endings so that if a repo is accessed
# in Unix via a file share from Windows, the scripts will work.
*.sh text eol=lf

###############################
# Git Large File System (LFS) #
###############################

# Archives
*.7z filter=lfs diff=lfs merge=lfs -text
*.br filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
*.tar filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text

# Documents
*.pdf filter=lfs diff=lfs merge=lfs -text

# Images
*.gif filter=lfs diff=lfs merge=lfs -text
*.ico filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text
*.jpeg filter=lfs diff=lfs merge=lfs -text
*.pdf filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.psd filter=lfs diff=lfs merge=lfs -text
*.webp filter=lfs diff=lfs merge=lfs -text

# Fonts
*.woff2 filter=lfs diff=lfs merge=lfs -text
*.ttf filter=lfs diff=lfs merge=lfs -text

# Other
*.exe filter=lfs diff=lfs merge=lfs -text
28 changes: 28 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: publish

on:
release:
types: [published]

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: use node.js
uses: actions/setup-node@v2
with:
node-version: 14
cache: yarn

- run: yarn install --frozen-lockfile

- run: yarn format

- run: yarn build

- run: |
echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_ACCESS_TOKEN }}" > .npmrc
npm pack
npm publish
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.vscode
node_modules
node_modules
lib
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn lint
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!lib/**
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16.6.2
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 100,
"semi": true,
"singleQuote": false,
"trailingComma": "none"
}
8 changes: 8 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"dbaeumer.vscode-eslint",
"editorconfig.editorconfig",
"esbenp.prettier-vscode",
"mike-co.import-sorter"
]
}
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.detectIndentation": false,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.insertSpaces": false,
"editor.tabSize": 4,
"eslint.validate": ["javascript", "typescript"]
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Chris Leveille
Copyright (c) 2022 Chris Leveille

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
59 changes: 40 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,60 @@
The ```GamepadHelper``` class in ```gamepadhelper.js``` can be copied into any project, imported as a module into existing code, instantiated, and then used by calling the ```GamepadHelper.update()``` method within the game loop (see ```game.js``` for a simple example). Supports up to 4 gamepads connected simultaneously.
```bash
npm install gamepad-helper
or
yarn add gamepad-helper
```

The `GamepadHelper` class imported as a module into existing code and used by calling the static `GamepadHelper.update()` method within the game loop. Supports up to 4 gamepads connected simultaneously.

```javascript
import GamepadHelper from "gamepad-helper";

When a button on a connected gamepad is pressed or released, a custom event will be dispatched to the document containing the respective indexes of the gamepad and button that was pressed/released in the ```detail``` property of the event.
// call inside game loop
GamepadHelper.update();
```

When a button on a connected gamepad is pressed or released, a custom event will be dispatched to the document containing a `detail` property identifying the gamepad/button that was pressed/released.

There are two custom event types:
- ```gamepadbuttondown``` - dispatched when the ```value``` property of any button changes from 0 to 1
- ```gamepadbuttonup``` - dispatched when the ```value``` property of any button changes from 1 to 0

Here is an example event listener that simply logs the gamepad/button that was pressed down:
- `gamepadbuttondown` - dispatched when the `value` property of any button changes from `0` to `1`
- `gamepadbuttonup` - dispatched when the `value` property of any button changes from `1` to `0`

Here is an example that simply logs the gamepad/button that was pressed down:

```javascript
document.addEventListener('gamepadbuttondown', (event) => {
console.log('button ' + event.detail.button + ' on gamepad ' + event.detail.gamepad + ' was pressed down');
document.addEventListener("gamepadbuttondown", (event) => {
console.log(
"button " +
event.detail.button +
" on gamepad " +
event.detail.gamepad +
" was pressed down"
);
});
```

Here is a code sample which causes player 1 to jump if button 0 on gamepad 0 is released:
Here is an example which causes player 1 to jump if button 0 on gamepad 0 is released:

```javascript
document.addEventListener('gamepadbuttonup', (event) => {
if (event.detail.gamepad == 0 && event.detail.button == 0) {
player1.jump();
}
document.addEventListener("gamepadbuttonup", (event) => {
if (event.detail.gamepad == 0 && event.detail.button == 0) {
player1.jump();
}
});
```

The current state of any button or axis on a connected gamepad can also be queried via the ```getButtonValue(gamepad, button)``` and ```getAxisValue(gamepad, axis)``` methods.
The instantaneous state of any button or axis on a connected gamepad can also be queried via the `getButtonValue()` and `getAxisValue()` methods.

The ```mode``` property of an instance of the ```GamepadHelper``` class can be set 'default' to 'xbox' or 'playstation' to change the button labels if desired:
The `mode` property of an instance of the `GamepadHelper` class can be set to `default` (numeric labels), `xbox`, or `playstation` to change the button labels if desired:

```javascript
let gamepadhelper = new GamepadHelper();
gamepadhelper.mode = 'xbox';
gamepadhelper.mode = 'playstation';
GamepadHelper.setMode("default");
GamepadHelper.setMode("xbox");
GamepadHelper.setMode("playstation");
```

More info on the Gamepad API:
- https://www.w3.org/TR/gamepad/
- https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API

- https://www.w3.org/TR/gamepad/
- https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API
23 changes: 0 additions & 23 deletions game.js

This file was deleted.

Loading

0 comments on commit e8225c8

Please sign in to comment.