From e2b7a43b6bf567190144cb9b69951c5055e9d7d4 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 22 Aug 2010 11:52:24 +0100 Subject: [PATCH] Add Industry implementation --- buildings/power_plant/power_plant.xml | 3 ++ include/ICargo.hpp | 3 ++ include/IIndustry.hpp | 2 ++ schemas/building.xsd | 21 +++++++++++++ src/Building.cpp | 27 ++++++++++++++++- src/Industry.cpp | 43 +++++++++++++++++++++++++++ 6 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/Industry.cpp diff --git a/buildings/power_plant/power_plant.xml b/buildings/power_plant/power_plant.xml index 56d34b5..f68885d 100644 --- a/buildings/power_plant/power_plant.xml +++ b/buildings/power_plant/power_plant.xml @@ -2,4 +2,7 @@ Power Plant power_plant.obj + + + diff --git a/include/ICargo.hpp b/include/ICargo.hpp index a768f5d..86bc05d 100644 --- a/include/ICargo.hpp +++ b/include/ICargo.hpp @@ -21,6 +21,7 @@ #include "Platform.hpp" enum CargoType { + NONE = 0, COAL }; @@ -33,4 +34,6 @@ struct ICargo { typedef shared_ptr ICargoPtr; +ICargoPtr make_cargo(CargoType type); + #endif diff --git a/include/IIndustry.hpp b/include/IIndustry.hpp index 6a4d0ca..f770d49 100644 --- a/include/IIndustry.hpp +++ b/include/IIndustry.hpp @@ -31,4 +31,6 @@ struct IIndustry { typedef shared_ptr IIndustryPtr; +IIndustryPtr make_industry(CargoType produces, CargoType consumes); + #endif diff --git a/schemas/building.xsd b/schemas/building.xsd index aa56b5a..4845067 100644 --- a/schemas/building.xsd +++ b/schemas/building.xsd @@ -5,6 +5,27 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Building.cpp b/src/Building.cpp index 43f9107..457d5b3 100644 --- a/src/Building.cpp +++ b/src/Building.cpp @@ -43,16 +43,22 @@ public: // IXMLCallback interface void text(const string& local_name, const string& a_string); + void start_element(const string& local_name, + const AttributeSet& attrs); private: + static CargoType cargo_from_xml(const AttributeSet& attrs); + IModelPtr model_; string name_; IResourcePtr resource; float angle; Vector position; + IIndustryPtr industry_; struct ParserState { string model_file; + CargoType consumes, produces; } *parser_state; }; @@ -67,7 +73,8 @@ Building::Building(IResourcePtr a_res) Vector shift = -make_vector(0.5f, 0.0f, 0.5f); model_ = load_model(a_res, parser_state->model_file, 1.0f, shift); - + industry_ = make_industry(parser_state->produces, parser_state->consumes); + delete parser_state; } @@ -113,6 +120,24 @@ void Building::text(const string& local_name, const string& a_string) parser_state->model_file = a_string; } +CargoType Building::cargo_from_xml(const AttributeSet& attrs) +{ + const string type = attrs.get("cargo"); + if (type == "coal") + return COAL; + else + throw runtime_error("Invalid cargo type: " + type); +} + +void Building::start_element(const string& local_name, + const AttributeSet& attrs) +{ + if (local_name == "consumes") + parser_state->consumes = cargo_from_xml(attrs); + else if (local_name == "produces") + parser_state->produces = cargo_from_xml(attrs); +} + xml::element Building::to_xml() const { return xml::element("building") diff --git a/src/Industry.cpp b/src/Industry.cpp new file mode 100644 index 0000000..36e9663 --- /dev/null +++ b/src/Industry.cpp @@ -0,0 +1,43 @@ +// +// Copyright (C) 2010 Nick Gasson +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#include "IIndustry.hpp" + +class Industry : public IIndustry { +public: + Industry(CargoType produces, CargoType consumes); + + // IIndustry interface + CargoType produces() const { return produces_; } + CargoType consumes() const { return consumes_; } + +private: + CargoType produces_, consumes_; +}; + +Industry::Industry(CargoType produces, CargoType consumes) + : produces_(produces), consumes_(consumes) +{ + +} + +IIndustryPtr make_industry(CargoType produces, CargoType consumes) +{ + return IIndustryPtr(new Industry(produces, consumes)); +} + + -- 2.39.2