aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-17 19:09:33 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-17 19:09:33 -0400
commit2cedd39a90fdb0387783b50446b16732517fb651 (patch)
tree78efc6f36096b7e0e0ff200220298c4ee7576bf1
parent0236eb7f6391c9d925dcaaddb8cb01ecfb7d5e55 (diff)
World can now draw properly, and camera follows player
-rw-r--r--Assets/world_normal.pngbin1204 -> 1877 bytes
-rw-r--r--Scripts/init.lua154
-rw-r--r--Scripts/world.lua20
-rw-r--r--Shaders/world.frag5
-rw-r--r--src/physics.cpp17
-rw-r--r--src/physics.hpp9
-rw-r--r--src/render.cpp47
-rw-r--r--src/render.hpp4
-rw-r--r--src/script.cpp8
-rw-r--r--src/world.cpp27
-rw-r--r--src/world.hpp3
11 files changed, 138 insertions, 156 deletions
diff --git a/Assets/world_normal.png b/Assets/world_normal.png
index e5f013c..1818895 100644
--- a/Assets/world_normal.png
+++ b/Assets/world_normal.png
Binary files differ
diff --git a/Scripts/init.lua b/Scripts/init.lua
index 78b2a76..84d2073 100644
--- a/Scripts/init.lua
+++ b/Scripts/init.lua
@@ -1,30 +1,30 @@
-bird = {
+player = {
Player = 0,
EventListeners = {
MoveLeftPressed = function(self)
- self.Velocity.x = self.Velocity.x - 100
+ self.Velocity.x = self.Velocity.x - 3
self.Render.flipx = true;
end,
MoveLeftReleased = function(self)
- self.Velocity.x = self.Velocity.x + 100
+ self.Velocity.x = self.Velocity.x + 3
end,
MoveRightPressed = function(self)
- self.Velocity.x = self.Velocity.x + 100
+ self.Velocity.x = self.Velocity.x + 3
self.Render.flipx = false;
end,
MoveRightReleased = function(self)
- self.Velocity.x = self.Velocity.x - 100
+ self.Velocity.x = self.Velocity.x - 3
end,
JumpKeyPressed = function(self)
if self.Physics.standing == true then
- self.Velocity.y = self.Velocity.y + 210
+ self.Velocity.y = self.Velocity.y + 9
end
end,
JumpKeyReleased = function(self)
end
},
Position = {
- x = 150,
+ x = 15,
y = 75
},
Velocity = {
@@ -62,127 +62,35 @@ bird = {
visibleTick = 0
}
-cat = {
- Velocity = {
- x = 0.0,
- y = 0.0
- },
- Position = {
- x = 180,
- y = -75
- },
- Render = {
- texture = "Assets/cat.png",
- normal = "Assets/cat_normal.png",
- visible = true
- },
- counter = 0;
- Idle = function(self)
- self.Velocity.x = -100 * math.sin(math.rad(self.counter));
- self.Velocity.y = 100 * math.cos(math.rad(self.counter));
- self.counter = self.counter + 5;
- end,
- PhysicsIdle = function(self)
- if self.Velocity.x < 0 then
- self.Render.flipx = true
- elseif self.Velocity.x > 0 then
- self.Render.flipx = false
- end
- end
-}
-
-animal = {
- Name = "animal",
- Velocity = { -- This will automatically assign position to (0,0)
- x = 0.0,
- y = 0.0
- },
- Render = {
- texture = "Assets/rabbit.png",
- visible = true
- },
- Light = {
- r = 0.0,
- b = 1.0,
- g = 0.2,
- strength = 1.0
- },
- Idle = function(self)
- self.Velocity.x = -200 * math.sin(math.rad(self.counter));
- self.Velocity.y = 500 * math.cos(math.rad(self.counter*5));
- self.counter = self.counter + 5;
- end,
- counter = 0,
- TestMe = function(self)
- print("Light: rgb("..self.Light.r..","..self.Light.g..","..self.Light.b..")")
- end
-}
-
-wall = {
- Position = {
- y = -100
- },
- Render = {
- texture = "Assets/rock.png",
- normal = "Assets/rock_normal.png",
- visible = true
- }
-}
-
-- Create the world
dofile("Scripts/world.lua")
-birdSpawn = game.spawn(bird);
-
-dogSpawn = game.spawn(cat);
-
-animalSpawn = game.spawn(animal);
-
-wallSpawn = game.spawn(wall);
-
-game.spawn({
- Velocity = {
- x = 0,
- y = 0
- },
- Light = {
- r = 0.8,
- g = 0.1,
- b = 0.2,
- strength = 0.75
- },
- counter = 0;
- Idle = function(self)
- self.Velocity.x = -100 * math.sin(math.rad(self.counter));
- self.Velocity.y = 100 * math.cos(math.rad(self.counter));
- self.counter = self.counter + 5;
- end
-});
+playerSpawn = game.spawn(player);
-------------------
-- SERIALIZING --
-------------------
-function serial (before_ser)
- binary = string.dump(before_ser)
- formatted_binary = ""
- for i = 1, string.len(binary) do
- dec, _ = ("\\%3d"):format(binary:sub(i, i):byte()):gsub(' ', '0')
- formatted_binary = formatted_binary .. dec
- end
- return formatted_binary
-end
-
-function hello(herro)
- print("Hello world ".. herro)
-end
-
---print(serial(hello))
-
-local ser = string.dump(hello)
-f2 = (loadstring or load)(ser)
-f2("shite")
-
-local testPut = string.dump(animalSpawn.TestMe)
-testRun = (loadstring or load)(testPut)
-testRun(animalSpawn)
+--function serial (before_ser)
+-- binary = string.dump(before_ser)
+-- formatted_binary = ""
+-- for i = 1, string.len(binary) do
+-- dec, _ = ("\\%3d"):format(binary:sub(i, i):byte()):gsub(' ', '0')
+-- formatted_binary = formatted_binary .. dec
+-- end
+-- return formatted_binary
+--end
+--
+--function hello(herro)
+-- print("Hello world ".. herro)
+--end
+--
+----print(serial(hello))
+--
+--local ser = string.dump(hello)
+--f2 = (loadstring or load)(ser)
+--f2("shite")
+--
+--local testPut = string.dump(animalSpawn.TestMe)
+--testRun = (loadstring or load)(testPut)
+--testRun(animalSpawn)
diff --git a/Scripts/world.lua b/Scripts/world.lua
index 8fb3136..044559a 100644
--- a/Scripts/world.lua
+++ b/Scripts/world.lua
@@ -63,21 +63,27 @@ world = {
Generate = function(self)
math.randomseed(self.Seed)
xsize, ysize, zsize = self:setSize(250, 128, 3)
- --self.data = {}
for Z = 0,zsize-1 do
- --self.data[Z] = {}
for X = 0,xsize-1 do
- --self.data[Z][X] = {}
- YGen = math.floor(6*math.sin(X/20) + Z) + 64
- YDepth = math.random(2,5)
+ if Z == 0 then
+ YGen = math.floor(6*math.sin(X/20)) + 64
+ elseif Z == 1 then
+ YGen = math.floor(9*math.sin(X/20)) + 64
+ else
+ YGen = math.floor(15*math.sin(X/20)) + 64
+ end
+ YDepth = math.random(3,5)
for Y = 0,ysize-1 do
if Y == YGen then
self:setData(X, Y, Z, "grass");
elseif Y < YGen and Y > (YGen - YDepth) then
self:setData(X, Y, Z, "dirt");
elseif Y < YGen then
- --self:setData(X, Y, Z, "stone");
- self:setData(X, Y, Z, "grass");
+ self:setData(X, Y, Z, "stone");
+ elseif Y == YGen + 1 then
+ if math.random(0, 100) == 53 then
+ self:setData(X, Y, Z, "flower");
+ end
end
--print(X..","..Y..","..Z);
end
diff --git a/Shaders/world.frag b/Shaders/world.frag
index 2b5ab5e..18945a8 100644
--- a/Shaders/world.frag
+++ b/Shaders/world.frag
@@ -22,7 +22,7 @@ uniform int Flipped;
void main()
{
// Quadratic light falloff
- vec3 Falloff = vec3(0.1, 0.2, 0.0);
+ vec3 Falloff = vec3(0.4, 0.1, 0.002);
vec4 DiffuseColor = texture2D(textu, texCoord);
@@ -33,7 +33,8 @@ void main()
vec3 SumLight = vec3(0.0);
for (int i = 0; i < LightNum; i++) {
- vec3 LightDir = vec3(LightPos[i].xy - fragCoord.xy, LightPos[i].z);
+ vec3 LightDir = vec3(LightPos[i].xy - fragCoord.xy,
+ (LightPos[i].z - fragCoord.z)*2);
float D = length(LightDir);
diff --git a/src/physics.cpp b/src/physics.cpp
index 31569ab..1f70ecc 100644
--- a/src/physics.cpp
+++ b/src/physics.cpp
@@ -18,6 +18,7 @@
*/
#include "physics.hpp"
+
#include "components/Physics.hpp"
#include "components/Position.hpp"
#include "components/Velocity.hpp"
@@ -25,7 +26,7 @@
void PhysicsSystem::configure([[maybe_unused]]entityx::EntityManager& entities,
[[maybe_unused]]entityx::EventManager& events)
{
-
+ events.subscribe<WorldChangeEvent>(*this);
}
void PhysicsSystem::update([[maybe_unused]]entityx::EntityManager& entities,
@@ -33,17 +34,18 @@ void PhysicsSystem::update([[maybe_unused]]entityx::EntityManager& entities,
[[maybe_unused]]entityx::TimeDelta dt)
{
entities.each<Position, Velocity>
- ([dt](entityx::Entity e, Position &pos, Velocity &vel){
+ ([this, dt](entityx::Entity e, Position &pos, Velocity &vel){
bool has_phys = e.has_component<Physics>();
pos.x += (vel.x * dt/1000.0);
pos.y += (vel.y * dt/1000.0);
- double fallPosition = 0;
-
// TODO make this intersect world instead of 0 y
if (has_phys) {
+
+ double fallPosition = currentWorld->getHeight(pos.x, pos.y, 0.0);
+
Physics *p = e.component<Physics>().get();
// TODO only make this occur when the entity has a hitbox
if (pos.y == fallPosition) {
@@ -57,8 +59,13 @@ void PhysicsSystem::update([[maybe_unused]]entityx::EntityManager& entities,
p->standing = true;
} else {
p->standing = false;
- vel.y -= 920 * (dt/1000.0f);
+ vel.y -= 32.2 * (dt/1000.0f);
}
}
});
}
+
+void PhysicsSystem::receive(const WorldChangeEvent& wce)
+{
+ currentWorld = wce.newWorld;
+}
diff --git a/src/physics.hpp b/src/physics.hpp
index 8231d6d..db93999 100644
--- a/src/physics.hpp
+++ b/src/physics.hpp
@@ -24,13 +24,18 @@
#include <entityx/entityx.h>
#include <sol/sol.hpp>
+#include "world.hpp"
+#include "events/world.hpp"
+
/**
* @class PhysicsSystem
* Handles the position and velocity updating of all entities
*/
-class PhysicsSystem : public entityx::System<PhysicsSystem>
+class PhysicsSystem : public entityx::System<PhysicsSystem>,
+ public entityx::Receiver<PhysicsSystem>
{
private:
+ World* currentWorld;
public:
PhysicsSystem(void) {}
@@ -49,6 +54,8 @@ public:
void update(entityx::EntityManager& entites,
entityx::EventManager& events,
entityx::TimeDelta dt) final;
+
+ void receive(const WorldChangeEvent& wce);
};
#endif // SYSTEM_PHYSICS_HPP_
diff --git a/src/render.cpp b/src/render.cpp
index b991b2f..2b49b2c 100644
--- a/src/render.cpp
+++ b/src/render.cpp
@@ -28,6 +28,7 @@ void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
[[maybe_unused]] entityx::EventManager& events)
{
events.subscribe<WorldMeshUpdateEvent>(*this);
+ events.subscribe<entityx::ComponentAddedEvent<Player>>(*this);
init();
}
@@ -53,7 +54,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
***********/
glm::mat4 view = glm::lookAt(camPos, // Pos
- glm::vec3(0.0f, 0.0f, 0.0f), // Facing
+ glm::vec3(camPos.x, camPos.y, 0.0f), // Facing
glm::vec3(0.0f, 1.0f, 0.0f)); // Up
//glm::mat4 projection = glm::perspective(45.0f,
@@ -61,16 +62,19 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
// 0.01f,
// 2048.0f);
- glm::mat4 projection = glm::ortho(-((float)width/2), // Left
- ((float)width/2), // Right
- -((float)height/2), // Bottom
- ((float)height/2), // Top
- -10.0f, // zNear
- 10.0f // zFar
+ float scale = 40.0f;
+ float scaleWidth = static_cast<float>(width) / scale;
+ float scaleHeight = static_cast<float>(height) / scale;
+
+ glm::mat4 projection = glm::ortho(-(scaleWidth/2), // Left
+ (scaleWidth/2), // Right
+ -(scaleHeight/2), // Bottom
+ (scaleHeight/2), // Top
+ 10.0f, // zFar
+ -10.0f // zNear
);
glm::mat4 model = glm::mat4(1.0f);
- model = glm::scale(model, glm::vec3(20.0f, 20.0f, 1.0f));
glUseProgram(s);
@@ -91,10 +95,23 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
GLfloat amb[4] = {1.0f, 1.0f, 1.0f, 0.0f};
glUniform4fv(b, 1, amb);
+ /************
+ * CAMERA *
+ ************/
+ try {
+ if (player.has_component<Position>()) {
+ Position *pos = player.component<Position>().get();
+ camPos.y = pos->y;
+ camPos.x = pos->x;
+ }
+ } catch (...) { // If the player doesn't exist or anything goes wrong
+ camPos.y = 0.0f;
+ camPos.x = 0.0f;
+ }
+
/**************
* LIGHTING *
**************/
-
std::vector<glm::vec3> lightPos;
std::vector<glm::vec4> lightColor;
@@ -103,7 +120,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
entities.each<Light, Position>([&]
(entityx::Entity, Light &l, Position &p){
- lightPos.push_back(glm::vec3(p.x, p.y,-10.0));
+ lightPos.push_back(glm::vec3(p.x, p.y, 1.0));
lightColor.push_back(glm::vec4(l.r, l.g, l.b, l.strength));
lightNum++;
});
@@ -132,8 +149,8 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
// e.component<Scripted>()->updateRender();
//}
- float w = r.texture.width/2.0f;
- float h = r.texture.height;
+ float w = 0.5f;
+ float h = (float)r.texture.height/r.texture.width;
GLuint tri_vbo;
GLfloat tri_data[] = {
@@ -179,6 +196,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
5*sizeof(float), (void*)(3*sizeof(float)));
glDrawArrays(GL_TRIANGLES, 0, 6);
});
+ glUniform1i(f, 0);
// If we were given a world VBO render it
if (worldVBO) {
@@ -299,3 +317,8 @@ void RenderSystem::receive(const WorldMeshUpdateEvent &wmu)
worldTexture = wmu.worldTexture;
worldNormal = wmu.worldNormal;
}
+
+void RenderSystem::receive(const entityx::ComponentAddedEvent<Player> &cae)
+{
+ player = cae.entity;
+}
diff --git a/src/render.hpp b/src/render.hpp
index 26e525b..f3064d1 100644
--- a/src/render.hpp
+++ b/src/render.hpp
@@ -38,6 +38,8 @@
#include "world.hpp"
#include "events/world.hpp"
+#include "components/Player.hpp"
+
class RenderSystem : public entityx::System<RenderSystem>,
public entityx::Receiver<RenderSystem>
{
@@ -56,6 +58,7 @@ private:
unsigned int worldVertex = 0;
GLuint worldTexture = 0;
GLuint worldNormal = 0;
+ entityx::Entity player; // Save the player so we can track the camera
public:
RenderSystem() :
window(nullptr, SDL_DestroyWindow) {}
@@ -89,6 +92,7 @@ public:
* EVENTS *
************/
void receive(const WorldMeshUpdateEvent &wmu);
+ void receive(const entityx::ComponentAddedEvent<Player> &cae);
};
diff --git a/src/script.cpp b/src/script.cpp
index cf5b2dd..fd99228 100644
--- a/src/script.cpp
+++ b/src/script.cpp
@@ -188,10 +188,6 @@ sol::table ScriptSystem::spawn(sol::object param)
e.assign<Velocity>(Velocity().FromLua(tab["Velocity"])).get();
}
- if (tab["Player"] != nullptr) {
- (*toRet)["Player"] = e.assign<Player>().get();
- }
-
if (tab["Physics"] != nullptr) {
if (!e.has_component<Position>()) // Position must exist for phys.
(*toRet)["Position"] = e.assign<Position>().get();
@@ -214,6 +210,10 @@ sol::table ScriptSystem::spawn(sol::object param)
e.assign<EventListener>(listeners);
}
+ if (tab["Player"] != nullptr) {
+ (*toRet)["Player"] = e.assign<Player>().get();
+ }
+
} else {
// TODO better logging
std::cerr << "Parameter to spawn() must be a table!" << std::endl;
diff --git a/src/world.cpp b/src/world.cpp
index 7710ecc..c20ce17 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -180,6 +180,31 @@ unsigned int World::setSeed(unsigned int s)
return seed;
}
+/* PHYSICS */
+double World::getHeight(double x, double y, double z)
+{
+ unsigned int X = static_cast<unsigned int>(x);
+ unsigned int Z = static_cast<unsigned int>(z);
+
+ double Y = 0.0;
+ try {
+ auto &d = data.at(Z).at(X);
+ for (int yi = d.size()-1; yi >= 0; yi--) {
+ if (d.at(yi) >= 0) {
+ if (!registry.at(d.at(yi)).passable) {
+ Y = static_cast<double>(yi);
+ Y += 1;
+ break;
+ }
+ }
+ }
+ } catch (...) { // If we get any errors, just let the character
+ return y;
+ }
+
+ return Y;
+}
+
/******************
* WORLD SYSTEM *
@@ -188,8 +213,6 @@ unsigned int World::setSeed(unsigned int s)
World* WorldSystem::addWorld(sol::object t)
{
worlds.push_back(World(t));
- if (currentWorld == nullptr)
- currentWorld = &(worlds.back());
return &(worlds.back());
}
diff --git a/src/world.hpp b/src/world.hpp
index 696f0aa..9314fa6 100644
--- a/src/world.hpp
+++ b/src/world.hpp
@@ -112,6 +112,9 @@ public:
/* SEED */
unsigned int getSeed();
unsigned int setSeed(unsigned int);
+
+ /* PHYSICS */
+ double getHeight(double x, double y, double z);
};
/**