Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 151cdfe

Browse files
committed
phew
1 parent a030c3c commit 151cdfe

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

demo/character.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ export default class Character extends Component {
4141
const { keys, store } = this.props;
4242
const { body } = this.body;
4343

44-
const shouldMoveStageLeft = body.position.x < 448 && store.stageX < 0;
45-
const shouldMoveStageRight = body.position.x > 448 && store.stageX > -1024;
44+
const midPoint = Math.abs(store.stageX) + 448;
45+
46+
const shouldMoveStageLeft = body.position.x < midPoint && store.stageX < 0;
47+
const shouldMoveStageRight = body.position.x > midPoint && store.stageX > -1024;
4648

4749
if (body.velocity.y === 0) {
4850
this.isJumping = false;
@@ -62,18 +64,18 @@ export default class Character extends Component {
6264
if (keys.isDown(keys.LEFT) || gamepad.button(0, 'button 14')) {
6365
if (shouldMoveStageLeft) {
6466
store.setStageX(store.stageX + 5);
65-
} else {
66-
this.move(body, -5);
6767
}
6868

69+
this.move(body, -5);
70+
6971
characterState = 1;
7072
} else if (keys.isDown(keys.RIGHT) || gamepad.button(0, 'button 15')) {
7173
if (shouldMoveStageRight) {
7274
store.setStageX(store.stageX - 5);
73-
} else {
74-
this.move(body, 5);
7575
}
7676

77+
this.move(body, 5);
78+
7779
characterState = 0;
7880
}
7981

@@ -82,14 +84,22 @@ export default class Character extends Component {
8284
this.setState({
8385
characterState,
8486
});
87+
} else {
88+
const targetX = store.stageX + (this.lastX - body.position.x);
89+
if (shouldMoveStageLeft || shouldMoveStageRight) {
90+
store.setStageX(targetX);
91+
}
8592
}
93+
94+
this.lastX = body.position.x;
8695
};
8796

8897
constructor(props) {
8998
super(props);
9099

91100
this.loopID = null;
92101
this.isJumping = false;
102+
this.lastX = 0;
93103

94104
this.state = {
95105
characterState: 2,
@@ -105,13 +115,14 @@ export default class Character extends Component {
105115
}
106116

107117
getWrapperStyles() {
108-
const { characterPosition } = this.props.store;
118+
const { characterPosition, stageX } = this.props.store;
109119
const { scale } = this.context;
110120
const { x, y } = characterPosition;
121+
const targetX = x + stageX;
111122

112123
return {
113124
position: 'absolute',
114-
transform: `translate(${x * scale}px, ${y * scale}px)`,
125+
transform: `translate(${targetX * scale}px, ${y * scale}px)`,
115126
transformOrigin: 'left top',
116127
};
117128
}

demo/demo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ export default class Demo extends Component {
2727
);
2828

2929
const leftWall = Matter.Bodies.rectangle(
30-
-96, 288,
30+
-64, 288,
3131
64, 576,
3232
{
3333
isStatic: true,
3434
},
3535
);
3636

3737
const rightWall = Matter.Bodies.rectangle(
38-
992, 288,
38+
1984, 288,
3939
64, 576,
4040
{
4141
isStatic: true,

demo/level.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default class Level extends Component {
3737
getWrapperStyles() {
3838
return {
3939
position: 'absolute',
40-
transform: `translate(${this.state.stageX}px, 0px)`,
40+
transform: `translate(${this.state.stageX}px, 0px) translateZ(0)`,
4141
transformOrigin: 'top left',
4242
};
4343
}

demo/stores/game-store.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ class GameStore {
1010
}
1111

1212
setStageX(x) {
13-
this.stageX = x;
13+
if (x > 0) {
14+
this.stageX = 0;
15+
} else if (x < -1024) {
16+
this.stageX = -1024;
17+
} else {
18+
this.stageX = x;
19+
}
1420
}
1521
}
1622

0 commit comments

Comments
 (0)