Created
April 9, 2018 00:02
-
-
Save bberak/89adaab0b0dbc612203b498f2d394823 to your computer and use it in GitHub Desktop.
React Native Donkey Kong - Basic Mario
This file contains hidden or 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
import React, { Component } from "react"; | |
import { StyleSheet, Image } from "react-native"; | |
import { collisionCategories } from "./constants"; | |
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource'; | |
import Matter from "matter-js"; | |
import MarioIdling from "./mario-idling.gif"; | |
export class Renderer extends Component { | |
render() { | |
const source = this.props.actions[this.props.action]; | |
const { width, height } = source; | |
const body = this.props.body; | |
const x = body.position.x - width / 2; | |
const y = body.position.y - height / 2; | |
const angle = body.angle; | |
const direction = this.props.direction.horizontal; | |
return ( | |
<Image | |
source={source} | |
style={[ | |
styles.mario, | |
{ | |
left: x, | |
top: y, | |
transform: [ | |
{ rotateZ: angle + "rad" }, | |
{ rotateY: (direction === "right" ? 180 : 0) + "deg" } | |
] | |
} | |
]} | |
/> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
mario: { | |
position: "absolute" | |
} | |
}); | |
export default (world, pos) => { | |
let width = 30; | |
let height = 40; | |
let body = Matter.Bodies.rectangle(pos.x, pos.y, width, height, { | |
density: 0.8, | |
frictionAir: 0.2, | |
friction: 1, | |
collisionFilter: { | |
category: collisionCategories.mario, | |
mask: | |
collisionCategories.barrier | | |
collisionCategories.platform | | |
collisionCategories.barrel | |
} | |
}); | |
Matter.World.add(world, [body]); | |
return { | |
body, | |
size: { width, height }, | |
controls: { | |
gestures: {}, | |
mode: "platform" | |
}, | |
direction: { | |
horizontal: "right", | |
vertical: "up" | |
}, | |
action: "idling", | |
actions: { | |
idling: resolveAssetSource(MarioIdling) | |
}, | |
"power-ups": {}, | |
animations: {}, | |
renderer: <Renderer /> | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment