-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
202 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { io } from 'socket.io-client' | ||
|
||
const socket = io('http://localhost:5000') | ||
|
||
socket.on('connect', () => { | ||
console.log('Connected to server') | ||
|
||
// Example player action | ||
socket.emit('playerAction', { entityId: 'exampleEntityId', newPosition: { x: 10, y: 20 } }) | ||
|
||
// Listen for game state updates | ||
socket.on('gameState', (state) => { | ||
console.log('Updated game state:', state) | ||
}) | ||
}) | ||
|
||
socket.on('disconnect', () => { | ||
console.log('Disconnected from server') | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,44 @@ | ||
import { Server } from 'socket.io' | ||
import Entity from './models/Entity.js' | ||
import Feature from './models/Feature.js' | ||
|
||
const setupGameLogic = (server) => { | ||
const io = new Server(server) | ||
|
||
const simulateWorld = async () => { | ||
// Example: Move entities, update features | ||
const entities = await Entity.find() | ||
for (const entity of entities) { | ||
// Example logic to update entity state | ||
entity.attributes.energy -= 1 | ||
await entity.save() | ||
} | ||
setTimeout(simulateWorld, 1000) // Simulate every second | ||
} | ||
|
||
io.on('connection', (socket) => { | ||
console.log('New player connected') | ||
|
||
// Handle player actions | ||
socket.on('playerAction', (action) => { | ||
// Process player action | ||
console.log('Player action:', action) | ||
socket.on('playerAction', async (action) => { | ||
const entity = await Entity.findById(action.entityId) | ||
if (entity) { | ||
// Process player action (example: move entity) | ||
entity.position = action.newPosition | ||
await entity.save() | ||
|
||
// Broadcast updated game state | ||
io.emit('gameState', { /* updated state */ }) | ||
// Broadcast updated game state | ||
io.emit('gameState', { entities: await Entity.find() }) | ||
} | ||
}) | ||
|
||
socket.on('disconnect', () => { | ||
console.log('Player disconnected') | ||
}) | ||
}) | ||
|
||
// Start the world simulation | ||
simulateWorld() | ||
} | ||
|
||
export default setupGameLogic |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import mongoose from 'mongoose' | ||
|
||
const entitySchema = new mongoose.Schema({ | ||
type: { type: String, required: true }, | ||
position: { x: Number, y: Number }, | ||
attributes: { type: Object, default: {} }, | ||
}) | ||
|
||
const Entity = mongoose.model('Entity', entitySchema) | ||
|
||
export default Entity |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import mongoose from 'mongoose' | ||
|
||
const featureSchema = new mongoose.Schema({ | ||
type: { type: String, required: true }, | ||
position: { x: Number, y: Number }, | ||
details: { type: Object, default: {} }, | ||
}) | ||
|
||
const Feature = mongoose.model('Feature', featureSchema) | ||
|
||
export default Feature |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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