在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:bberak/react-native-game-engine开源软件地址:https://github.com/bberak/react-native-game-engine开源编程语言:JavaScript 100.0%开源软件介绍:React Native Game Engine ·Some components that make it easier to construct dynamic and interactive scenes using React Native. If you are looking for the React (Web) version of this library, go to react-game-engine. Table of Contents
ExamplesTake a look at Studious Bear, a super-polished puzzle game with great visuals and music. One of the first published games to use RNGE. See the React Native Game Engine Handbook for a complimentary app, examples and ideas. Quick StartIf you've used react-native-game-engine before and understand the core concepts, take a look at react-native-game-engine-template. It's a sort of game kickstarter project that allows you to prototype ideas quickly and comes preloaded with a bunch of stuff like:
Otherwise, continue reading the quick start guide below. Firstly, install the package to your project:
Then import the GameEngine component: import { GameEngine } from "react-native-game-engine" Let's code a scene that incorporates some multi-touch logic. To start with, let's create some components that can be rendered by React. Create a file called import React, { PureComponent } from "react";
import { StyleSheet, View } from "react-native";
const RADIUS = 20;
class Finger extends PureComponent {
render() {
const x = this.props.position[0] - RADIUS / 2;
const y = this.props.position[1] - RADIUS / 2;
return (
<View style={[styles.finger, { left: x, top: y }]} />
);
}
}
const styles = StyleSheet.create({
finger: {
borderColor: "#CCC",
borderWidth: 4,
borderRadius: RADIUS * 2,
width: RADIUS * 2,
height: RADIUS * 2,
backgroundColor: "pink",
position: "absolute"
}
});
export { Finger }; Next, let's code our logic in a file called const MoveFinger = (entities, { touches }) => {
//-- I'm choosing to update the game state (entities) directly for the sake of brevity and simplicity.
//-- There's nothing stopping you from treating the game state as immutable and returning a copy..
//-- Example: return { ...entities, t.id: { UPDATED COMPONENTS }};
//-- That said, it's probably worth considering performance implications in either case.
touches.filter(t => t.type === "move").forEach(t => {
let finger = entities[t.id];
if (finger && finger.position) {
finger.position = [
finger.position[0] + t.delta.pageX,
finger.position[1] + t.delta.pageY
];
}
});
return entities;
};
export { MoveFinger }; Finally let's bring it all together in our import React, { PureComponent } from "react";
import { AppRegistry, StyleSheet, StatusBar } from "react-native";
import { GameEngine } from "react-native-game-engine";
import { Finger } from "./renderers";
import { MoveFinger } from "./systems"
export default class BestGameEver extends PureComponent {
constructor() {
super();
}
render() {
return (
<GameEngine
style={styles.container}
systems={[MoveFinger]}
entities={{
1: { position: [40, 200], renderer: <Finger />}, //-- Notice that each entity has a unique id (required)
2: { position: [100, 200], renderer: <Finger />}, //-- and a renderer property (optional). If no renderer
3: { position: [160, 200], renderer: <Finger />}, //-- is supplied with the entity - it won't get displayed.
4: { position: [220, 200], renderer: <Finger />},
5: { position: [280, 200], renderer: <Finger />}
}}>
<StatusBar hidden={true} />
</GameEngine>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#FFF"
}
});
AppRegistry.registerComponent("BestGameEver", () => BestGameEver); Build and run. Each entity is a "finger" and is assigned to a particular touch id. The touch ids increase as you place more fingers on the screen. Move your fingers around the screen to move the entities. As an exercise, try add a system that will insert another finger entity into the game state when a "start" touch event is encountered. What about adding a system that removes the closest entity from the game state when a "long-press" is encountered? If you're curious, our GameEngine Properties
GameEngine Methods
FAQIs React Native Game Engine suitable for production quality games?
Do you know of any apps that currently utilize this library?
How do I manage physics?
Do I have a choice of renderers?
RNGE doesn't give me sensor data out of the box - what gives?
Is this compatible with Android and iOS?
Won't this kind of be harsh on the battery?
IntroductionThis package contains only two components:
Both are standalone components. The The The Game LoopThe game loop is a common pattern in game development and other interactive programs. It loosely consists of two main functions that get called over and over again: The Once the Ideally, both functions complete within 16ms, and we start the next iteration of the loop until some loop-breaking condition is encountered: pause, quit, game over etc. This might seem like a lot of processing overhead, but unlike regular applications, games are highly interactive and ever changing. The game loop affords us full control over scenes - even when no user input or external events have fired. The Game Loop vs React NativeA typical React Native app will only redraw itself when This works perfectly fine (and is even ideal) for a business-oriented app - but it doesn't give the developer fine grained control to create highly interactive and dynamic scenes.
That said, React Native and game loops are not mutually exclusive, and we can use Using the GameLoop ComponentThe Firstly, install the package to your project:
Then import the GameLoop component: import { GameLoop } from "react-native-game-engine" Let's code a basic scene with a single moveable game object. Add this into your import React, { PureComponent } from "react";
import { AppRegistry, StyleSheet, Dimensions, View } from "react-native";
import { GameLoop } from "react-native-game-engine";
const { width: WIDTH, height: HEIGHT } = Dimensions.get("window");
const RADIUS = 25;
export default class BestGameEver extends PureComponent {
constructor() {
super();
this.state = {
x: WIDTH / 2 - RADIUS,
y: HEIGHT / 2 - RADIUS
};
}
updateHandler = ({ touches, screen, layout, time }) => {
let move = touches.find(x => x.type === "move");
if (move) {
this.setState({
x: this.state.x + move.delta.pageX,
y: this.state.y + move.delta.pageY
});
}
};
render() {
return (
<GameLoop style={styles.container} onUpdate={this.updateHandler}>
<View style={[styles.player, { left: this.state.x, top: this.state.y }]} />
</GameLoop>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#FFF"
},
player: {
position: "absolute",
backgroundColor: "pink",
width: RADIUS * 2,
height: RADIUS * 2,
borderRadius: RADIUS * 2
}
});
AppRegistry.registerComponent("BestGameEver", () => BestGameEver); Behind the Scenes
Where is the Draw FunctionNice observation! Indeed, there is none. The logic of our scene is processed in the All we've done here is hookup a timer to a function that fires every ~16ms, and used Managing Complexity with Component Entity SystemsTypically, game developers have used OOP to implement complex game objects and scenes. Each game object is instantiated from a class, and polymorphism allows code re-use and behaviors to be extended through inheritance. As class hierarchies grow, it becomes increasingly difficult to create new types of game entities without duplicating code or seriously re-thinking the entire class hierarchy.
One way to address these problems is to favor composition over inheritance. With this approach, we break out the attributes and behaviours of our various game entities into decoupled, encapsulated and atomic components. This allows us to be really imaginative with the sorts of game entities we create because we can easily compose them with components from disparate domains and co |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论