react-native-game-engine-ex.../game/entities/Wall.tsx

46 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2022-06-25 00:29:43 -05:00
import Matter from 'matter-js'
import React from 'react'
import { ColorValue, View } from 'react-native'
import { GameEntity, Position2D, Size2D } from '@types'
2022-06-25 00:29:43 -05:00
const Wall = ({ body, color }: GameEntity) => {
2022-06-25 00:29:43 -05:00
const widthBody = body.bounds.max.x - body.bounds.min.x
const heightBody = body.bounds.max.y - body.bounds.min.y
const xBody = body.position.x - widthBody / 2
const yBody = body.position.y - heightBody / 2
return (
<View style={{
backgroundColor: color,
position: 'absolute',
left: xBody,
top: yBody,
width: widthBody,
height: heightBody
}} />
)
}
2022-06-26 21:44:24 -05:00
export default (label: string, world: Matter.Composite, color: ColorValue, pos: Position2D, size: Size2D) => {
2022-06-25 00:29:43 -05:00
const body = Matter.Bodies.rectangle(
pos.x,
pos.y,
size.width,
size.height,
{
2022-06-26 21:44:24 -05:00
label,
2022-06-25 00:29:43 -05:00
isStatic: true
} as Matter.IChamferableBodyDefinition
)
Matter.Composite.add(world, body)
return {
body,
color,
pos,
renderer: Wall
}
}