-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hx
302 lines (259 loc) · 11.6 KB
/
Main.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package;
import sdl.Sdl; // Import SDL library
import hxd.Window; // Import Window module from HaxeD
import hxd.System; // Import System module from HaxeD
import hxd.Event; // Import Event module from HaxeD
import h3d.Vector4; // Import Vector4 class from H3D
import h2d.Text; // Import Text class from H2D
import hxd.Key; // Import Key module from HaxeD
// Define a Point class for 2D coordinates
class Point {
public var x:Float;
public var y:Float;
public function new(x:Float = 0, y:Float = 0) {
this.x = x;
this.y = y;
}
}
// Define a Rect class for a rectangle's position and dimensions
class Rect {
public var x:Float;
public var y:Float;
public var width:Float;
public var height:Float;
public function new(x:Float = 0, y:Float = 0, width:Float = 0, height:Float = 0) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
// Define an enum for different camera modes
enum CameraMode {
FirstPerson; // First-person camera mode
ThirdPerson; // Third-person camera mode
}
// Main class extending HaxeD's App class
class Main extends hxd.App {
var light: h3d.scene.fwd.DirLight; // Directional light object
var player: h3d.scene.Object; // Player object
var floor: h3d.scene.Object; // Floor object
var playerPosition: Point; // Player's current position
var cameraDistance: Float; // Camera distance from player
var cameraHeight: Float; // Camera height from player
var cache: h3d.prim.ModelCache; // Model cache for storing loaded models
var isMoving: Bool = false; // Flag indicating if player is moving
var movementSpeed: Float = 0.0; // Current movement speed of player
var walkSpeed: Float = 0.04; // Base walking speed
var playerSpeed: Float; // Player's speed factor
var worldBounds: Rect; // World boundary rectangle
var walkingAnimation: h3d.anim.Animation; // Walking animation object
var cylinder: h3d.scene.Mesh; // Cylinder mesh for player
var circle: differ.shapes.Circle; // Circle shape for player collision
var obstacles: Array<differ.shapes.Polygon>; // Array of obstacles in the scene
var cameraMode: CameraMode; // Current camera mode (FirstPerson or ThirdPerson)
var cameraModeText: Text; // Text object for displaying camera mode
var lastMouseX: Float = 0.0; // Last recorded mouse X position
var lastMouseY: Float = 0.0; // Last recorded mouse Y position
var mouseSensitivity: Float = 0.002; // Mouse sensitivity for camera control
var yaw: Float = 0.0; // Yaw angle for camera orientation
var pitch: Float = 0.0; // Pitch angle for camera orientation
override function init() {
cameraMode = FirstPerson; // Initialize camera mode to FirstPerson
// Setup directional light
light = new h3d.scene.fwd.DirLight(new h3d.Vector(0.3, -0.4, -0.9), s3d);
light.enableSpecular = true;
light.color.set(0.28, 0.28, 0.28);
// Initialize model cache and load player model
cache = new h3d.prim.ModelCache();
player = cache.loadModel(hxd.Res.Model);
player.scale(1 / 20);
player.rotate(0, 0, Math.PI / 2);
s3d.addChild(player);
playerPosition = new Point(player.x, player.y);
// Load walking animation
walkingAnimation = cache.loadAnimation(hxd.Res.Model);
worldBounds = new Rect(-10, -10, 20, 20);
// Create floor mesh and set its properties
var prim = new h3d.prim.Cube(worldBounds.width, worldBounds.height, 1.0);
prim.unindex();
prim.addNormals();
prim.addUVs();
var tex = hxd.Res.load("hxlogo.png").toImage().toTexture();
var mat = h3d.mat.Material.create(tex);
var floor = new h3d.scene.Mesh(prim, mat, s3d);
floor.material.color.setColor(0xFFB280);
floor.x = worldBounds.x;
floor.y = worldBounds.y;
floor.z = -1;
// Initialize obstacle array with boundary polygons and random cubes
obstacles = [];
obstacles.push(differ.shapes.Polygon.rectangle(worldBounds.x - 1, worldBounds.y - 1, worldBounds.width + 1, 1, false));
obstacles.push(differ.shapes.Polygon.rectangle(worldBounds.x + worldBounds.width, worldBounds.y - 1, 1, worldBounds.height + 1, false));
obstacles.push(differ.shapes.Polygon.rectangle(worldBounds.x, worldBounds.y + worldBounds.height, worldBounds.width + 1, 1, false));
obstacles.push(differ.shapes.Polygon.rectangle(worldBounds.x - 1, worldBounds.y, 1, worldBounds.height + 1, false));
var prim = new h3d.prim.Cylinder(12, 0.35, 0.1);
prim.addNormals();
cylinder = new h3d.scene.Mesh(prim, s3d);
cylinder.material.color.setColor(0x00ff00);
cylinder.material.receiveShadows = false;
cylinder.material.mainPass.culling = None;
circle = new differ.shapes.Circle(0, 0, 0.35);
// Create random cubes as obstacles
var prim = new h3d.prim.Cube(1.0, 1.0, 1.0);
prim.unindex();
prim.addNormals();
prim.addUVs();
for (i in 0...50) {
var cube = new h3d.scene.Mesh(prim, s3d);
cube.material.color.setColor(Std.int(Math.random() * 0xff0000));
cube.material.receiveShadows = false;
cube.material.shadows = false;
var scale = 0.3 + 0.7 * Math.random();
cube.scale(scale);
cube.x = worldBounds.x + Math.random() * (worldBounds.width - scale);
cube.y = worldBounds.y + Math.random() * (worldBounds.height - scale);
obstacles.push(differ.shapes.Polygon.square(cube.x, cube.y, scale, false));
}
// Perform initial collision check
collideWithObstacles();
cameraHeight = 5; // Set initial camera height
updateCamera(); // Update camera position and orientation
// Setup camera mode text display
cameraModeText = new Text(hxd.res.DefaultFont.get(), s2d);
cameraModeText.x = 0;
cameraModeText.y = 0;
cameraModeText.color = new Vector4(255, 255, 255, 255);
cameraModeText.scale(3);
cameraModeText.text = "First Person Camera";
// Initialize mouse position and set cursor position
lastMouseX = s2d.mouseX;
lastMouseY = s2d.mouseY;
Window.getInstance().setCursorPos(s2d.width >> 1, s2d.height >> 1);
}
override function update(dt: Float) {
var currentMouseX = s2d.mouseX;
var currentMouseY = s2d.mouseY;
// Calculate mouse movement delta
var deltaX = (currentMouseX - (s2d.width >> 1)) * mouseSensitivity;
var deltaY = (currentMouseY - (s2d.height >> 1)) * mouseSensitivity;
// Update camera orientation based on camera mode
if (cameraMode == FirstPerson) {
yaw += deltaX;
pitch -= deltaY;
pitch = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, pitch));
}
else if (cameraMode == ThirdPerson) {
yaw += deltaX;
pitch = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, pitch));
}
lastMouseX = s2d.width >> 1;
lastMouseY = s2d.height >> 1;
// Determine player speed based on key input
playerSpeed = 0.0;
if (hxd.Key.isDown(hxd.Key.W)) {
playerSpeed += 1;
}
if (hxd.Key.isDown(hxd.Key.S)) {
playerSpeed -= 1;
}
// Adjust movement speed for running
var runningMultiplicator = 1.0;
if (hxd.Key.isDown(hxd.Key.SHIFT)) {
if (playerSpeed < 0) {
runningMultiplicator = 1.3;
} else {
runningMultiplicator = 2;
}
}
// Move player based on current speed and direction
if (playerSpeed != 0) {
var forwardX = Math.cos(yaw) * playerSpeed * walkSpeed;
var forwardY = Math.sin(yaw) * playerSpeed * walkSpeed;
playerPosition.x += forwardX;
playerPosition.y += forwardY;
collideWithObstacles();
// Play walking animation if moving
if (movementSpeed != playerSpeed) {
if (playerSpeed > 0) {
player.playAnimation(walkingAnimation).speed = runningMultiplicator;
}
movementSpeed = playerSpeed;
}
isMoving = true;
} else {
// Stop animation and reset movement state if not moving
if (isMoving) {
player.stopAnimation();
}
movementSpeed = 0;
isMoving = false;
}
// Update player and camera orientation
player.setRotation(0, 0, yaw + Math.PI / 2);
updateCamera();
// Reset mouse position and set relative mouse mode
Window.getInstance().setCursorPos(s2d.width >> 1, s2d.height >> 1);
Sdl.setRelativeMouseMode(true);
// Switch camera mode based on key input
if (Key.isPressed(Key.NUMBER_1)) {
cameraMode = FirstPerson;
cameraModeText.text = "First Person Camera";
}
else if (Key.isPressed(Key.NUMBER_2)) {
cameraMode = ThirdPerson;
cameraModeText.text = "Third Person Camera";
}
// Exit application if ESCAPE key is pressed
if (Key.isPressed(Key.ESCAPE)) {
System.exit();
}
}
// Function to handle collision detection with obstacles
function collideWithObstacles() {
circle.x = playerPosition.x;
circle.y = playerPosition.y;
for (i in 0...obstacles.length) {
var obstacle = obstacles[i];
var collideInfo = differ.Collision.shapeWithShape(circle, obstacle);
if (collideInfo != null) {
circle.x += collideInfo.separationX;
circle.y += collideInfo.separationY;
}
}
playerPosition.x = circle.x;
playerPosition.y = circle.y;
player.x = playerPosition.x;
player.y = playerPosition.y;
cylinder.x = playerPosition.x;
cylinder.y = playerPosition.y;
}
// Function to update camera position and orientation based on camera mode
function updateCamera() {
if (cameraMode == FirstPerson) {
cameraHeight = 1.5;
var forwardX = Math.cos(yaw) * Math.cos(pitch);
var forwardY = Math.sin(yaw) * Math.cos(pitch);
var forwardZ = Math.sin(pitch);
s3d.camera.pos.set(playerPosition.x, playerPosition.y, cameraHeight);
s3d.camera.target.set(playerPosition.x + forwardX, playerPosition.y + forwardY, cameraHeight + forwardZ);
}
else if (cameraMode == ThirdPerson) {
cameraHeight = 1; // Adjust this height as needed
var orbitDistance = 4.5; // Adjust this distance as needed
// Adjust the pitch to look down at the player at an angle
var desiredPitch = 0.2; // Adjust the angle (in radians) to look down
pitch = Math.max(desiredPitch, Math.min(Math.PI / 2, pitch)); // Clamp the pitch
var orbitX = orbitDistance * Math.cos(yaw) * Math.cos(pitch);
var orbitY = orbitDistance * Math.sin(yaw) * Math.cos(pitch);
var orbitZ = orbitDistance * Math.sin(pitch);
s3d.camera.pos.set(playerPosition.x - orbitX, playerPosition.y - orbitY, cameraHeight + orbitZ);
s3d.camera.target.set(playerPosition.x, playerPosition.y, cameraHeight);
}
}
// Main entry point of the application
static function main() {
hxd.Res.initEmbed(); // Initialize embedded resources
new Main(); // Create an instance of Main
}
}