CarChallenge Demo
import QtQuick 2.0
import Felgo 4.0
EntityBase {
id: car
entityType: "car"
property alias inputActionsToKeyCode: twoAxisController.inputActionsToKeyCode
property alias image: image
property alias controller: twoAxisController
readonly property real forwardForce: 8000 * world.pixelsPerMeter
Component.onCompleted: {
console.debug("car.onCompleted()")
console.debug("car.x:", x)
var mapped = mapToItem(world.debugDraw, x, y)
console.debug("car.x world:", mapped.x)
}
Image {
id: image
source: Qt.resolvedUrl("../../assets/img/car_red.png")
anchors.centerIn: parent
width: boxCollider.width
height: boxCollider.height
property list<Item> imagePoints: [
Item {x: image.width/2+30}
]
}
TwoAxisController {
id: twoAxisController
onInputActionPressed: actionName => handleInputAction(actionName)
}
BoxCollider {
id: boxCollider
width: 60
height: 40
anchors.centerIn: parent
density: 0.02
friction: 0.4
restitution: 0.5
body.bullet: true
body.linearDamping: 10
body.angularDamping: 15
force: Qt.point(twoAxisController.yAxis*forwardForce, 0)
torque: twoAxisController.xAxis*2000 * world.pixelsPerMeter * world.pixelsPerMeter
Component.onCompleted: {
console.debug("car.physics.x:", x)
var mapped = mapToItem(world.debugDraw, x, y)
console.debug("car.physics.x world:", mapped.x)
}
fixture.onBeginContact: (other, contactNormal) => {
var fixture = other
var body = other.getBody()
var component = other.getBody().target
var collidingType = component.entityType
console.debug("car contact with: ", other, body, component)
console.debug("car collided entity type:", collidingType)
console.debug("car contactNormal:", contactNormal, "x:", contactNormal.x, "y:", contactNormal.y)
}
}
function handleInputAction(action) {
if( action === "fire") {
console.debug("creating weapon at current position x", car.x, "y", car.y)
console.debug("image.imagePoints[0].x:", image.imagePoints[0].x, ", image.imagePoints[0].y:", image.imagePoints[0].y)
var imagePointInWorldCoordinates = mapToItem(level,image.imagePoints[0].x, image.imagePoints[0].y)
console.debug("imagePointInWorldCoordinates x", imagePointInWorldCoordinates.x, " y:", imagePointInWorldCoordinates.y)
entityManager.createEntityFromUrlWithProperties(Qt.resolvedUrl("Rocket.qml"), {"x": imagePointInWorldCoordinates.x, "y": imagePointInWorldCoordinates.y, "rotation": car.rotation})