-
-
Save anna274/53b2b331f7ae2052ecab0cc6507177c7 to your computer and use it in GitHub Desktop.
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
import InteractionObject from '../objects/interactionObjects/InteractionObject'; | |
import { restock } from './playerStates/stats'; | |
import eventsCenter from '../eventsCenter'; | |
export default class ObjectInteraction { | |
constructor(scene, player) { | |
this.scene = scene; | |
this.player = player; | |
this.activatedObjects = []; | |
this.activeObject = null; | |
scene.matterCollision.addOnCollideStart({ | |
objectA: [this.player.sensors.objectSensor], | |
callback: (eventData) => { | |
const { gameObjectB } = eventData; | |
if (gameObjectB instanceof InteractionObject && !gameObjectB.activated) { | |
this.onObjectCollide(gameObjectB); | |
} | |
}, | |
context: this, | |
}); | |
scene.matterCollision.addOnCollideEnd({ | |
objectA: [this.player.sensors.objectSensor], | |
callback: (eventData) => { | |
const { gameObjectB } = eventData; | |
if (gameObjectB instanceof InteractionObject && gameObjectB.activated) { | |
this.onObjectCollideEnd(gameObjectB); | |
} | |
}, | |
context: this, | |
}); | |
this.interactKey = this.scene.input.keyboard.addKey('E'); | |
this.interactKey.on('up', this.interact, this); | |
eventsCenter.on('player-died', () => { | |
this.interactKey.off('up', this.interact, this); | |
}); | |
} | |
// герой столкнулся с объектом | |
onObjectCollide(object) { | |
// есть ли сейчас активный объект | |
if (this.activeObject) { | |
// заносим объект в "очередь" активированных объектов, с которыми | |
// можем провзаимодействовать позже | |
this.activatedObjects.push(this.activeObject); | |
this.activeObject.deactivate(); | |
} | |
// активным становится объект, с которым только что произошло столкновение | |
this.activeObject = object; | |
this.activeObject.activate(); | |
object.setActivated(true); | |
} | |
// герой прошёл объект | |
onObjectCollideEnd(object) { | |
// отошёл ли герой от активного объекта | |
if (object === this.activeObject) { | |
// деактивируем объект | |
this.activeObject.deactivate(); | |
// меняем активный объект | |
this.changeActiveObject(); | |
} else { | |
// герой отшёл от ранее активированного объекта | |
// найдём и удалим его из нашей "очереди" | |
const objectIndex = this.activatedObjects.findIndex((el) => el === object); | |
this.activatedObjects.splice(objectIndex, 1); | |
} | |
object.setActivated(false); | |
} | |
// обработка нажатия клавиши взаимодействия | |
interact() { | |
// есть ли сейчас объект, с которым можно провзаимодействовать | |
if (this.activeObject) { | |
const interactionInfo = this.activeObject.interact(); | |
if (interactionInfo.type === 'storage') { | |
interactionInfo.items.forEach((item) => { | |
updateStats(item.name, item.quantity); | |
}); | |
} | |
this.changeActiveObject(); | |
} | |
} | |
changeActiveObject() { | |
// есть ли ранее активированные объекты | |
if (this.activatedObjects.length !== 0) { | |
// находим последний активированный объект | |
// и определяем его как активный | |
this.activeObject = this.activatedObjects.pop(); | |
this.activeObject.activate(); | |
} else { | |
this.activeObject = null; | |
} | |
} | |
processInteraction(info) { | |
if (info.type === 'storage') { | |
restock(info.items); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment