|
| 1 | +/* |
| 2 | + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). |
| 3 | + * |
| 4 | + * The MIT License |
| 5 | + * Copyright © 2014-2022 Ilkka Seppälä |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package com.iluwatar; |
| 26 | + |
| 27 | +/** |
| 28 | + * The main entry point for the application. |
| 29 | + * This class simulates a game loop where entities are created, updated, and their states are modified. |
| 30 | + */ |
| 31 | +public class App { |
| 32 | + |
| 33 | + /** |
| 34 | + * The main method that runs the application. |
| 35 | + * It creates entities, adds components, updates them over several frames, |
| 36 | + * and demonstrates applying damage and forces to entities. |
| 37 | + * |
| 38 | + * @param args Command-line arguments (not used in this application) |
| 39 | + */ |
| 40 | + public static void main(String[] args) { |
| 41 | + // Create example entities |
| 42 | + Entity entity1 = new Entity("Entity1"); |
| 43 | + Entity entity2 = new Entity("Entity2"); |
| 44 | + |
| 45 | + // Set up some transform components (position, rotation, scale) |
| 46 | + TransformComponent transform1 = new TransformComponent(new float[]{0.0f, 0.0f, 0.0f}, |
| 47 | + new float[]{0.0f, 0.0f, 0.0f}, new float[]{1.0f, 1.0f, 1.0f}); |
| 48 | + TransformComponent transform2 = new TransformComponent(new float[]{5.0f, 0.0f, 0.0f}, |
| 49 | + new float[]{0.0f, 45.0f, 0.0f}, new float[]{1.0f, 1.0f, 1.0f}); |
| 50 | + |
| 51 | + // Set the transform components for each entity |
| 52 | + entity1.addComponent(transform1); |
| 53 | + entity2.addComponent(transform2); |
| 54 | + |
| 55 | + // Create a health component for entity1 with initial health of 100 |
| 56 | + HealthComponent health1 = new HealthComponent(100); // Ensure HealthComponent is implemented |
| 57 | + entity1.addComponent(health1); |
| 58 | + |
| 59 | + // Create a velocity component for entity1 |
| 60 | + VelocityComponent velocity1 = new VelocityComponent(1.0f, 0.0f, 0.0f); |
| 61 | + entity1.addComponent(velocity1); |
| 62 | + |
| 63 | + // Set up a system and add entities to the system |
| 64 | + GameSystem gameSystem = new GameSystem(); |
| 65 | + gameSystem.addEntity(entity1); |
| 66 | + gameSystem.addEntity(entity2); |
| 67 | + |
| 68 | + // Simulate game update loop (e.g., 60 FPS) |
| 69 | + float deltaTime = 1.0f / 60.0f; // 60 FPS |
| 70 | + |
| 71 | + // Simulate for a few frames |
| 72 | + for (int i = 0; i < 10; i++) { |
| 73 | + System.out.println("Frame: " + (i + 1)); |
| 74 | + |
| 75 | + // Update all entities in the system |
| 76 | + gameSystem.update(deltaTime); |
| 77 | + |
| 78 | + // Apply some damage to entity1's health component at frame 6 |
| 79 | + if (i == 5) { |
| 80 | + health1.applyDamage(30); |
| 81 | + System.out.println("Entity1's health after damage: " + health1.getCurrentHealth()); |
| 82 | + } |
| 83 | + |
| 84 | + // Apply some force to entity1's velocity at frame 3 |
| 85 | + if (i == 3) { |
| 86 | + velocity1.applyForce(0.5f, 0.0f, 0.0f); |
| 87 | + System.out.println("Entity1's velocity after force: (" + velocity1.getVelocityX() + ", " |
| 88 | + + velocity1.getVelocityY() + ", " + velocity1.getVelocityZ() + ")"); |
| 89 | + } |
| 90 | + |
| 91 | + // Render the system (optional rendering logic can be added here) |
| 92 | + gameSystem.renderSystem(); |
| 93 | + } |
| 94 | + |
| 95 | + // After the simulation, check final entity states |
| 96 | + System.out.println("\nFinal Entity States:"); |
| 97 | + System.out.println("Entity1 position: " + entity1.getTransformComponent().getPosition()[0] + ", " |
| 98 | + + entity1.getTransformComponent().getPosition()[1] + ", " |
| 99 | + + entity1.getTransformComponent().getPosition()[2]); |
| 100 | + System.out.println("Entity1 velocity: " + velocity1.getVelocityX() + ", " |
| 101 | + + velocity1.getVelocityY() + ", " + velocity1.getVelocityZ()); |
| 102 | + System.out.println("Entity1 health: " + health1.getCurrentHealth()); |
| 103 | + } |
| 104 | +} |
0 commit comments