GP2P / LongFeiEngine

LongFei - Text-based 2D game engine in C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LongFeiEngine

This repository contains the source code for the LongFei game engine, a simple text-based 2D game engine written in C++.

Table of Contents

Project Structure

The project is organized into the following directories:

  • src: Contains the source code for the LongFei engine itself. This includes core components like the World Manager, Object Manager, Display Manager, Input Manager, and various event classes.
  • demo: Contains a simple demo project that utilizes the LongFei engine. This demonstrates how to use the engine to create basic game objects, handle events, and manage game logic.

Dependency

The LongFei engine depends on SFML. It is used for graphics, audio, and window management.

Building and Running the Demo

  1. Install SFML: Follow the instructions on the SFML website to install the library for your operating system. Then edit the library directory in ./CMakeLists.txt to your SFML install directory's include folder.
  2. Generate build files: Use CMake to generate build files for your preferred IDE or build system.
  3. Build the project: Build the project using your chosen build system. This will compile the engine and the demo executable.
  4. Run the demo: Run the demo executable to launch the demo game.

Run Tests

Run demo/src/tests.cpp's main() method

Using the LongFei Engine

To develop a game, replace files in the demo folder with your own game files. The engine is designed to be modular and extensible. You can easily add new game objects, event types, and functionality.

The LongFei engine provides a basic framework for developing 2D games. It includes features such as:

  • Object Management: Create and manage game objects with properties like position, velocity, and sprite.
  • Event Handling: Respond to various events such as keyboard input, mouse input, collisions, and custom events.
  • World Management: Manage the game world with features like collision detection and world boundaries.
  • Display Management: Draw sprites and text to the screen.

To use the engine, you will need to:

  • Create game objects: Derive your game objects from the Object class and implement their behavior. Handle events: Register for events and implement event handlers to respond to relevant events.
  • Update the game world: Use the World Manager to update the positions of objects and handle collisions.
  • Draw the game: Use the Display Manager to draw your game objects and other visual elements.

The demo project provides a simple example of how to use the engine to create a basic game. You can use this as a starting point for your own game development projects.

Engine Documentation

This section provides a brief overview of the core components of the LongFei engine:

Core Components

1. Object Management:

  • Object Class:

    • This is the base class for all game objects in the engine.
    • It defines properties such as:
      • id: Unique identifier for the object.
      • type: String representing the object type.
      • box: Defines the object's bounding box for collision detection.
      • position: Current position of the object in the world.
      • direction: The direction the object is facing.
      • speed: The object's movement speed.
      • altitude: Determines the drawing order (higher altitude objects are drawn on top).
      • solidness: Whether the object is solid and can collide with others.
      • animation: The animation associated with the object.
    • Key methods include:
      • eventHandler: Handles events that the object is interested in.
      • draw: Draws the object's sprite on the screen.
  • World Manager:

    • This singleton class manages all game objects in the world.
    • It handles tasks such as:
      • Inserting and removing objects.
      • Updating object positions based on their velocity.
      • Detecting and resolving collisions between objects.
      • Maintaining lists of objects for update and deletion.
      • Defining the boundaries of the game world.
      • Managing the view window (the portion of the world that is visible on the screen).
    • Key methods include:
      • insertObject: Adds an object to the world.
      • removeObject: Removes an object from the world.
      • update: Updates the positions of all objects and handles collisions.
      • draw: Calls the draw method of all objects in the world.
      • getCollisions: Returns a list of objects that collide with a given object at a specific position.

2. Event Handling:

  • Event Class:

    • This is the base class for all events in the engine.
    • It defines a common interface for different event types.
  • Event Types:

    • The engine defines various event types, including:
      • KeyboardEvent: Represents keyboard presses and releases.
      • MouseEvent: Represents mouse movements and clicks.
      • CollisionEvent: Represents collisions between objects.
      • StepEvent: Occurs at every game loop step.
      • OutEvent: Occurs when an object moves outside the world boundaries.
      • ViewEvent: Used to update ViewObjects.
      • NukeEvent: A custom event used in the demo project.
  • Event Handling Mechanism:

    • Objects can register interest in specific event types.
    • When an event occurs, the WorldManager dispatches it to all interested objects.
    • Objects can then handle the event in their eventHandler method.

3. Display Management:

  • Display Manager:

    • This singleton class handles drawing sprites and text to the screen.
    • It uses SFML for rendering.
  • ViewObject Class:

    • This class represents objects that display information on the screen, such as score, lives, or other game data.
    • It inherits from the Object class and adds features for displaying text and customizing its appearance.

4. Other Components:

  • Input Manager:

    • This singleton class handles keyboard and mouse input using SFML.
    • It provides methods for checking key presses and mouse button states.
  • Resource Manager:

    • This singleton class manages game resources such as sprites, sounds, and music.
    • It provides methods for loading and accessing resources.
  • Utility Functions:

    • The engine provides various helper functions for common tasks, such as converting between world coordinates and screen coordinates, calculating distances, and generating random numbers.

Demo Project

The demo project included in the repository demonstrates how to use the LongFei engine to create a simple game. The game features a hero who can shoot bullets at saucers.

The demo showcases various aspects of the engine, including:

  • Creating and managing game objects.
  • Handling keyboard and mouse input.
  • Detecting and resolving collisions.
  • Displaying sprites and text on the screen.
  • Using ViewObjects to display game information.

By studying the demo project, you can learn how to use the LongFei engine to develop your own 2D games.

Additional Notes

This README only provided a high-level overview and documentation of the LongFei engine. For detailed documentation, please refer to the comments and header files within the source code.

While the engine provides a basic framework for game development, it is still under development and may require further enhancements for more complex games.

Feel free to contribute to the project by adding features, fixing bugs, and improving the documentation.

I hope this README provides a helpful introduction to the LongFei game engine. Please feel free to explore the source code and experiment with the demo project.

About

LongFei - Text-based 2D game engine in C++


Languages

Language:C++ 96.7%Language:CMake 3.3%