106 lines
2.4 KiB
C++
106 lines
2.4 KiB
C++
|
//
|
||
|
// Created by tqcq on 2023/11/14.
|
||
|
//
|
||
|
|
||
|
#include "GameObject.h"
|
||
|
#include "FoodObject.h"
|
||
|
|
||
|
std::map<int, GameObject *> GameObject::game_object_map;
|
||
|
int GameObject::game_object_id_counter = 1;
|
||
|
|
||
|
GameObject::GameObject()
|
||
|
: game_object_id(game_object_id_counter++), object_type(Default) {
|
||
|
}
|
||
|
|
||
|
GameObject::GameObject(const std::string &name, const std::string &keyword, const std::string &description)
|
||
|
: game_object_id(game_object_id_counter++), object_type(Default), name(name), keyword(keyword),
|
||
|
description(description) {
|
||
|
}
|
||
|
|
||
|
const std::string &GameObject::getName() const {
|
||
|
return name;
|
||
|
}
|
||
|
|
||
|
const std::string &GameObject::getKeyword() const {
|
||
|
return keyword;
|
||
|
}
|
||
|
|
||
|
const std::string &GameObject::getDescription() const {
|
||
|
return description;
|
||
|
}
|
||
|
|
||
|
std::string GameObject::ToString() const {
|
||
|
Packer packer;
|
||
|
packer.pack(game_object_id);
|
||
|
packer.pack(name);
|
||
|
packer.pack(keyword);
|
||
|
packer.pack(description);
|
||
|
|
||
|
return packer.ToHexStr();
|
||
|
}
|
||
|
|
||
|
void GameObject::FromString(const std::string &str) {
|
||
|
Packer packer;
|
||
|
packer.FromHexStr(str);
|
||
|
packer.unpack(description);
|
||
|
packer.unpack(keyword);
|
||
|
packer.unpack(name);
|
||
|
packer.unpack(game_object_id);
|
||
|
}
|
||
|
|
||
|
GameObject *GameObject::CreateGameObject(GameObject::Type type, bool add_to_map) {
|
||
|
GameObject *object = nullptr;
|
||
|
switch (type) {
|
||
|
case Default:
|
||
|
object = new GameObject();
|
||
|
break;
|
||
|
case Food:
|
||
|
object = new FoodObject();
|
||
|
break;
|
||
|
default:
|
||
|
return nullptr;
|
||
|
}
|
||
|
if (add_to_map) AddGameObject(object);
|
||
|
return object;
|
||
|
}
|
||
|
|
||
|
int GameObject::getGameObjectId() const {
|
||
|
return game_object_id;
|
||
|
}
|
||
|
|
||
|
GameObject::Type GameObject::getObjectType() const {
|
||
|
return object_type;
|
||
|
}
|
||
|
|
||
|
GameObject *GameObject::getGameObjectById(int id) {
|
||
|
auto it = game_object_map.find(id);
|
||
|
return it == game_object_map.end() ? nullptr : it->second;
|
||
|
}
|
||
|
|
||
|
void GameObject::AddGameObject(GameObject *object) {
|
||
|
if (!object) {
|
||
|
return;
|
||
|
}
|
||
|
game_object_map.insert({object->getGameObjectId(), object});
|
||
|
}
|
||
|
|
||
|
GameObject::~GameObject() {
|
||
|
game_object_map.erase(game_object_id);
|
||
|
}
|
||
|
|
||
|
void GameObject::setName(const std::string &_name) {
|
||
|
name = _name;
|
||
|
}
|
||
|
|
||
|
void GameObject::setKeyword(const std::string &_keyword) {
|
||
|
keyword = _keyword;
|
||
|
}
|
||
|
|
||
|
void GameObject::setDescription(const std::string &_description) {
|
||
|
description = _description;
|
||
|
}
|
||
|
|
||
|
std::string GameObject::getInfo() const {
|
||
|
return keyword + ": " + name;
|
||
|
}
|