Grog-Knight

some game in C++
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/Grog-Knight

commit d6f887ebbb65536516aa9cabd6d8e7b2744cf9c9
parent dce052d0e1ad9fa9b0464bb3e22e2693b2b1ff86
Author: Louis Solofrizzo <louis@ne02ptzero.me>
Date:   Fri, 27 Feb 2015 18:09:50 +0100

Merge branch 'build'

Diffstat:
MAngel/Actors/PhysicsActor.cpp | 1-
MAngel/Infrastructure/World.cpp | 1+
MAngel/Infrastructure/World.h | 1+
MAngel/Scripting/Interfaces/angel.i | 2+-
MAngel/Scripting/Interfaces/inheritance.i | 5+++++
MConfig/input_bindings.ini | 23+++++++++++++++--------
MMACOSX/Makefile | 31++++++++++++++++++++++---------
MMakefile | 12++++++++++--
MREADME.md | 7++++---
AResources/Elements/Hero.json | 43+++++++++++++++++++++++++++++++++++++++++++
AResources/Elements/Sword.json | 36++++++++++++++++++++++++++++++++++++
AResources/Images/Enemy/enemy_000.png | 0
AResources/Images/Enemy/enemy_001.png | 0
AResources/Images/Enemy/enemy_002.png | 0
AResources/Images/Enemy/enemy_003.png | 0
AResources/Images/Enemy/enemy_004.png | 0
AResources/Images/Hero/old/hero_000.png | 0
AResources/Images/Hero/old/hero_001.png | 0
AResources/Images/Hero/old/hero_002.png | 0
AResources/Images/Hero/old/hero_003.png | 0
AResources/Images/Hero/old/hero_004.png | 0
AResources/Images/Hero/old/hero_005.png | 0
AResources/Images/Hero/old/hero_006.png | 0
AResources/Images/Hero/old/hero_007.png | 0
AResources/Images/Hero/old/hero_008.png | 0
AResources/Images/Hero/old/hero_009.png | 0
ASources/inc/Characters.hpp | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ASources/inc/ContactFilter.hpp | 38++++++++++++++++++++++++++++++++++++++
MSources/inc/Elements.hpp | 56++++++++++++++++++++++++++++++++++----------------------
ASources/inc/Enemy.hpp | 43+++++++++++++++++++++++++++++++++++++++++++
MSources/inc/Game.hpp | 5+++++
DSources/inc/Game.hpp.gch | 0
MSources/inc/Hero.hpp | 48++++++++++++++++--------------------------------
ASources/inc/Log.hpp | 45+++++++++++++++++++++++++++++++++++++++++++++
ASources/inc/Projectile.hpp | 45+++++++++++++++++++++++++++++++++++++++++++++
ASources/inc/Shooter.hpp | 48++++++++++++++++++++++++++++++++++++++++++++++++
MSources/inc/Weapon.hpp | 32++++++++++++++++++++++++++------
ASources/src/Characters.cpp | 310+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ASources/src/ContactFilter.cpp | 41+++++++++++++++++++++++++++++++++++++++++
MSources/src/Elements.cpp | 7++-----
ASources/src/Enemy.cpp | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MSources/src/Game.cpp | 21+++++++++++++++++++--
MSources/src/Hero.cpp | 185+++++--------------------------------------------------------------------------
ASources/src/Log.cpp | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MSources/src/Maps.cpp | 2--
ASources/src/Projectile.cpp | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ASources/src/Shooter.cpp | 48++++++++++++++++++++++++++++++++++++++++++++++++
MSources/src/Weapon.cpp | 132+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
MSources/src/main.cpp | 10++++++++--
49 files changed, 1280 insertions(+), 289 deletions(-)

diff --git a/Angel/Actors/PhysicsActor.cpp b/Angel/Actors/PhysicsActor.cpp @@ -55,7 +55,6 @@ PhysicsActor::PhysicsActor(void) : } void PhysicsActor::setId(int i) { - std::cout << "New set id " << i << " (" << this->_id << "), " << Game::currentIds<< std::endl; this->_id = i; } PhysicsActor::~PhysicsActor() diff --git a/Angel/Infrastructure/World.cpp b/Angel/Infrastructure/World.cpp @@ -648,6 +648,7 @@ void World::EndContact(b2Contact* contact) void World::TickAndRender() { Tick(); + Game::destroyAllBodies(); Render(); } diff --git a/Angel/Infrastructure/World.h b/Angel/Infrastructure/World.h @@ -63,6 +63,7 @@ class Console; * * http://msdn.microsoft.com/en-us/library/ms954629.aspx */ + class World : public b2ContactListener, public MessageListener { public: diff --git a/Angel/Scripting/Interfaces/angel.i b/Angel/Scripting/Interfaces/angel.i @@ -82,7 +82,7 @@ typedef std::vector<String> StringList; %include textures.i #ifndef INTROGAME -%include ../../../ClientGame/script_interface.i +%include ../../script_interface.i #else %include ../../../IntroGame/script_interface.i #endif diff --git a/Angel/Scripting/Interfaces/inheritance.i b/Angel/Scripting/Interfaces/inheritance.i @@ -1,2 +1,6 @@ %include <factory.i> +%factory(Actor*, TextActor, HUDActor, FullScreenActor, Camera, PhysicsActor, ParticleActor); +%factory(DeveloperLog*, ConsoleLog, CompoundLog, FileLog, SystemLog); +%factory(MessageListener*, TextActor, FullScreenActor, Camera, HUDActor, PhysicsActor, ParticleActor, Actor); +%factory(Renderable*, TextActor, FullScreenActor, HUDActor, Camera, PhysicsActor, Actor, GridActor, ParticleActor);+ \ No newline at end of file diff --git a/Config/input_bindings.ini b/Config/input_bindings.ini @@ -1,9 +1,16 @@ ; This is the key-bindings file. -D= +forwardPressed -D= -forwardRealeased -A= +backPressed -A= -backRealeased -C= Smash -R= RangedAttack -F= MeleeAttack -SPACE= Jump +;; MOVEMENT + D = +forwardPressed + D = -forwardReleased + A = +backwardPressed + A = -backwardReleased + SPACE = +jumpPressed + SPACE = -jumpReleased + +;; ORENTATION + W = upPressed + S = downPressed + +;; ATTACK + E = +attackPressed + E = -attackReleased diff --git a/MACOSX/Makefile b/MACOSX/Makefile @@ -2,14 +2,22 @@ NAME = rogue-like CC = g++ CFLAGS = -std=c++0x -arch i386 -Wno-deprecated ANGEL_FLAG = -D ANGEL -SRCS = ../Sources/src/Elements.cpp \ +SRCS = ../Sources/src/main.cpp \ + ../Sources/src/Elements.cpp \ ../Sources/src/Game.cpp \ + ../Sources/src/Characters.cpp \ ../Sources/src/Weapon.cpp \ + ../Sources/src/Shooter.cpp \ + ../Sources/src/Projectile.cpp \ ../Sources/src/Hero.cpp \ + ../Sources/src/Enemy.cpp \ ../Sources/src/Maps.cpp \ - ../Sources/src/main.cpp \ ../Angel/Scripting/Interfaces/AngelLuaWrapping.cpp \ - ../Sources/src/GameContactListener.cpp + ../Sources/src/GameContactListener.cpp \ + ../Sources/src/ContactFilter.cpp \ + ../Sources/src/Log.cpp + +OBJS = $(SRCS:.cpp=.o) INCLUDES = -I../Angel/Libraries/FTGL/include \ -I../Angel/Libraries/freetype-2.4.8/include \ @@ -22,7 +30,8 @@ INCLUDES = -I../Angel/Libraries/FTGL/include \ -I../Angel/Libraries/FMOD/inc \ -I../Angel/Libraries/HID\ Utilities \ -I../Angel/Libraries/lua-5.2.1/src \ - -I../Tools/jsoncpp/include/ + -I../Tools/jsoncpp/include/ \ + -I../Sources/inc/ LIBS = ../Angel/build/libAngel.a \ ../Angel/build/libBox2D.a \ @@ -50,16 +59,20 @@ LIBS_DIRS = -L../Angel/build \ -L../Tools/jsoncpp/src/lib_json/ \ -L../Angel/Libraries/DevIL-SDK-1.7.8/mac/lib + all: $(NAME) -$(NAME): angel json - $(CC) $(CFLAGS) $(ANGEL_FLAG) $(SRCS) $(INCLUDES) $(LIBS) $(FRAMEWORKS) $(LIBS_FLAG) $(LIBS_DIRS) -o build/$(NAME) +$(NAME): angel json $(OBJS) + $(CC) $(CFLAGS) $(ANGEL_FLAG) -o build/$@ $(OBJS) $(INCLUDES) $(LIBS) $(FRAMEWORKS) $(LIBS_FLAG) $(LIBS_DIRS) cd build/ && ./rogue-like -quick: - $(CC) $(CFLAGS) $(ANGEL_FLAG) $(SRCS) $(INCLUDES) $(LIBS) $(FRAMEWORKS) $(LIBS_FLAG) $(LIBS_DIRS) -o build/$(NAME) +quick: $(OBJS) + $(CC) $(CFLAGS) $(ANGEL_FLAG) -o build/$(NAME) $^ $(INCLUDES) $(LIBS) $(FRAMEWORKS) $(LIBS_FLAG) $(LIBS_DIRS) cd build/ && ./rogue-like +%.o: %.cpp + $(CC) $(CFLAGS) $(INCLUDES) $(FRAMEWORKS) $(LIBS_FLAG) $(LIBS_DIRS) -o $@ -c $^ + json: $(CC) $(CFLAGS) -I../Tools/jsoncpp/include/ -c $(wildcard ../Tools/jsoncpp/src/lib_json/*.cpp) \ && ar rcs libjson.a ./json_reader.o ./json_value.o ./json_writer.o && mv libjson.a ../Tools/jsoncpp/src/lib_json/. @@ -76,6 +89,6 @@ angel-clean: clean: angel-clean json-clean exec: - cd ../Angel/build/ && ./rogue-like + cd build/ && ./rogue-like re: clean all diff --git a/Makefile b/Makefile @@ -18,7 +18,8 @@ INCLUDE = \ -IAngel/Libraries/FTGL/include \ -IAngel/Libraries/lua-5.2.1/src \ -I/usr/include/freetype2 \ - -I./Tools/jsoncpp/include + -I./Tools/jsoncpp/include \ + -ISources/inc/ ifneq ($(ANGEL_DISABLE_FMOD), 1) INCLUDE += -IAngel/Libraries/FMOD/inc endif @@ -60,7 +61,14 @@ SRCS = ./Sources/src/Elements.cpp \ ./Sources/src/main.cpp \ ./Sources/src/Maps.cpp \ ./Sources/src/Hero.cpp \ - ./Sources/src/GameContactListener.cpp + ./Sources/src/Shooter.cpp \ + ./Sources/src/Projectile.cpp \ + ./Sources/src/Enemy.cpp \ + ./Sources/src/GameContactListener.cpp \ + ./Sources/src/ContactFilter.cpp \ + ./Sources/src/Weapon.cpp \ + ./Sources/src/Log.cpp \ + ./Sources/src/Characters.cpp SYSOBJS = $(patsubst %.cpp,%.o,$(SYSSRCS)) OBJS = $(patsubst %.cpp,%.o,$(SRCS)) diff --git a/README.md b/README.md @@ -1,7 +1,8 @@ # Rogue-Like -[![Build Status](https://travis-ci.org/Ne02ptzero/rogue-like.svg?branch=master)](https://travis-ci.org/Ne02ptzero/rogue-like) - -[![Join the chat at https://gitter.im/Ne02ptzero/rogue-like](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Ne02ptzero/rogue-like?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Build Status](https://travis-ci.org/Ne02ptzero/rogue-like.svg?branch=build)](https://travis-ci.org/Ne02ptzero/rogue-like) +![Language](https://img.shields.io/badge/language-c%2B%2B-orange.svg) +[![License](https://img.shields.io/badge/license-BSD-blue.svg)](LICENSE) +[![Join the chat at https://gitter.im/Ne02ptzero/rogue-like](https://img.shields.io/badge/chat-gitter-red.svg)](https://gitter.im/Ne02ptzero/rogue-like?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) ### First draft on open project thoughts and timeline diff --git a/Resources/Elements/Hero.json b/Resources/Elements/Hero.json @@ -0,0 +1,43 @@ +{ + "infos": { + "name": "Hero", + "id": 0, + "HP": 100, + "sprites": "Resources/Images/Hero/hero_000.png", + "size": 1.3, + "maxSpeed": 5, + "weapon": "Sword" + }, + "Actions": { + "forward": { + "subscribe": "forward", + "beginFrame": 4, + "endFrame": 9, + "time": 0.1, + "force": 5 + }, + "backward": { + "subscribe": "backward", + "beginFrame": 4, + "endFrame": 9, + "time": 0.1, + "force": 5 + }, + "jump": { + "subscribe": "jump", + "double": 1, + "force": 5, + "beginFrame": 16, + "endFrame": 27, + "time": 0.1 + }, + "attack": { + "subscribe": "attack" + }, + "breath": { + "beginFrame": 1, + "endFrame": 3, + "time": 0.1 + } + } +} diff --git a/Resources/Elements/Sword.json b/Resources/Elements/Sword.json @@ -0,0 +1,35 @@ +{ + "infos": { + "name": "Sword", + "flavor": "Hey look, its a sword. A pretty shitty one actually...", + "hitbox": "SWORD", + "size": 1, + "active": 0.2, + "lvlRequired": 1, + "damage": 1, + "recovery": 1, + "active": 0.2, + "attack": "melee", + "combo": true, + "particule": "la soeur de ryad", + "heroAnim": "sword_anim_1" + }, + "combo": { + "1": { + "damage": 10, + "particule": "entrecote", + "heroAnim": "sword_anim_2" + }, + "2": { + "damage": 100, + "specialEffect": "stunSpecial", + "heroAnim": "sword_anim_3" + } + }, + "Capacities": { + "stunSpecial": { + "duration": 1, + "particule": "boom" + } + } +}+ \ No newline at end of file diff --git a/Resources/Images/Enemy/enemy_000.png b/Resources/Images/Enemy/enemy_000.png Binary files differ. diff --git a/Resources/Images/Enemy/enemy_001.png b/Resources/Images/Enemy/enemy_001.png Binary files differ. diff --git a/Resources/Images/Enemy/enemy_002.png b/Resources/Images/Enemy/enemy_002.png Binary files differ. diff --git a/Resources/Images/Enemy/enemy_003.png b/Resources/Images/Enemy/enemy_003.png Binary files differ. diff --git a/Resources/Images/Enemy/enemy_004.png b/Resources/Images/Enemy/enemy_004.png Binary files differ. diff --git a/Resources/Images/Hero/old/hero_000.png b/Resources/Images/Hero/old/hero_000.png Binary files differ. diff --git a/Resources/Images/Hero/old/hero_001.png b/Resources/Images/Hero/old/hero_001.png Binary files differ. diff --git a/Resources/Images/Hero/old/hero_002.png b/Resources/Images/Hero/old/hero_002.png Binary files differ. diff --git a/Resources/Images/Hero/old/hero_003.png b/Resources/Images/Hero/old/hero_003.png Binary files differ. diff --git a/Resources/Images/Hero/old/hero_004.png b/Resources/Images/Hero/old/hero_004.png Binary files differ. diff --git a/Resources/Images/Hero/old/hero_005.png b/Resources/Images/Hero/old/hero_005.png Binary files differ. diff --git a/Resources/Images/Hero/old/hero_006.png b/Resources/Images/Hero/old/hero_006.png Binary files differ. diff --git a/Resources/Images/Hero/old/hero_007.png b/Resources/Images/Hero/old/hero_007.png Binary files differ. diff --git a/Resources/Images/Hero/old/hero_008.png b/Resources/Images/Hero/old/hero_008.png Binary files differ. diff --git a/Resources/Images/Hero/old/hero_009.png b/Resources/Images/Hero/old/hero_009.png Binary files differ. diff --git a/Sources/inc/Characters.hpp b/Sources/inc/Characters.hpp @@ -0,0 +1,75 @@ + +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * File: Characters.hpp + * Creation: 2015-02-27 04:45 + * Louis Solofrizzo <louis@ne02ptzero.me> + */ + +#ifndef __Characters__ +# define __Characters__ + +# include "Weapon.hpp" +# include "Elements.hpp" +# include "Log.hpp" +# include "json/json.h" + +class Characters : public Elements { + + public: + Characters(void); + Characters(std::string name); + ~Characters(); + + virtual void ReceiveMessage(Message *m); + virtual void AnimCallback(String s); + virtual void BeginContact(Elements *elem, b2Contact *contact); + virtual void EndContact(Elements *elem, b2Contact *contact); + // Virtual function, overwritten in childs + virtual void actionCallback(std::string name, int status) {}; + + protected: + std::string _name; + int _id; + int _size; + int _maxSpeed; + int _isJump; + Weapon* _weapon; + std::list<Elements*> _grounds; + std::list<Elements*> _walls; + + Json::Value _getAttr(std::string category, std::string key); + Json::Value _getAttr(std::string key); + void _setCategory(std::string category); + virtual void _forward(int status); + virtual void _backward(int status); + virtual void _jump(int status); + virtual void _attack(int status); + + private: + std::map<std::string, std::map<std::string, Json::Value> > _attr; + std::string _category; + + void _readFile(std::string name); + void _parseJson(std::string file); +}; + +#endif diff --git a/Sources/inc/ContactFilter.hpp b/Sources/inc/ContactFilter.hpp @@ -0,0 +1,38 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * File: ContactFilter.cpp + * Creation: 2015-02-23 12:40 + * Vincent Rey <vrey@student.42.fr> + */ + +#ifndef __ContactFilter__ +# define __ContactFilter__ + +# include "Elements.hpp" +# include "Game.hpp" +# include "../../Angel/Angel.h" + +class ContactFilter: public b2ContactFilter { +public: + bool ShouldCollide(b2Fixture *a, b2Fixture *b); +}; + +#endif diff --git a/Sources/inc/Elements.hpp b/Sources/inc/Elements.hpp @@ -34,29 +34,41 @@ # include "Maps.hpp" # include "../../Angel/Angel.h" +class Weapon; + class Elements : public PhysicsActor { - public: - Elements(); - Elements(int id); - Elements(Elements & obj); - ~Elements(); - - void addAttribute(std::string name, std::string value); - std::string getAttribute(std::string name); - void setXStart(float X); - void setYStart(float Y); - void display(void); - std::map<std::string, std::string> getAttributes(void); - - /* Virtual function, overrited in Childs */ - virtual void callback(Elements * elem) { }; - virtual void BeginContact(Elements * elem, b2Contact *contact) { }; - virtual void EndContact(Elements * elem, b2Contact *contact) { }; - - private: - float _XStartPos; - float _YStartPos; - std::map<std::string, std::string> _attributes; +public: + Elements(); + Elements(int id); + Elements(Elements & obj); + ~Elements(); + + enum Orientation { + LEFT, + RIGHT, + UP, + DOWN, + }; + + void addAttribute(std::string name, std::string value); + std::string getAttribute(std::string name); + void setXStart(float X); + void setYStart(float Y); + void display(void); + std::map<std::string, std::string> getAttributes(void); + + /* Virtual function, overridden in Childs */ + virtual void callback(Elements * elem) { }; + virtual void BeginContact(Elements * elem, b2Contact *contact) { }; + virtual void EndContact(Elements * elem, b2Contact *contact) { }; + + int getOrientationX(void); + int getOrientationY(void); + int getLateralOrientation(void); +private: + float _XStartPos; + float _YStartPos; + std::map<std::string, std::string> _attributes; }; #endif diff --git a/Sources/inc/Enemy.hpp b/Sources/inc/Enemy.hpp @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * File: Enemy.hpp + * Creation: 2015-02-23 14:25 + * Manon Budin <mbudin@student.42.fr> + */ + + +#ifndef __Enemy__ +# define __Enemy__ + +# include "Elements.hpp" +# include "Hero.hpp" + +class Enemy : public Elements { +public: + Enemy(); + ~Enemy(); + + virtual void callback(Elements * elem); + void ReceiveMessage(Message *m); + void init(void); +}; + +#endif diff --git a/Sources/inc/Game.hpp b/Sources/inc/Game.hpp @@ -32,6 +32,7 @@ # include "Elements.hpp" # endif # include "GameContactListener.hpp" +# include "ContactFilter.hpp" class Game { @@ -46,6 +47,10 @@ class Game { void initMap(void); void displayMap(t_map map); void displayHero(Elements & Hero); + void displayEnemy(Elements & Enemy); + + static void destroyAllBodies(void); + static int getNextId(void); static void addElement(Elements & elem); diff --git a/Sources/inc/Game.hpp.gch b/Sources/inc/Game.hpp.gch Binary files differ. diff --git a/Sources/inc/Hero.hpp b/Sources/inc/Hero.hpp @@ -26,38 +26,22 @@ #ifndef __Hero__ # define __Hero__ -# include "Elements.hpp" - -# define RUN_SPEED 4 -# define MAX_RUN_SPEED 10 -# define MAX_JUMP 2 -# define HERO_SIZE 1 - -class Hero : public Elements { -public: - Hero(); - ~Hero(); - - virtual void callback(Elements * elem); - virtual void EndContact(Elements * elem, b2Contact *contact); - virtual void BeginContact(Elements * elem, b2Contact *contact); - virtual void AnimCallback(String name); - void ReceiveMessage(Message *m); - void init(); - -private: - int _jumping; - bool _canMove; - bool _invincibility; - bool _meleeAttack; - bool _rangedAttack; - bool _smashing; - bool _canSmash; - int _orientation; - int _up; - std::list<Elements*> _grounds; - std::list<Elements*> _walls; - int _isJump; +# include "Characters.hpp" +# include "Weapon.hpp" +# include "Shooter.hpp" + +class Hero : public Characters { + + public: + Hero(); + ~Hero(); + + void init(); + virtual void actionCallback(std::string name, int status); + + private: + Shooter *_shooter; + }; #endif diff --git a/Sources/inc/Log.hpp b/Sources/inc/Log.hpp @@ -0,0 +1,45 @@ + +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * File: Log.hpp + * Creation: 2015-02-27 04:51 + * Louis Solofrizzo <louis@ne02ptzero.me> + */ + +#ifndef __Log__ +# define __Log__ + +# include <iostream> +# include <cstdlib> + +class Log { + + public: + Log(); + ~Log(); + + static void info(std::string name); + static void warning(std::string name); + static void error(std::string name); + +}; + +#endif diff --git a/Sources/inc/Projectile.hpp b/Sources/inc/Projectile.hpp @@ -0,0 +1,45 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * File: Projectile.hpp + * Creation: 2015-02-23 15:14 + * Matthieu Maudet <mmaudet@student.42.fr> + */ + +#ifndef __Projectile__ +# define __Projectile__ + +# include "Elements.hpp" + +/* +** Default constructor +** @param: Elements * +*/ + +class Projectile: public Elements { +public: + Projectile(float x, float y, int direction, std::string owner); + ~Projectile(); + void BeginContact(Elements *elem, b2Contact *contact); + void EndContact(Elements *elem, b2Contact *contact); + void ReceiveMessage(Message *m); +}; + +#endif diff --git a/Sources/inc/Shooter.hpp b/Sources/inc/Shooter.hpp @@ -0,0 +1,48 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * File: Shooter.hpp + * Creation: 2015-02-23 16:14 + * Matthieu Maudet <mmaudet@student.42.fr> + */ + +#ifndef __Shooter__ +# define __Shooter__ + +# include <iostream> +# include "Elements.hpp" + +class Shooter : public Elements { +public: + Shooter(); + ~Shooter(); + + void fire(float x, float y, int direction, std::string owner); + +private: + int _damage; + float _rate; + int _orientation; + bool _straight; + std::string _spriteName; + +}; + +#endif diff --git a/Sources/inc/Weapon.hpp b/Sources/inc/Weapon.hpp @@ -23,11 +23,12 @@ * Vincent Rey <vrey@student.42.fr> */ -#ifndef __Weapon +#ifndef __Weapon__ # define __Weapon__ +# include "Log.hpp" # include "Elements.hpp" - +# include "json/json.h" /* ** Default constructor, using the element that called the attack ** @param: Elements * @@ -35,10 +36,29 @@ class Weapon: public Elements { public: - Weapon(Elements *m); - ~Weapon(); - void BeginContact(Elements *elem, b2Contact *contact); - void EndContact(Elements *elem, b2Contact *contact); + Weapon(std::string name); + ~Weapon(void); + + void attack(int, int, int, int, b2Vec2); + void BeginContact(Elements *elem, b2Contact *contact); + void EndContact(Elements *elem, b2Contact *contact); + void ReceiveMessage(Message *m); + +private: + std::map<std::string, std::map<std::string, Json::Value> > _attr; + + std::string _name; + std::string _flavor; + int _canAttack; + int _recovery; + int _active; + int _size; + void _readFile(std::string name); + void _parseJson(std::string file); +// WeaponArea* _attackBox; + +protected: + Json::Value _getAttr(std::string category, std::string key); }; #endif diff --git a/Sources/src/Characters.cpp b/Sources/src/Characters.cpp @@ -0,0 +1,310 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * File: Characters.cpp + * Creation: 2015-02-27 04:44 + * Louis Solofrizzo <louis@ne02ptzero.me> + */ + +# include "Characters.hpp" + +/** + * Base constructor + */ +Characters::Characters(void) { + return ; +} + +/** + * Main constructor + * @param: name (std::string) + */ +Characters::Characters(std::string name) : _name(name) { + this->addAttribute("physic", "1"); + this->addAttribute("type", name); + this->SetDensity(1.0f); + this->SetFriction(1.0f); + this->SetRestitution(0.0f); + this->SetFixedRotation(true); + this->_readFile(name); +} + +/** + * Basic destructor + */ +Characters::~Characters(void) { + return ; +} + +/** + * Read a config file, base on the name of the class + * @param: name (std::string) + */ +void Characters::_readFile(std::string name) { + std::string file; + std::stringstream buffer; + std::ifstream fd; + + file = "./Resources/Elements/" + name + ".json"; + fd.open(file.c_str()); + if (!fd.is_open()) + Log::error("Can't open the file for the " + + name + " class. (Supposed path is Resources/Elements/" + name +".json)"); + buffer << fd.rdbuf(); + this->_parseJson(buffer.str()); +} + +/** + * Parse, read and stock the info in the config file + * @param: file (std::string) + * @note: file is the whole file content + */ +void Characters::_parseJson(std::string file) { + Json::Reader read; + Json::Value json; + Json::ValueIterator i, v; + std::map<std::string, Json::Value> tmp; + + if (!read.parse(file, json)) + Log::error("Error in json syntax :\n" + read.getFormattedErrorMessages()); + if (json["infos"].get("name", "").asString() != this->_name) + Log::warning("The class name is different with the name in the config file."); + this->_name = json["infos"].get("name", "").asString(); + this->_id = json["infos"].get("id", "").asInt(); + this->_size = json["infos"].get("size", "").asFloat(); + this->_maxSpeed = json["infos"].get("maxSpeed", "").asFloat(); + this->_weapon = new Weapon(json["infos"].get("weapon", "").asString()); + this->addAttribute("spritesFrame", json["infos"].get("sprites", "").asString()); + + for (i = json["Actions"].begin(); i != json["Actions"].end(); i++) { + for (v = (*i).begin(); v != (*i).end(); v++) { + tmp[v.key().asString()] = (*v); + this->_attr[i.key().asString()] = tmp; + // Subcribe to the broadcasts + if (v.key().asString() == "subscribe") { + Log::info("SubscribeTo " + (*v).asString()); + theSwitchboard.SubscribeTo(this, (*v).asString() + "Pressed"); + theSwitchboard.SubscribeTo(this, (*v).asString() + "Released"); + } + } + } +} + +/** + * Get a Json::Value of a key in the config file + * @param: category (std::string) + * @param: key (std::string) + * @note: See the docs for the utilisation of Json::Value + */ +Json::Value Characters::_getAttr(std::string category, std::string key) { + if (this->_attr.find(category) != this->_attr.end()) { + if (this->_attr[category].find(key) != this->_attr[category].end()) + return this->_attr[category][key]; + else + Log::warning("The key " + key + " not in the category " + category); + } else { + Log::warning("The category " + category + " not in the config"); + } + return nullptr; +} + +/** + * Set the current working category + * @param: category (std::string) + */ +void Characters::_setCategory(std::string category) { + this->_category = category; +} + +/** + * Get a Json::Value of a key in the config file, with a pre-set category + * @param: key (std::string) + */ +Json::Value Characters::_getAttr(std::string key) { + if (this->_attr[this->_category].find(key) != this->_attr[this->_category].end()) + return this->_attr[this->_category][key]; + Log::warning("The key " + key + " not in the category " + this->_category); + return nullptr; +} + +/** + * Receive and redistribute broadcasts messages + * @param: m (Message *) + */ +void Characters::ReceiveMessage(Message *m) { + std::map<std::string, std::map<std::string, Json::Value> >::iterator i; + std::string name, attrName; + int status; + + Log::info(m->GetMessageName()); + for (i = this->_attr.begin(); i != this->_attr.end(); i++) { + attrName = this->_getAttr(i->first, "subscribe").asString(); + if (!strncmp(attrName.c_str(), m->GetMessageName().c_str(), strlen(attrName.c_str()))) { + + // Get the key status (1 = Pressed, 0 = Released) + status = (m->GetMessageName().substr(strlen(attrName.c_str()), 7) == "Pressed" ? 1 : 0); + if (attrName == "forward") { + this->_forward(status); + } else if (attrName == "backward") { + this->_backward(status); + } else if (attrName == "jump") { + this->_jump(status); + } else if ("attack") { + this->_attack(status); + } + this->actionCallback(attrName, status); + } + } +} + +/** + * The animation callback + * @param: s (String) + * @note: String is the std::string object used in Angel2d. + */ +void Characters::AnimCallback(String s) { + this->_setCategory("breath"); + if (s == "base") { + this->PlaySpriteAnimation(this->_getAttr("time").asFloat(), SAT_OneShot, + this->_getAttr("beginFrame").asInt(), + this->_getAttr("endFrame").asInt(), "base"); + } +} + +/** + * Collision begin callback + * @param: elem (Elements *) + * @param: contact (b2Contact *) + * @note: This function is called just before a collision + */ +void Characters::BeginContact(Elements *elem, b2Contact *contact) { + if (elem->getAttributes()["type"] == "wall" || + elem->getAttributes()["type"] == "ground" || + elem->getAttributes()["type"] == "corner") { + if (this->GetBody()->GetWorldCenter().y > elem->GetBody()->GetWorldCenter().y + 1) { + if (this->_grounds.size() > 0) + contact->SetEnabled(false); + else { + if (this->_isJump > 0) { + this->_isJump = 0; + this->PlaySpriteAnimation(0.1f, SAT_OneShot, this->_getAttr("jump", "endFrame").asInt(), + this->_getAttr("jump", "endFrame").asInt(), "base"); + } + } + this->_grounds.push_back(elem); + } else { + if (this->_walls.size() > 0) + contact->SetEnabled(false); + this->_walls.push_back(elem); + } + } + +} + +/** + * Collision end callback + * @param: elem (Elements *) + * @param: contact (b2Contact *) + * @note: This function is called just after the elements leave another + */ +void Characters::EndContact(Elements *elem, b2Contact *contact) { + if (elem->getAttributes()["type"] == "wall" || + elem->getAttributes()["type"] == "ground" || + elem->getAttributes()["type"] == "corner") { + if (this->GetBody()->GetWorldCenter().y > elem->GetBody()->GetWorldCenter().y + 1) { + this->_grounds.remove(elem); + } + else { + this->_walls.remove(elem); + } + } +} + +/****************************/ +/* */ +/* ACTIONS */ +/* */ +/****************************/ + +/** + * Forward action + * @param: status (int) + */ +void Characters::_forward(int status) { + this->_setCategory("forward"); + if (status == 1) { + if (this->GetSpriteFrame() < this->_getAttr("beginFrame").asInt()) + this->PlaySpriteAnimation(this->_getAttr("time").asFloat(), SAT_Loop, + this->_getAttr("beginFrame").asInt(), + this->_getAttr("endFrame").asInt()); + if (this->GetBody()->GetLinearVelocity().x < this->_maxSpeed) + this->ApplyLinearImpulse(Vector2(this->_getAttr("force").asFloat(), 0), Vector2(0, 0)); + } else { + this->GetBody()->SetLinearVelocity(b2Vec2(0, this->GetBody()->GetLinearVelocity().y)); + this->AnimCallback("base"); + } + return ; +} + +/** + * Backward action + * @param: status (int) + */ +void Characters::_backward(int status) { + this->_setCategory("backward"); + if (status == 1) { + if (this->GetSpriteFrame() < this->_getAttr("beginFrame").asInt()) + this->PlaySpriteAnimation(this->_getAttr("time").asFloat(), SAT_Loop, + this->_getAttr("beginFrame").asInt(), + this->_getAttr("endFrame").asInt()); + if (this->GetBody()->GetLinearVelocity().x > -(this->_maxSpeed)) + this->ApplyLinearImpulse(Vector2(-(this->_getAttr("force").asFloat()), 0), Vector2(0, 0)); + } else { + this->GetBody()->SetLinearVelocity(b2Vec2(0, this->GetBody()->GetLinearVelocity().y)); + this->AnimCallback("base"); + } + return ; +} + +/** + * Jump Action + * @param: status (int) + */ +void Characters::_jump(int status) { + this->_setCategory("jump"); + if (status == 1) { + if (this->_isJump == 0 || (this->_isJump <= 1 && this->_getAttr("double").asInt() == 1)) { + this->ApplyLinearImpulse(Vector2(0, this->_getAttr("force").asFloat()), Vector2(0, 0)); + this->PlaySpriteAnimation(this->_getAttr("time").asFloat(), SAT_OneShot, + this->_getAttr("beginFrame").asInt(), + this->_getAttr("endFrame").asInt() - 1, "jump"); + this->_isJump++; + } + } + return ; +} + +/** + * Attack action + * @param: status (int) + */ +void Characters::_attack(int status) { + this->_weapon->attack(this->GetBody()->GetWorldCenter().x + 0.75f , this->GetBody()->GetWorldCenter().y, 0, 0, this->GetBody()->GetLinearVelocity()); +} diff --git a/Sources/src/ContactFilter.cpp b/Sources/src/ContactFilter.cpp @@ -0,0 +1,41 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * File: ContactFilter.hpp + * Creation: 2015-02-23 12:40 + * Vincent Rey <vrey@student.42.fr> + */ + + + +#include "../inc/ContactFilter.hpp" + + +bool ContactFilter::ShouldCollide(b2Fixture* fixA, b2Fixture* fixB) { + + std::string attrA = static_cast<Elements*>(fixA->GetBody()->GetUserData())->getAttributes()["type"]; + std::string attrB = static_cast<Elements*>(fixB->GetBody()->GetUserData())->getAttributes()["type"]; + + if ((attrA == "Hero" || attrB == "Hero") && ((attrA == "heroWeapon" || attrB == "heroWeapon") || (attrA == "heroProjectile" || attrB == "heroProjectile"))) { + // std::cout << "hello" << std::endl; + return false; + } + else return true; +} diff --git a/Sources/src/Elements.cpp b/Sources/src/Elements.cpp @@ -1,3 +1,4 @@ + /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -30,7 +31,6 @@ */ Elements::Elements(void) : PhysicsActor() { this->setId(Game::getNextId()); - std::cout << "L" << this->getId() << std::endl; Game::addElement(*this); return ; } @@ -40,7 +40,6 @@ Elements::Elements(void) : PhysicsActor() { */ Elements::Elements(int id) : PhysicsActor() { this->setId(Game::getNextId()); - std::cout << ";" << this->getId() << std::endl; Game::addElement(*this); return ; } @@ -53,14 +52,12 @@ Elements::Elements(Elements & obj) : PhysicsActor(obj) { this->_attributes = obj.getAttributes(); this->setId(Game::getNextId()); Game::addElement(*this); - std::cout << "'" << this->getId() << std::endl; } /** - * Basic deconstructor + * Basic destructor */ Elements::~Elements(void) { - return ; } /** diff --git a/Sources/src/Enemy.cpp b/Sources/src/Enemy.cpp @@ -0,0 +1,80 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * File: Enemy.cpp + * Creation: 2015-02-23 14:26 + * Manon Budin <mbudin@student.42.fr> + */ + + #include "../inc/Enemy.hpp" + +/** + * Basic Constructor + */ +Enemy::Enemy(void) { + this->addAttribute("physic", "1"); + this->addAttribute("type", "Enemy"); + this->SetDensity(1.0f); + this->SetFriction(1.0f); + this->SetRestitution(0.0f); + this->SetFixedRotation(true); + this->SetDrawShape(ADS_Square); + this->addAttribute("spritesFrame", "Resources/Images/Enemy/enemy_000.png"); +} + +/** + * Collision with another element + * @param: elem (Elements *) + */ +void Enemy::callback(Elements * elem) { + if (elem->getAttributes()["type"] == "Hero") { + this->GetBody()->SetLinearVelocity(b2Vec2(0, 0)); + + if (this->GetBody()->GetWorldCenter().x > elem->GetBody()->GetWorldCenter().x) + this->ApplyLinearImpulse(Vector2(10, 10), Vector2(0, 0)); + else + this->ApplyLinearImpulse(Vector2(-10, 10), Vector2(0, 0)); + theSwitchboard.DeferredBroadcast(new Message("canMove"), 1); + theSwitchboard.DeferredBroadcast(new Message("endInvincibility"), 1.5); + } +} + + +/** + * Receive broadcasts message + * @param: (Message *) + */ +void Enemy::ReceiveMessage(Message *m) { + return; +} + + + +void Enemy::init(void) { + this->PlaySpriteAnimation(0.1f, SAT_Loop, 0, 4, "Jump"); +} + +/** + * Basic Destructor + */ +Enemy::~Enemy(void) { + return; +} + diff --git a/Sources/src/Game.cpp b/Sources/src/Game.cpp @@ -30,10 +30,12 @@ * (http://docs.angel2d.com/class_world.html#ae5d7e8d20d3e6fc93785ab2014ac0c13) */ Game::Game(void) { - theWorld.Initialize(1024, 768, NAME); + theWorld.Initialize(1920, 1080, NAME); theWorld.SetupPhysics(); //this->elements = new Elements(); GameContactListener *gListen = new GameContactListener(); + ContactFilter *filter = new ContactFilter(); + theWorld.GetPhysicsWorld().SetContactFilter(filter); theWorld.GetPhysicsWorld().SetContactListener(gListen); this->maps = new Maps("Maps/"); return ; @@ -48,6 +50,8 @@ Game::Game(unsigned int width, unsigned int height) { theWorld.Initialize(width, height, NAME); theWorld.SetupPhysics(); GameContactListener *gListen = new GameContactListener(); + ContactFilter *filter = new ContactFilter(); + theWorld.GetPhysicsWorld().SetContactFilter(filter); theWorld.GetPhysicsWorld().SetContactListener(gListen); //this->elements = new Elements(); this->maps = new Maps("Maps/"); @@ -139,6 +143,17 @@ void Game::displayHero(Elements & Hero) { } /** + * Display the Enemy + * @param: Enemy (Elements &) + */ +void Game::displayEnemy(Elements & Enemy) { + Enemy.setXStart(3); + Enemy.setYStart(3); + Enemy.addAttribute("enemy", "1"); + Enemy.display(); +} + +/** * Get the current id, for the intern elements map */ int Game::getNextId(void) { @@ -150,7 +165,6 @@ int Game::getNextId(void) { * @param: elem (Elements &) */ void Game::addElement(Elements & elem) { - std::cout << Game::currentIds << std::endl; Game::elementMap[Game::currentIds] = &elem; Game::currentIds += 1; } @@ -178,6 +192,9 @@ void Game::listElement(void) { } } +void Game::destroyAllBodies(void) { +} + // Set for the statics int Game::currentIds = 0; std::map<int, Elements *> Game::elementMap = {}; diff --git a/Sources/src/Hero.cpp b/Sources/src/Hero.cpp @@ -28,38 +28,8 @@ /** * Basic constructor */ -Hero::Hero(void) { - //this->addAttribute("sprite", "Resources/Images/test.png"); - this->addAttribute("physic", "1"); - this->addAttribute("type", "Hero"); - this->SetDensity(1.0f); - this->SetFriction(1.0f); - this->SetRestitution(0.0f); - this->SetFixedRotation(true); - this->_jumping = MAX_JUMP; - this->_invincibility = false; - this->_canMove = true; - this->_orientation = 1; - this->_up = 0; - this->_isJump = 0; - theSwitchboard.SubscribeTo(this, "MeleeAttack"); - theSwitchboard.SubscribeTo(this, "RangedAttack"); - theSwitchboard.SubscribeTo(this, "SmashAttack"); - theSwitchboard.SubscribeTo(this, "Jump"); - theSwitchboard.SubscribeTo(this, "Smash"); - theSwitchboard.SubscribeTo(this, "canMove"); - theSwitchboard.SubscribeTo(this, "endInvincibility"); - theSwitchboard.SubscribeTo(this, "forwardPressed"); - theSwitchboard.SubscribeTo(this, "backPressed"); - theSwitchboard.SubscribeTo(this, "upPressed"); - theSwitchboard.SubscribeTo(this, "downPressed"); - theSwitchboard.SubscribeTo(this, "forwardRealeased"); - theSwitchboard.SubscribeTo(this, "backReleased"); - theSwitchboard.SubscribeTo(this, "upReleased"); - theSwitchboard.SubscribeTo(this, "downReleased"); - this->addAttribute("spritesFrame", "Resources/Images/Hero/hero_000.png"); - //theSwitchboard.SubscribeTo(this, "callbackEndRun"); - theSwitchboard.SubscribeTo(this, "Jump"); +Hero::Hero(void) : Characters("Hero") { + return ; } /** @@ -70,151 +40,18 @@ Hero::~Hero(void) { } /** - * Collision with another element - * @param: elem (Elements *) - */ -void Hero::callback(Elements * elem) { - if (elem->getAttributes()["type"] == "enemy") { - this->GetBody()->SetLinearVelocity(b2Vec2(0, 0)); - this->_canMove = false; - this->_invincibility = true; - - if (this->GetBody()->GetWorldCenter().x > elem->GetBody()->GetWorldCenter().x) - this->ApplyLinearImpulse(Vector2(10, 10), Vector2(0, 0)); - else - this->ApplyLinearImpulse(Vector2(-10, 10), Vector2(0, 0)); - theSwitchboard.DeferredBroadcast(new Message("canMove"), 1); - theSwitchboard.DeferredBroadcast(new Message("endInvincibility"), 1.5); - } -} - - -/** - * Receive broadcasts message - * @param: (Message *) + * Init Animation */ -void Hero::ReceiveMessage(Message *m) { - if (m->GetMessageName() == "canMove") - this->_canMove = true; - else if (m->GetMessageName() == "endInvincibility") - this->_invincibility = false; - - if (this->_canMove == false) - return; - else if (m->GetMessageName() == "Jump") { - if (this->_grounds.size() == 0) - this->_jumping--; - this->ApplyLinearImpulse(Vector2(0, 10), Vector2(0, 0)); - this->PlaySpriteAnimation(0.1f, SAT_OneShot, 16, 26, "Jump"); - this->_isJump = 1; - } - - else if (m->GetMessageName() == "Smash") { - this->ApplyLinearImpulse(Vector2(0, -100), Vector2(0, 0)); - } - - else if (m->GetMessageName() == "forwardPressed") { - this->_orientation = 1; - this->_up = 0; - if (this->GetBody()->GetLinearVelocity().x < MAX_RUN_SPEED) { - this->ApplyLinearImpulse(Vector2(RUN_SPEED, 0), Vector2(0, 0)); - } - if (this->GetSpriteFrame() <= 3) - this->PlaySpriteAnimation(0.1f, SAT_Loop, 4, 9, "run"); - } - else if (m->GetMessageName() == "forwardRealeased") { - this->GetBody()->SetLinearVelocity(b2Vec2(0, 0)); - this->PlaySpriteAnimation(0.1f, SAT_OneShot, 0, 3, "base"); - } - - else if (m->GetMessageName() == "backPressed") { - this->_orientation = -1; - this->_up = 0; - if (this->GetBody()->GetLinearVelocity().x > -MAX_RUN_SPEED) { - this->ApplyLinearImpulse(Vector2(-RUN_SPEED, 0), Vector2(0, 0)); - } - if (this->GetSpriteFrame() <= 3) - this->PlaySpriteAnimation(0.1f, SAT_Loop, 4, 9, "run"); - } - else if (m->GetMessageName() == "backReleased") { - this->GetBody()->SetLinearVelocity(b2Vec2(0, 0)); - this->PlaySpriteAnimation(0.1f, SAT_OneShot, 0, 3, "base"); - } - - else if (m->GetMessageName() == "upPressed") { - this->_orientation == 0; - this->_up = 1; - } - - else if (m->GetMessageName() == "downPressed") { - this->_orientation == 0; - this->_up = -1; - } -} - -/* -** Callback before contact in order to disable it if necessary -** @params: Elements*, b2Contact* -*/ - -void Hero::BeginContact(Elements *elem, b2Contact *contact) { - if (elem->getAttributes()["type"] == "wall" || - elem->getAttributes()["type"] == "ground" || - elem->getAttributes()["type"] == "corner") { - if (this->GetBody()->GetWorldCenter().y > elem->GetBody()->GetWorldCenter().y + HERO_SIZE) { - if (this->_grounds.size() > 0) - contact->SetEnabled(false); - this->_grounds.push_back(elem); - this->_jumping = MAX_JUMP; - if (this->_isJump < 3) { - this->_isJump++; - } - else if (this->_isJump == 3) { - this->PlaySpriteAnimation(0.1f, SAT_OneShot, 27, 27, "base"); - this->_isJump = 0; - } - } - else { - if (this->_walls.size() > 0) - contact->SetEnabled(false); - this->_walls.push_back(elem); - } - } -} - -/* -** Callback when a contact is lost -** @params: Elements*, b2Contact* -*/ - -void Hero::EndContact(Elements *elem, b2Contact *contact) { - if (elem->getAttributes()["type"] == "wall" || - elem->getAttributes()["type"] == "ground" || - elem->getAttributes()["type"] == "corner") { - if (this->GetBody()->GetWorldCenter().y > elem->GetBody()->GetWorldCenter().y + HERO_SIZE) { - this->_grounds.remove(elem); - if (this->_grounds.size() == 0) - this->_jumping--; - } - else { - this->_walls.remove(elem); - } - } +void Hero::init(void) { + this->AnimCallback("base"); } /** - * Callback for end of animation - * @param: name (String) + * Mother's callback for actions + * @param: name (std::string) + * @param: status (int) */ -void Hero::AnimCallback(String name) { - std::cout << name << std::endl; - if (name == "base") { - this->PlaySpriteAnimation(0.2f, SAT_Loop, 0, 3, "base"); - } else if (name == "run") { - this->PlaySpriteAnimation(0.1f, SAT_OneShot, 4, 9, "run"); - } -} - -void Hero::init(void) { - this->PlaySpriteAnimation(0.2f, SAT_OneShot, 0, 3, "base"); +void Hero::actionCallback(std::string name, int status) { + std::cout << "CALLBACK " << name << std::endl; + return ; } diff --git a/Sources/src/Log.cpp b/Sources/src/Log.cpp @@ -0,0 +1,65 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * File: Log.cpp + * Creation: 2015-02-27 04:51 + * Louis Solofrizzo <louis@ne02ptzero.me> + */ + +# include "Log.hpp" + +/** + * Base constructor + */ +Log::Log(void) { + return ; +} + +/** + * Base destructor + */ +Log::~Log(void) { + return ; +} + +/** + * Log INFO level + * @param: str (std::string) + */ +void Log::info(std::string str) { + std::cout << "\033[1;34m[INFO]\033[0m " << str << std::endl; +} + +/** + * Log WARNING level + * @param: str (std::string) + */ +void Log::warning(std::string str) { + std::cout << "\033[1;33m[WARNING]\033[0m " << str << std::endl; +} + +/** + * Log ERROR log + * @param: str (std::string) + */ +void Log::error(std::string str) { + std::cout << "\033[1;31m[ERROR]\033[0m " << str << std::endl; + exit(1); +} diff --git a/Sources/src/Maps.cpp b/Sources/src/Maps.cpp @@ -102,9 +102,7 @@ void Maps::_getMap(void) { for (index = 0; index < blocks.size(); index++) { for (v = 0; v < blocks[index].size(); v++) { tmp.push_back(blocks[index][v].asInt()); - std::cout << blocks[index][v].asInt() << ", "; } - std::cout << std::endl; current.map.push_back(tmp); tmp.clear(); } diff --git a/Sources/src/Projectile.cpp b/Sources/src/Projectile.cpp @@ -0,0 +1,71 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * File: Projectile.cpp + * Creation: 2015-02-23 15:140 + * Matthieu Maudet <mmaudet@student.42.fr> + */ + +# include "../inc/Projectile.hpp" + + +/* +** Default constructor +*/ +Projectile::Projectile(float x, float y, int direction, std::string owner) { + this->SetPosition(x, y); + this->SetSize(0.5f); + this->SetName("Projectile"); + this->addAttribute("type", owner+"Projectile"); + this->SetShapeType(PhysicsActor::SHAPETYPE_BOX); + this->SetDensity(1); + this->SetFriction(0); + this->SetRestitution(0.0f); + this->SetFixedRotation(true); + this->Tag("projectile"); + this->InitPhysics(); + this->GetBody()->SetGravityScale(0.0f); + this->ApplyLinearImpulse(Vector2(2 * direction, 0), Vector2(0, 0)); + theWorld.Add(this); + theSwitchboard.DeferredBroadcast(new Message("DeleteProjectile"), 0.5f); +} + +Projectile::~Projectile(void) { + return; +} + +void Projectile::BeginContact(Elements *elem, b2Contact *contact) { +} + +void Projectile::EndContact(Elements *elem, b2Contact *contact) { +} + + +/** + * Receive broadcasts message + * @param: (Message *) + */ +void Projectile::ReceiveMessage(Message *m) { + if (m->GetMessageName() == "DeleteProjectile") { + theWorld.GetPhysicsWorld().DestroyBody(this->GetBody()); + theWorld.Remove(this); + } +} + diff --git a/Sources/src/Shooter.cpp b/Sources/src/Shooter.cpp @@ -0,0 +1,48 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * File: Shooter.cpp + * Creation: 2015-02-23 16:14 + * Matthieu Maudet <mmaudet@student.42.fr> + */ + +# include "../inc/Shooter.hpp" +# include "../inc/Projectile.hpp" +# include "../inc/Elements.hpp" + + +/* +** Default constructor +*/ + +Shooter::Shooter(void) { + this->_damage = 10; + this->_rate = 1; + this->_orientation = 1; + this->_straight = true; + this->_spriteName = "PleaseChangeMe"; +} + +Shooter::~Shooter(void) {} + +void Shooter::fire(float x, float y, int direction, std::string owner) { + std::cout << "FIRE !" << std::endl; + Projectile *projectile = new Projectile(x, y, direction, owner); +} diff --git a/Sources/src/Weapon.cpp b/Sources/src/Weapon.cpp @@ -23,36 +23,128 @@ * Vincent Rey <vrey@student.42.fr> */ -# include "../inc/Weapon.hpp" +# include "Weapon.hpp" /* ** Default constructor, using the element that called the attack ** @param: Elements * */ -Weapon::Weapon(Elements *m) { - this->SetPosition(m->GetBody()->GetWorldCenter().x/* + m->GetOrientationX()*/, m->GetBody()->GetWorldCenter().y/* + m->getOrientationY()*/); - this->SetSize(1); - this->SetName("Weapon"); - this->SetShapeType(PhysicsActor::SHAPETYPE_BOX); - this->SetDensity(1.0f); - this->SetFriction(1.0f); - this->SetRestitution(0.0f); - this->SetFixedRotation(true); - this->Tag("weapon"); - this->InitPhysics(); - this->GetBody()->SetLinearVelocity(m->GetBody()->GetLinearVelocity()); - theWorld.Add(this); - theSwitchboard.DeferredBroadcast(new Message("DeleteWeapon"), 0.1f); -} - -Weapon::~Weapon(void) {} +Weapon::Weapon(std::string name) : _name(name) { + theSwitchboard.SubscribeTo(this, "deleteWeapon"); + theSwitchboard.SubscribeTo(this, "canAttack"); + this->_readFile(name); + this->_canAttack = 1; +} +Weapon::~Weapon(void) { + return; +} + +/** + * Read a config file, base on the name of the class + * @param: name (std::string) + */ + +void Weapon::_readFile(std::string name) { + std::string file; + std::stringstream buffer; + std::ifstream fd; + + file = "./Resources/Elements/" + name + ".json"; + fd.open(file.c_str()); + if (!fd.is_open()) + Log::error("Can't open the file for the " + + name + " class. (Supposed path is Resources/Elements/" + name +".json)"); + buffer << fd.rdbuf(); + this->_parseJson(buffer.str()); +} + +/** + * Parse, read and stock the info in the config file + * @param: file (std::string) + * @note: file is the whole file content + */ +void Weapon::_parseJson(std::string file) { + Json::Reader read; + Json::Value json; + Json::ValueIterator i, v; + std::map<std::string, Json::Value> tmp; + + if (!read.parse(file, json)) + Log::error("Error in json syntax :\n" + read.getFormattedErrorMessages()); + if (json["infos"].get("name", "").asString() != this->_name) + Log::warning("The class name is different with the name in the config file."); + this->_name = json["infos"].get("name", "").asString(); + this->_flavor = json["infos"].get("flavor", "").asString(); + this->_active = json["infos"].get("active", "").asFloat(); + this->_recovery = json["infos"].get("recovery", "").asFloat(); + this->_size = json["infos"].get("size", "").asFloat(); + for (i = json["Actions"].begin(); i != json["Actions"].end(); i++) { + for (v = (*i).begin(); v != (*i).end(); v++) { + tmp[v.key().asString()] = (*v); + this->_attr[i.key().asString()] = tmp; + // Subcribe to the broadcasts + if (v.key().asString() == "subscribe") { + Log::info("SubscribeTo " + (*v).asString()); + theSwitchboard.SubscribeTo(this, (*v).asString() + "Pressed"); + theSwitchboard.SubscribeTo(this, (*v).asString() + "Released"); + } + } + } +} + +/** + * Get a Json::Value of a key in the config file + * @param: category (std::string) + * @param: key (std::string) + * @note: See the docs for the utilisation of Json::Value + */ +Json::Value Weapon::_getAttr(std::string category, std::string key) { + if (this->_attr.find(category) != this->_attr.end()) { + if (this->_attr[category].find(key) != this->_attr[category].end()) + return this->_attr[category][key]; + else + Log::warning("The key " + key + " not in the category " + category); + } else { + Log::warning("The category " + category + " not in the config"); + } + return nullptr; +} + +/** + * Called by the Character to attack with the currently equipped weapon + * @param: x (Int) + * @param: y (Int) + * @param: orientationX (int) + * @param: orientationY (int) + * @param: linearVelocity (b2Vec2) + */ + +void Weapon::attack(int x, int y, int orientationX, int orientationY, b2Vec2 linearVelocity) { + if (this->_canAttack == 1) { + this->_canAttack = 0; + //Waiting for orientation vars, have some rude code + int centerX = x + (this->_size / 2); + int centerY = y; +// this->_attackBox = new WeaponArea(centerX, centerY, linearVelocity); + theSwitchboard.DeferredBroadcast(new Message("deleteWeapon"), this->_active); + theSwitchboard.DeferredBroadcast(new Message("canAttack"), this->_recovery); + } +} + +void Weapon::ReceiveMessage(Message *m) { + if (m->GetMessageName() == "deleteWeapon") { + std::cout << "delete" << std::endl; + } + else if (m->GetMessageName() == "canAttack") { + std::cout << "recovery" << std::endl; + this->_canAttack = 1; + } +} void Weapon::BeginContact(Elements *elem, b2Contact *contact) { - std::cout << "TODO" << std::endl; } void Weapon::EndContact(Elements *elem, b2Contact *contact) { - std::cout << "TODO" << std::endl; } diff --git a/Sources/src/main.cpp b/Sources/src/main.cpp @@ -25,6 +25,8 @@ # include "../inc/Game.hpp" # include "../inc/Hero.hpp" +# include "../inc/Enemy.hpp" + class MouseDebugger: public MouseListener { public: @@ -41,18 +43,22 @@ class MouseDebugger: public MouseListener { int main(int ac, char **av) { Game *game = new Game(); - //game->grid(); + + game->grid(); game->readMaps(); game->initMap(); MouseDebugger l; theWorld.SetBackgroundColor(*(new Color(0.51f, 0.90f, 1))); + Hero *hero = new Hero(); + Enemy *enemy = new Enemy(); theCamera.LockTo(hero); game->displayHero(*(hero)); + game->displayEnemy(*(enemy)); hero->init(); + enemy->init(); //theWorld.SetSideBlockers(true, 0.7f); - Game::listElement(); game->start(); return 0; }