Skip to content

Commit

Permalink
add world features
Browse files Browse the repository at this point in the history
  • Loading branch information
cubap committed Oct 15, 2024
1 parent b427a93 commit cbc40ac
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 5 deletions.
19 changes: 19 additions & 0 deletions client.js
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')
})
30 changes: 25 additions & 5 deletions gameLogic.js
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
11 changes: 11 additions & 0 deletions models/Entity.js
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
11 changes: 11 additions & 0 deletions models/Feature.js
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
135 changes: 135 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"express": "^4.21.1",
"mongoose": "^8.7.1",
"socket.io": "^4.8.0",
"socket.io-client": "^4.8.0",
"supertest": "^7.0.0"
}
}

0 comments on commit cbc40ac

Please sign in to comment.