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

Commit

Permalink
phew
Browse files Browse the repository at this point in the history
  • Loading branch information
kenwheeler committed Sep 5, 2016
1 parent a030c3c commit 151cdfe
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
27 changes: 19 additions & 8 deletions demo/character.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ export default class Character extends Component {
const { keys, store } = this.props;
const { body } = this.body;

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

const shouldMoveStageLeft = body.position.x < midPoint && store.stageX < 0;
const shouldMoveStageRight = body.position.x > midPoint && store.stageX > -1024;

if (body.velocity.y === 0) {
this.isJumping = false;
Expand All @@ -62,18 +64,18 @@ export default class Character extends Component {
if (keys.isDown(keys.LEFT) || gamepad.button(0, 'button 14')) {
if (shouldMoveStageLeft) {
store.setStageX(store.stageX + 5);
} else {
this.move(body, -5);
}

this.move(body, -5);

characterState = 1;
} else if (keys.isDown(keys.RIGHT) || gamepad.button(0, 'button 15')) {
if (shouldMoveStageRight) {
store.setStageX(store.stageX - 5);
} else {
this.move(body, 5);
}

this.move(body, 5);

characterState = 0;
}

Expand All @@ -82,14 +84,22 @@ export default class Character extends Component {
this.setState({
characterState,
});
} else {
const targetX = store.stageX + (this.lastX - body.position.x);
if (shouldMoveStageLeft || shouldMoveStageRight) {
store.setStageX(targetX);
}
}

this.lastX = body.position.x;
};

constructor(props) {
super(props);

this.loopID = null;
this.isJumping = false;
this.lastX = 0;

this.state = {
characterState: 2,
Expand All @@ -105,13 +115,14 @@ export default class Character extends Component {
}

getWrapperStyles() {
const { characterPosition } = this.props.store;
const { characterPosition, stageX } = this.props.store;
const { scale } = this.context;
const { x, y } = characterPosition;
const targetX = x + stageX;

return {
position: 'absolute',
transform: `translate(${x * scale}px, ${y * scale}px)`,
transform: `translate(${targetX * scale}px, ${y * scale}px)`,
transformOrigin: 'left top',
};
}
Expand Down
4 changes: 2 additions & 2 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export default class Demo extends Component {
);

const leftWall = Matter.Bodies.rectangle(
-96, 288,
-64, 288,
64, 576,
{
isStatic: true,
},
);

const rightWall = Matter.Bodies.rectangle(
992, 288,
1984, 288,
64, 576,
{
isStatic: true,
Expand Down
2 changes: 1 addition & 1 deletion demo/level.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class Level extends Component {
getWrapperStyles() {
return {
position: 'absolute',
transform: `translate(${this.state.stageX}px, 0px)`,
transform: `translate(${this.state.stageX}px, 0px) translateZ(0)`,
transformOrigin: 'top left',
};
}
Expand Down
8 changes: 7 additions & 1 deletion demo/stores/game-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ class GameStore {
}

setStageX(x) {
this.stageX = x;
if (x > 0) {
this.stageX = 0;
} else if (x < -1024) {
this.stageX = -1024;
} else {
this.stageX = x;
}
}
}

Expand Down

0 comments on commit 151cdfe

Please sign in to comment.