Skip to content

Commit 305d79d

Browse files
feat: initial release (#1)
Co-authored-by: pauldps <github@pauloddr.com>
1 parent 8ecea4a commit 305d79d

File tree

19 files changed

+14747
-3
lines changed

19 files changed

+14747
-3
lines changed

.github/renovate.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": [
3+
"github>dimensionalpocket/development-js//renovate/default",
4+
"github>dimensionalpocket/development-js//renovate/automerge"
5+
]
6+
}

.github/workflows/node.js.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: Node.js
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: main
7+
8+
jobs:
9+
default:
10+
uses: dimensionalpocket/development-js/.github/workflows/default-node-build.yml@0.6.1

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 Dimensional Pocket
3+
Copyright (c) 2022 dimensionalpocket.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 191 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,191 @@
1-
# aa-line-segment-js
2-
Axis-Aligned Line Segment library for Node apps
1+
# Axis-Aligned Line Segment
2+
3+
[![build](https://github.com/dimensionalpocket/aa-line-segment-js/actions/workflows/node.js.yml/badge.svg)](https://github.com/dimensionalpocket/aa-line-segment-js/actions/workflows/node.js.yml) [![Total alerts](https://img.shields.io/lgtm/alerts/g/dimensionalpocket/aa-line-segment-js.svg)](https://lgtm.com/projects/g/dimensionalpocket/aa-line-segment-js/alerts/) [![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/dimensionalpocket/aa-line-segment-js.svg)](https://lgtm.com/projects/g/dimensionalpocket/aa-line-segment-js/context:javascript)
4+
5+
A Node library for line segment manipulations. Features include:
6+
7+
* positioning,
8+
* nesting (add segments inside segments),
9+
* flipping around its position,
10+
* global positioning, and
11+
* collision detection.
12+
13+
It is tailored for usage in games that require a box management system, for handling things such as scrolling stages, hitboxes/hurtboxes, etc.
14+
15+
## Usage
16+
17+
```js
18+
var segment = new AALineSegment(-1, 5)
19+
20+
/*
21+
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
22+
├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤
23+
A──P──────────────B
24+
25+
Position starts at 0
26+
*/
27+
28+
segment.a // <= -1
29+
segment.b // <= 5
30+
```
31+
32+
### Positioning
33+
34+
The segment can be moved along the axis:
35+
36+
```js
37+
segment.position = 2
38+
39+
/*
40+
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
41+
├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤
42+
A──P──────────────B
43+
44+
New position
45+
*/
46+
47+
segment.a // <= 1
48+
segment.b // <= 7
49+
```
50+
51+
### Flipping
52+
53+
Segments can be flipped around their positions:
54+
55+
```js
56+
segment.flip(true)
57+
58+
/*
59+
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
60+
├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤
61+
A──────────────P──B
62+
*/
63+
64+
segment.a // <= -3
65+
segment.b // <= 3
66+
67+
segment.flip(false) // unflip
68+
```
69+
70+
### Nested Segments
71+
72+
Segments can be added to other segments, inheriting their positions and flip states.
73+
74+
```js
75+
var child = new AALineSegment(1, 2)
76+
77+
/*
78+
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
79+
├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤
80+
A──P──────────────B
81+
82+
P A──B
83+
84+
Child without parent, position 0
85+
86+
*/
87+
88+
child.a // <= 1
89+
child.b // <= 2
90+
```
91+
92+
```js
93+
segment.add(child)
94+
95+
/*
96+
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
97+
├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤
98+
A──P──────────────B
99+
P A──B
100+
101+
Child with parent, inherits position
102+
*/
103+
104+
child.a // <= 3
105+
child.b // <= 4
106+
```
107+
108+
```js
109+
child.flip(true)
110+
111+
/*
112+
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
113+
├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤
114+
A──P──────────────B
115+
A──B P
116+
117+
Flipped child
118+
*/
119+
120+
child.a // <= 0
121+
child.b // <= 1
122+
123+
child.flip(false) // unflip
124+
```
125+
126+
Flipping the parent also flips its children:
127+
128+
```js
129+
segment.flip(true)
130+
131+
/*
132+
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
133+
├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤
134+
A──────────────P──B
135+
A──B P
136+
*/
137+
138+
child.a // <= 0
139+
child.b // <= 1
140+
```
141+
142+
If any child is already flipped, their global flip state is toggled:
143+
144+
```js
145+
child.flip(true)
146+
147+
/*
148+
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
149+
├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤
150+
A──────────────P──B
151+
P A──B
152+
153+
Child is flipped,
154+
but appears unflipped
155+
because parent is flipped
156+
*/
157+
158+
child.a // <= 3
159+
child.b // <= 4
160+
```
161+
162+
### Collision Detection
163+
164+
Two segments collide if they touch or intersect with each other.
165+
166+
```js
167+
var s1 = new AALineSegment(-1, 0)
168+
var s2 = new AALineSegment(0, 1)
169+
/*
170+
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
171+
├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤
172+
A──B (s1)
173+
A──B (s2)
174+
*/
175+
176+
s1.collidesWith(s2) // <= true
177+
178+
s2.position = 1
179+
/*
180+
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
181+
├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤
182+
A──B (s1)
183+
A──B (s2, repositioned)
184+
*/
185+
186+
s1.collidesWith(s2) // <= false
187+
```
188+
189+
## License
190+
191+
MIT

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
import { AALineSegment } from './src/AALineSegment.js'
4+
5+
export default AALineSegment

jsconfig.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"checkJs": true,
4+
"strict": true,
5+
"strictNullChecks": true,
6+
"typeRoots": ["./node_modules/@types"],
7+
"target": "ES5",
8+
"lib": [
9+
"dom",
10+
"es5",
11+
"es2015.collection",
12+
"es2015.iterable"
13+
],
14+
"downlevelIteration": true
15+
},
16+
"include": [
17+
"src/**/*.js",
18+
"test/**/*.js"
19+
]
20+
}

0 commit comments

Comments
 (0)